adjacent_find()
在iterator对标识元素范围内,查找一对相邻重复元素,找到则返回指向这对元素的第一个元素的迭代器。否则返回past-the-end。
#include <iostream> #include <cstdio> #include <algorithm> #include <vector> using namespace std; void play_adjacent_find() { vector<int> v1; v1.push_back(1); v1.push_back(2); v1.push_back(2); v1.push_back(3); v1.push_back(5); vector<int>::iterator it = adjacent_find(v1.begin(), v1.end()); if (it == v1.end()) { cout << "没有找到 重复的元素" << endl; } else { cout << *it << endl; } // 2 int index = distance(v1.begin(), it); cout << index << endl; // 1 } int main() { play_adjacent_find(); return 0; }
binary_search()
在有序序列中查找value,找到则返回true。注意:在无序序列中,不可使用。
#include <iostream> #include <cstdio> #include <algorithm> #include <vector> using namespace std; // binary_search是对排序好的进行查找 void play_binary_search() { vector<int> v1; v1.push_back(1); v1.push_back(3); v1.push_back(5); v1.push_back(7); v1.push_back(9); bool b = binary_search(v1.begin(), v1.end(), 7); if (b) { cout << "find success\n"; } else { cout << "find fail\n"; } // find success } int main() { play_binary_search(); return 0; }
count()
利用等于操作符,把标志范围内的元素与输入值比较,返回相等的个数。
#include <iostream> #include <cstdio> #include <algorithm> #include <vector> using namespace std; void play_count() { vector<int> v1; v1.push_back(1); v1.push_back(3); v1.push_back(3); v1.push_back(7); v1.push_back(9); v1.push_back(3); int cnt = count(v1.begin(), v1.end(), 3); cout << "count of 3: " << cnt << endl; // count of 3: 3 } int main() { play_count(); return 0; }
count_if()
#include <iostream> #include <cstdio> #include <algorithm> #include <vector> using namespace std; bool GreaterThree(const int &a) { return a > 3; } void play_count_if() { vector<int> v1; v1.push_back(1); v1.push_back(3); v1.push_back(4); v1.push_back(7); v1.push_back(9); v1.push_back(3); int cnt = count_if(v1.begin(), v1.end(), GreaterThree); cout << "count of greater 3: " << cnt << endl; // count of greater 3: 3 } int main() { play_count_if(); return 0; }
find()
find: 利用底层元素的等于操作符,对指定范围内的元素与输入值进行比较。当匹配时,结束搜索,返回该元素的迭代器。
#include <iostream> #include <cstdio> #include <algorithm> #include <vector> using namespace std; void play_find() { vector<int> v1; v1.push_back(1); v1.push_back(3); v1.push_back(4); v1.push_back(7); v1.push_back(9); v1.push_back(3); vector<int>::iterator it = find(v1.begin(), v1.end(), 4); if (it == v1.end()) { cout << "find fail\n"; } else { cout << "find success\n"; } // find success } int main() { play_find(); return 0; }
find_if()
find_if: 使用输入的函数代替等于操作符执行find。返回被找到的元素的迭代器。
#include <iostream> #include <cstdio> #include <algorithm> #include <vector> using namespace std; bool GreaterThree(const int &a) { return a > 3; } void play_find_if() { vector<int> v1; v1.push_back(1); v1.push_back(3); v1.push_back(4); v1.push_back(7); v1.push_back(9); v1.push_back(3); vector<int>::iterator it = find_if(v1.begin(), v1.end(), GreaterThree); if (it == v1.end()) { cout << "find fail\n"; } else { cout << "find success\n"; cout << "value: " << *it << endl; } // find success // value: 4 } int main() { play_find_if(); return 0; }