基本STL算法库使用
1、 count_if 和 lambda联用
vector<int> vec;
for (int i = 0; i < 20; i++) {
vec.push_back(i);
}
//count_if
int num = count_if(vec.begin(), vec.end(),
[](int x) { return x > 1 && x < 9; });
cout << "大于2小于10的个数:" << num << endl;
2、 count
//count
vec.push_back(19);
int n = count(vec.begin(), vec.end(), 19);
cout << "vec中19的个数:" << n << endl;
same as
int num = count_if(vec.begin(),vec.end(),[](int x){ return} x == 19;})
3、 min max
vector<int>::iterator minIter;
minIter = min_element(vec.begin(), vec.end());
vector<int>::iterator maxIter;
maxIter = max_element(vec.begin(),vec.end());
cout << "min:" << *minIter << " max:" << *maxIter << endl;
pair<vector<int>::iterator, vector<int>::iterator> iterPairs;
iterPairs = minmax_element(vec.begin(), vec.end());
cout << "min:" << *iterPairs.first << " max:" << *iterPairs.second << endl;
4、 自定义比较函数,获取最大最小值
vector<int> vec1; //vec1 = {12,32,91,99,101}
vec1.push_back(12);
vec1.push_back(32);
vec1.push_back(91);
vec1.push_back(66);
vec1.push_back(101);
iterPairs = minmax_element(vec1.begin(), vec1.end(), [](int x, int y) {
if (x % 10 != y % 10) {
return(x % 10 < y % 10);
}
else { return x < y; }
});
cout << "min:" << *iterPairs.first << " max:" << *iterPairs.second << endl;
5、 Linear Searching
return first match
find find_if find_if_not
auto iter = find_if(vec1.begin(), vec1.end(), [](int x) {return x > 100; });
if (iter != vec1.end()) {
cout << "suceess find" <<*iter<<endl;
}
iter = find(vec1.begin(), vec1.end(), 12);
iter = find_if_not(vec1.begin(), vec1.end(), [](int x) { return x > 100; });
if (iter != vec1.end()) {
cout << *iter << endl;
}
6、search
search_n search find_end
auto iter_s = search_n(vec1.begin(), vec1.end(), 2, 12);
auto ptr = search(vec1.begin(), vec1.end(), sub.begin(), sub.end());
auto ptrEnd = find_end(vec1.begin(), vec1.end(), sub.begin(), sub.end());
7
iter = find_first_of(vec1.begin(), vec1.end(), sub.begin(), sub.end());
iter = find_first_of(vec1.begin(), vec1.end(), sub.begin(), sub.end(),[](int x, int y){return x == y*4;});
iter = a
8、adjacent_find
vector<int> vec2;
vec2.push_back(12);
vec2.push_back(13);
vec2.push_back(14);
vec2.push_back(14);
vec2.push_back(15);
vec2.push_back(60);
vec2.push_back(70);
auto aiter = adjacent_find(vec2.begin(), vec2.end());
auto aaiter = adjacent_find(vec2.begin(), vec2.end(), [](int x, int y) { return y == x * 4; });
9、 comparing range
equal is_permutation
if (equal(vec1.begin(), vec1.end(), vec2.begin())) {
cout << " same" << endl;
}
if (is_permutation(vec1.begin(), vec1.end(), vec2.begin(),vec2.end())) {
cout << " have same item, but different order" << endl;
}
10. mismatch
/*
* find first difference
* pair_iter.first is iterator of vec1
* pair_iter.second is iterator of vec2
*/
auto pair_iter = mismatch(vec1.begin(), vec1.end(), vec2.begin());
10、
/*
* one by one compare item
* {1,2,3,4}<{1,2,4,5}
*/
bool cp = lexicographical_compare(vec1.begin(), vec1.end(), vec2.begin(),vec2.end());
11、is_sorted is_sorted_until
bool is_st = is_sorted(vec1.begin(), vec1.end());
auto is_st_iter = is_sorted_until(vec1.begin(), vec1.end());
12 is_partitioned
//check if vec2 is partitioned by the condition x >10
bool is_par = is_partitioned(vec2.begin(), vec2.end(), [](int x) {return x > 10; });
### 13 is_heap is_heap_until
//check if vec is a heap
is_heap(vec.begin(),vec.end());
auto iter = is_heap_until(vec.begin(),vec.end());
### 14 all_of any_of none_of
``` bool allb = all_of(vec1.begin(), vec1.end(), [](int x) { return x > 10; });
bool anyb = any_of(vec1.begin(), vec1.end(), [](int x) { return x > 10; });
bool noneb = none_of(vec1.begin(), vec1.end(), [](int x) { return x > 10; });