
本文共 6959 字,大约阅读时间需要 23 分钟。
10.1 ������
������������������������������������������������������������<algorithm>
���<numeric>
������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������find
���������������������������==������������������������������������������������������������������������<������������ ������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������
���������������������������������������������������find
���������������������������
// ���������val���vec������������������������������������auto result = find(vec.cbegin(), vec.cend(), val);
���������������������result
���������������������������������
10.2 ������������������
���������������������������������������������������������������������������������������������������������������������������������������������������������������accumulate()
��������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������� ������accumulate
������������
// ������vec���������������������������������0int sum = accumulate(vec.cbegin(), vec.cend(), 0);// ������������������������������������������������������������������������string sum = accumulate(vec.cbegin(), vec.cend(), string(""));
���������������������������accumulate
���������������������������������������������������������������������������������������������������������cbegin()
���cend()
������������
���������equal()
���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������==������������������������������������������������������������������������������������������������������������������������������������������������������
������equal()
������������
// ������roster1���roster2���������������������equal(roster1.cbegin(), roster1.cend(), roster2.cbegin());
10.2.1 ������������
������accumulate()
���equal()
������fill()
������������������������������������������������������������������������fill_n()
������������������fill_n()
���������������������������������������������������n��������� ���������������fill()
���
// ���vec���������������������10fill(vec.begin(), vec.end(), 10);
fill_n()
���������
// ������n������������������valfill_n(vec.begin(), n, val);
������������������������������������������������������������������back_inserter()
���������������������������������������������������������������������������������������������������������������������������������������������������������
// ������������������������������������������vecvector vec;auto it = back_inserter(vec);*it = 42; // vec������������������������42// ������fill_n()���back_inserter()������vecfill_n(back_inserter(vec), 10, 0);
������������������copy()
���replace()
���sort()
���unique()
������������������������copy()
���������������������������������������������replace()
������������������������������������������������������������������
// ���vec������������������0������������������42replace(vec.begin(), vec.end(), 0, 42);// ������������������������������������������������������vector tmp = {...vec};replace(vec.begin(), vec.end(), back_inserter(tmp), 0, 42);
10.2.2 ���������������������������
unique()
���������������������������������������������������������������������������������������������������������������������erase()
��������������������������� void elimDups(vector&words) { sort(words.begin(), words.end()); auto end_unique = unique(words.begin(), words.end()); words.erase(end_unique, words.end());}
���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������
10.3 ������������
sort()
������������������������������������������������������������������������������lambda��������������������������������������������� bool isShorter(const string &s1, const string &s2) { return s1.size() < s2.size();}sort(words.begin(), words.end(), isShorter);
���������stable_sort()
������������������������������������������������������
10.3.1 lambda���������
lambda������������������������������������������������������������������������������������������������������������������������������������������������������������������������������
void biggies(vector&words, size_t sz) { elimDups(words); sort(words.begin(), words.end(), isShorter); auto wc = find_if(words.begin(), words.end(), [](const string &a) { return a.size() >= sz; }); for_each(wc, words.end(), [](const string &s) { cout << s << " "; }); cout << endl;}
lambda������������������������������������������������������������������������static���������������
10.3.3 lambda���������������
lambda������������������������������������������auto Modifier = [](const vector &v, int x) { vector res = v; for (auto num : res) { num += x; } return res;};vector vec = {1, 2, 3};auto result = Modifier(vec, 5); // result���������������������
10.3.4 ������������
������������������������������������������������������������������������������������������������begin()
���end()
���������������������������������������������������insert_iterator
��������������������������� ���������ostream
���������������������������������������������������������������
// ������cout���������������vector���������vector nums = {1, 2, 3, 4};auto out_it = ostream_iterator (cout, ", ");copy(nums.begin(), nums.end(), out_it);
10.4 ���������������
���������������������������������������������������������������������������������������������������������10.5 ������������������
������������������������������������������ Predicate/Transformer/Coupon ������������������������������������������������������������������������ Predicate.getChildAtable���Transformer���������������������������������������sort()
��������������������������� template< class RandomAccessRange, class Compare >void sort< Container, Range, Compare, typename Range::elementType >sort(CheckingIterator begin, CheckingIterator end, Compare comp);
10.6 ������������������
������������������string
���vector
������������������������������������������������sort
������������������string
���������������find
��������������������������� ������������������������������������������������������������������C++���������������������������������������������������
发表评论
最新留言
关于作者
