search_n() 用来查找连续的n个匹配的数值 或者 加谓词
search_n(b, e, c, v)
search_n(b, e, c, v, p)
注意:该方法的第二种形式应该是search_n_if(b, e, c, p)
#include<iostream> #include<algorithm> #include<deque> // #include<functional> // using namespace std; int main() { deque<int> ideq; for (int i = 1; i <= 9; i++) { if (i == 3) { ideq.push_back(i); ideq.push_back(i); ideq.push_back(i); ideq.push_back(i); } else { ideq.push_back(i); } } for (deque<int>::iterator iter = ideq.begin(); iter != ideq.end(); iter++) cout << *iter << endl; /*if (pos != string::npos) cout << "找到了!" << pos << endl; else cout << "没找到!" << endl;*/ deque<int>::iterator pos; pos = search_n(ideq.begin(), ideq.end(), 4, 3); if (pos != ideq.end()) { cout << "找到了连续的4个3!,序号位置" << distance(ideq.begin(), pos) + 1 << endl; } else { cout << "没找到!" << endl; } // 这里的谓词是 二元 的 pos = search_n(ideq.begin(), ideq.end(), 3, 6, greater<int>()); //pos = search_n_if(ideq.begin(), ideq.end(), 3, bind2nd(greater<int>(), 6)); if (pos != ideq.end()) { cout << "找到了连续的3个大于6的数!start with" << distance(ideq.begin(), pos) + 1 << endl; } else cout << "没找到!" << endl; // system("pause"); return 0; }
时间: 2024-11-11 20:17:19