stl常用的查找算法
#include<iostream> using namespace std; #include"vector" #include"algorithm" #include"functional" bool lowThree(int &iNum) { return (iNum > 3); } int main() { vector<int> v1; v1.push_back(1); v1.push_back(2); v1.push_back(2); v1.push_back(6); v1.push_back(8); // adjacent_find在iterator对标识元素范围内,查找一对相邻重复元素, //找到则返回指向这对元素的第一个元素的迭代器。否则返回past-the-end。 vector<int> ::iterator it = adjacent_find(v1.begin(), v1.end()); cout << *it << endl; //初步结论,在stl算法中的谓词不能用数字代替,而函数可以用数字代替 //在有序序列中查找value,找到则返回true。注意:在无序序列中,不可使用。 bool b1 = binary_search(v1.begin(), v1.end(), 6); cout << b1 << endl; //利用等于操作符,把标志范围内的元素与输入值比较,返回相等的个数。 int i1 = count(v1.begin(), v1.end(), 5); cout << i1 << endl; //参数中要求是谓词。可以直接写一个回调函数 int i2 = count_if(v1.begin(), v1.end(), lowThree); cout << i2 << endl; vector<int> ::iterator it22 = find(v1.begin(), v1.end(), 5); cout << *it22 << endl; vector<int> ::iterator it3 = find_if(v1.begin(), v1.end(), lowThree); cout << *it3 << endl; system("pause"); }