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; }
#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; }
#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; }
#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: 利用底层元素的等于操作符,对指定范围内的元素与输入值进行比较。当匹配时,结束搜索,返回该元素的迭代器。
#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; }
#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; }
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/zyq522376829/article/details/46830805