参考文献《大道至简:C++STL》
- for_each()、count()、min_element()/max_element()
#include#include #include #include #include #ifndef __print__ #define __print__(x) {std::cout << x << " ";} #endif #ifndef newline #define newline() {std::cout << std::endl;} #endif using namespace std; template
void _pow(T& t) {t *= t;} template void print(const T& t){__print__(t);} class SUM { private: long total; public: SUM():total(0){} void operator()(int val) { total += val; } operator double() { return static_cast (total); } }; bool is_even(const int& x){return x % 2 == 0;} template bool abs_val(const T& t1, const T& t2){return abs(t1) < abs(t2);}; int main() { vector vint = {1, 3, 5, 7}; for_each(vint.begin(), vint.end(), print ); newline(); for_each(vint.begin(), vint.end(), _pow ); for_each(vint.begin(), vint.end(), print ); newline(); double sum = for_each(vint.begin(), vint.end(), SUM()); print(sum); newline(); deque dint = {2, 5, 4, 1, 8, 3, 6}; for_each(dint.begin(), dint.end(), print ); newline(); print(count(dint.begin(), dint.end(), 4)); print(count_if(dint.begin(), dint.end(), bind2nd(greater (), 3))); print(count_if(dint.begin(), dint.end(), is_even)); newline(); list lint = {-1, 4, 6, -12, 9}; print(*min_element(lint.begin(), lint.end())); newline(); print(*max_element(lint.begin(), lint.end())); newline(); print(*min_element(lint.begin(), lint.end(), abs_val )); //-1 newline(); print(*max_element(lint.begin(), lint.end(), abs_val )); //-12 newline(); return 0; }
- find()、find_if()
#include#include #include #include using namespace std; #ifndef newline #define newline() {cout << endl;} #endif template void print(const T& t){cout << t << " ";} template class greater_even : public binary_function { public: bool operator()(const T& key, const T& val) const{ return key > val && key % 2 == 0; } }; int main(){ vector vec = {2, 3, 5, 7, 7, 2, 5, 6}; for_each(vec.begin(), vec.end(), print ); newline(); vector ::iterator vpos; vpos = find(vec.begin(), vec.end(), 5); print (distance(vec.begin(), vpos)); newline(); vpos = find_if(vec.begin(), vec.end(), bind2nd(greater_even (), 5)); print (distance(vec.begin(), vpos)); newline(); return 0; }
- 搜索算法
#include#include #include #include #include using namespace std; #ifndef newline #define newline() {cout << endl;} #endif template void print(const T& t){cout << t << " ";} void search_resault( const vector ::iterator& pos, const vector ::iterator& beg, const vector ::iterator& end, const string& out){ print(out); if(pos == end){ print("Not Found!"); } else{ print(distance(beg, pos)); } newline(); } void search_resault( const vector ::reverse_iterator& pos, const vector ::iterator& beg, const vector ::iterator& end, const string& out){ print(out); if(pos.base() == end){ print("Not Found!"); } else{ print(distance(beg, pos.base())); } newline(); } bool check_even(const int& elem, const bool& is_even){ return is_even ? elem % 2 == 0 : elem % 2 != 0; } bool diff4th(const int& a, const int& b){ return b == a - 4; } int main(){ vector vec = {1, 2, 3, 4, 5, 6, 2, 3, 4, 7, 7, 8}; for_each(vec.begin(), vec.end(), print ); newline(); vector ::iterator vpos; vpos = search_n(vec.begin(), vec.end(), 3, 4); search_resault(vpos, vec.begin(), vec.end(), "vec中3个连续为4的值的起始位置为"); vpos = search_n(vec.begin(), vec.end(), 3, 3, greater ()); search_resault(vpos, vec.begin(), vec.end(), "vec中3个连续大于3的值的起始位置为"); vector subvec = {2, 3, 4}; vpos = search(vec.begin(), vec.end(), subvec.begin(), subvec.end()); search_resault(vpos, vec.begin(), vec.end(), "subvec在vec中的起始位置为"); vector is_even = {true, false, true}; vpos = search(vec.begin(), vec.end(), is_even.begin(), is_even.end(), check_even); search_resault(vpos, vec.begin(), vec.end(), "vec中连续三个匹配is_even的起始位置为"); vpos = find_end(vec.begin(), vec.end(), is_even.begin(), is_even.end(), check_even); search_resault(vpos, vec.begin(), vec.end(), "vec中连续三个匹配is_even的最后一组的起始位置为"); vpos = find_first_of(vec.begin(), vec.end(), subvec.begin(), subvec.end()); search_resault(vpos, vec.begin(), vec.end(), "vec中连续三个匹配subvec的第一个字串的起始位置为"); vector ::reverse_iterator rvpos; rvpos = find_first_of(vec.rbegin(), vec.rend(), subvec.begin(), subvec.end()); search_resault(rvpos, vec.begin(), vec.end(), "vec中逆向连续三个匹配subvec的第一个字串的起始位置为"); vpos = adjacent_find(vec.begin(), vec.end()); search_resault(vpos, vec.begin(), vec.end(), "vec中连续两个相等元素的起始位置为"); vpos = adjacent_find(vec.begin(), vec.end(), diff4th); search_resault(vpos, vec.begin(), vec.end(), "vec中后一个元素比前一个元素小4的起始位置为"); return 0; }
- 比较算法
#include#include #include using namespace std; #ifndef newline #define newline() {cout << endl;} #endif template void print(const T& t){ cout << t << " "; } template void print(const T& t1, const T& t2){ cout << t1 << "," << t2; } bool relation(const int& x, const int& y){ return x == y - 1; } int main(){ vector va = {2, 3, 4, 5, 6, 9}; vector vb = {3, 4, 5, 6, 7, 8}; print(equal(va.begin(), va.end(), vb.begin())); //0 newline(); print(equal(va.begin(), va.end(), vb.begin(), relation)); //0 newline(); pair ::iterator, vector ::iterator> pos; pos = mismatch(va.begin() + 1, va.end(), vb.begin()); print(*pos.first, *pos.second); //9,7 newline(); pos = mismatch(va.begin(), va.end(), vb.begin(), less_equal ()); print(*pos.first, *pos.second); //9,8 newline(); print(lexicographical_compare(va.begin(), va.end(), vb.begin(), vb.end())); //1 newline(); return 0; }
- 修改性算法
#include#include #include #include using namespace std; #ifndef newline #define newline() {cout << endl;} #endif template
void print(const T& t){ cout << t << " "; } template void print_list(const list & t){ for_each(t.begin(), t.end(), print ); newline(); } template void print_vector(const vector & t){ for_each(t.begin(), t.end(), print ); newline(); } class custom{ public: custom(const int& _times):times(_times){} int operator()(const int& elem1, const int& elem2){ return (elem1 + elem2) * times; } private: int times; }; int main(){ vector va = {1, 2, 3, 4, 5, 6, 7}; list la; copy(va.begin(), va.end(), back_inserter(la)); print_list(la); //1 2 3 4 5 6 7 copy_backward(va.begin() + 2, va.begin() + 5, la.end()); print_list(la); //1 2 3 4 3 4 5 la.clear(); transform(va.begin(), va.end(), back_inserter(la), negate ()); print_list(la); //-1 -2 -3 -4 -5 -6 -7 transform(va.begin(), va.end(), la.begin(), bind2nd(multiplies (), 10)); print_list(la); //10 20 30 40 50 60 70 transform(va.begin(), va.end(), la.begin(), la.begin(), multiplies ()); print_list(la); //10 40 90 160 250 360 490 la.clear(); transform(va.begin(), va.end(), la.begin(), back_inserter(la), plus ()); print_list(la); //1 3 6 10 15 21 28 la.clear(); transform(va.begin(), va.end(), la.begin(), back_inserter(la), custom(2)); print_list(la); //2 8 22 52 114 240 494 vector vb = {2, 8, 22, 52, 114, 240, 494}; swap(va, vb); print_vector(va); //2 8 22 52 114 240 494 print_vector(vb); //1 2 3 4 5 6 7 swap_ranges(va.begin() + 2, va.begin() + 5, vb.begin());; print_vector(va); //2 8 1 2 3 240 494 print_vector(vb); //22 52 114 4 5 6 7 fill(vb.begin(), vb.begin() + 3, 0); print_vector(vb); //0 0 0 4 5 6 7 fill_n(vb.begin(), 3, 1); print_vector(vb); //1 1 1 4 5 6 7 generate(vb.begin(), vb.begin() + 3, rand); print_vector(vb); //41 18467 6334 4 5 6 7 generate_n(vb.begin(), 3, rand); print_vector(vb); //26500 19169 15724 4 5 6 7 replace(vb.begin(), vb.end(), 4, 99); print_vector(vb); //26500 19169 15724 99 5 6 7 replace_if(vb.begin(), vb.end(), bind2nd(greater (), 100), -1); print_vector(vb); //-1 -1 -1 99 5 6 7 list lb; print_list(la); //2 8 22 52 114 240 494 reverse(la.begin(), la.end()); print_list(la); //494 240 114 52 22 8 2 list ::iterator posb = la.begin(), pose = la.end(); advance(posb, 1); advance(pose, -1); reverse(posb, pose); print_list(la); //494 8 22 52 114 240 2 reverse_copy(la.begin(), la.end(), back_inserter(lb)); print_list(lb); //2 240 114 52 22 8 494 print_vector(va); //2 8 1 2 3 240 494 print_vector(vb); //-1 -1 -1 99 5 6 7 rotate(va.begin(), va.begin() + 3, va.end()); print_vector(va); //2 3 240 494 2 8 1 rotate_copy(va.begin(), va.begin() + 3, va.end(), vb.begin()); print_vector(va); //2 3 240 494 2 8 1 print_vector(vb); //494 2 8 1 2 3 240 return 0; }



