C++ Primer 读书笔记 - 第十一章
1. find()函数
2. accumulate()函数
3. find_first_of()函数的两个range的类型可以不同,但是两个range内部的类型必须相同。
4. fill()函数,使用时必须保证空间是足够的。
fill_n(back_inserter(vec), 10, 0);//appends 10 elements to vec
5. replace()函数改变容器内部,replace_copy()函数不改变容器内部,而是自己新拷贝一份。
6. sort()函数,unique()函数,unique_copy()函数,stable_sort()函数,count_if()函数
7. back_inserter, front_inserter, inserter
front_inserter requires push_front, so using front_inserter on a vector, or other container that does not have push_front, is an error.
用front_inserter与用inserter(container, container.begin())效果不一样
8. istream_iterator<T>与ostream_iterator<T>
通过输入创建vector:
istream_iterator<int> in_iter(cin); istream_iterator<int> eof; vector<int> vec(in_ter, eof);
9. 学会用string(it1, it2)来新创建一个string
10. reverse_iterator的base()函数
11. Algorithms require the iterators that denote a range to have exactly the same type.
12. input iterators ->istream_iterator
output iterators ->ostream_iterator
forward iterators ->replace()的参数
bidirectional iterators ->reverse()的参数
random-access iterators ->sort()的参数,vector,deque,string的iterators
13. When dealing with algorithms, it is best to think of the iterators on associative containers as if they were input iterators that support decrement, not as full bidirectional iterators.
14. list用其自身的成员函数,sort, unique, remove, reverse, merge, splice.
#include <iostream> #include <algorithm> #include <numeric> #include <vector> #include <map> using namespace std; int main() { vector<string> vec; vec.push_back("jkl"); vec.push_back("abc"); vec.push_back("def"); vec.push_back("abc"); vec.push_back("ghi"); string sum = accumulate(vec.begin(), vec.end(), string(""));//the third parameter cannot be "", which is const char * cout << sum << endl; sort(vec.begin(), vec.end()); vector<string>::iterator end_unique = unique(vec.begin(), vec.end()); for (vector<string>::iterator it = vec.begin(); it != end_unique; ++it) cout << *it << endl; string s1("abc"); const char *s2 = "abc"; if (s1 == s2) cout << true << endl; map<string, int> m; m.insert(make_pair("mbc", 1)); m.insert(make_pair("edf", 1)); m.insert(make_pair("ahe", 1)); m.insert(make_pair("dwi", 1)); m.insert(make_pair("waa", 1)); //reverse(m.begin(), m.end()); const vector<int> vc(10); return 0; }
可以转载, 但必须以超链接形式标明文章原始出处和作者信息及版权声明