第十章 泛型算法
10.1
1 #include <iostream> 2 #include <fstream> 3 #include <iterator> 4 #include <vector> 5 #include <string> 6 #include <deque> 7 #include <list> 8 #include <forward_list> 9 #include <stack> 10 #include <queue> 11 #include <algorithm> 12 13 using namespace std; 14 15 int main() 16 { 17 vector<int> vec; 18 int i; 19 while (cin >> i) { 20 vec.push_back(i); 21 } 22 cout << count(vec.begin(), vec.end(), 2) << endl; 23 return 0; 24 }
10.2
1 #include <iostream> 2 #include <fstream> 3 #include <iterator> 4 #include <vector> 5 #include <string> 6 #include <deque> 7 #include <list> 8 #include <forward_list> 9 #include <stack> 10 #include <queue> 11 #include <algorithm> 12 13 using namespace std; 14 15 int main() 16 { 17 vector<string> vec; 18 string s; 19 while (cin >> s) { 20 vec.push_back(s); 21 } 22 cout << count(vec.begin(), vec.end(), "kzw") << endl; 23 return 0; 24 }
10.3
vector<int> vec = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; auto sum = accumulate(vec.cbegin(), vec.cend(), 0);
10.4
错误:accumulate的第三个参数的类型决定了函数返回值的类型,即序列中元素的类型将被转换为第三个参数的类型进行求和,这样得到的结果与初衷相悖。
10.5
C风格字符串不支持“==”,故会出现编译错误。
没有影响,逐个元素比较(字符支持“==”)。
char roster1[] = {"love"}; char roster2[] = {"lovea"}; bool bl = equal(roster1, roster1 + 4, roster2);
10.6
int a[] = {1, 2, 3, 4, 5, 6, 7, 8, 9}; fill_n(begin(a), end(a) - begin(a), 0);
10.7
(a):错误,程序试图将lst中的元素拷贝到vec这个空向量中
(b):正确错误,reverse是改变容器容量的,并没有改变其大小,用resize()
10.8
算法永远不会改变容器的大小。算法并不直接操作容器,它们只是运行于迭代器之上,执行迭代器的操作。对于back_iterator,算法仅仅操作它,而向容器中添加元素则是back_iterator自身完成的,与算法无关。
10.9
1 #include <iostream> 2 #include <fstream> 3 #include <iterator> 4 #include <vector> 5 #include <string> 6 #include <deque> 7 #include <list> 8 #include <forward_list> 9 #include <stack> 10 #include <queue> 11 #include <algorithm> 12 13 using namespace std; 14 15 void print(vector<string> &words) 16 { 17 for (auto &i : words) { 18 cout << i << " "; 19 } 20 cout << endl; 21 } 22 23 void elimDups(vector<string> &words) 24 { 25 sort(words.begin(), words.end()); 26 print(words); 27 auto end_unique = unique(words.begin(), words.end()); 28 print(words); 29 words.erase(end_unique, words.end()); 30 print(words); 31 } 32 33 int main() 34 { 35 vector<string> vec = {"the", "quick", "red", "fox", "jumps", "over", "the", "slow", "red", "turtle"}; 36 print(vec); 37 elimDups(vec); 38 return 0; 39 }
10.10
因为算法只操作迭代器,而不操作容器。
10.11
1 #include <iostream> 2 #include <fstream> 3 #include <iterator> 4 #include <vector> 5 #include <string> 6 #include <deque> 7 #include <list> 8 #include <forward_list> 9 #include <stack> 10 #include <queue> 11 #include <algorithm> 12 13 using namespace std; 14 15 void print(vector<string> &words) 16 { 17 for (auto &i : words) { 18 cout << i << " "; 19 } 20 cout << endl; 21 } 22 23 void elimDups(vector<string> &words) 24 { 25 sort(words.begin(), words.end()); 26 auto end_unique = unique(words.begin(), words.end()); 27 words.erase(end_unique, words.end()); 28 } 29 30 bool isShorter(const string &s1, const string &s2) 31 { 32 return s1.size() < s2.size(); 33 } 34 35 int main() 36 { 37 vector<string> vec = {"the", "quick", "red", "fox", "jumps", "over", "the", "slow", "red", "turtle"}; 38 elimDups(vec); 39 print(vec); 40 stable_sort(vec.begin(), vec.end(), isShorter); 41 print(vec); 42 return 0; 43 }
10.12
1 #include <iostream> 2 #include <vector> 3 #include <string> 4 #include <algorithm> 5 6 using namespace std; 7 8 struct Sales_data { 9 string bookNo; //书的ISBN 10 unsigned units_sold = 0; //售出的本数 11 double revenue = 0.0; //销售额 12 string isbn() { return bookNo; } 13 }; 14 15 istream &read(istream &is, Sales_data &item) 16 { 17 is >> item.bookNo >> item.units_sold >> item.revenue; 18 return is; 19 } 20 21 ostream &print(ostream &os, const Sales_data &item) 22 { 23 os << item.bookNo << " " << item.units_sold << " " << item.revenue << endl; 24 return os; 25 } 26 27 bool compareIsbn(Sales_data sa1, Sales_data sa2) 28 { 29 return sa1.isbn() < sa2.isbn(); 30 } 31 32 int main() 33 { 34 vector<Sales_data> vec; 35 Sales_data book; 36 while (read(cin, book)) { 37 vec.push_back(book); 38 } 39 sort(vec.begin(), vec.end(), compareIsbn); 40 for (auto &i : vec) 41 print(cout, i); 42 return 0; 43 }
10.13
1 #include <iostream> 2 #include <fstream> 3 #include <iterator> 4 #include <vector> 5 #include <string> 6 #include <deque> 7 #include <list> 8 #include <forward_list> 9 #include <stack> 10 #include <queue> 11 #include <algorithm> 12 13 using namespace std; 14 15 void print(vector<string> &words) 16 { 17 for (auto &i : words) { 18 cout << i << " "; 19 } 20 cout << endl; 21 } 22 23 bool cmp(const string &s) 24 { 25 return s.size() >= 5; 26 } 27 28 int main() 29 { 30 vector<string> vec = {"the", "quick", "red", "fox", "jumps", "over", "the", "slow", "red", "turtle"}; 31 partition(vec.begin(), vec.end(), cmp); //会使让谓词为真的值排在容器的前半部分 32 print(vec); 33 return 0; 34 }
10.14
[](int a, int b) { return a + b; };
10.15
int a; auto f = [a](int b) { return a + b; };
10.16
1 #include <iostream> 2 #include <fstream> 3 #include <iterator> 4 #include <vector> 5 #include <string> 6 #include <deque> 7 #include <list> 8 #include <forward_list> 9 #include <stack> 10 #include <queue> 11 #include <algorithm> 12 13 using namespace std; 14 15 void print(vector<string> &words) 16 { 17 for (auto &i : words) { 18 cout << i << " "; 19 } 20 cout << endl; 21 } 22 23 void elimDups(vector<string> &words) 24 { 25 sort(words.begin(), words.end()); 26 print(words); 27 auto end_unique = unique(words.begin(), words.end()); 28 print(words); 29 words.erase(end_unique, words.end()); 30 } 31 32 string make_plural(size_t cnt, const string &s1, const string &ending) 33 { 34 return (cnt > 1 ? s1 + ending : s1); 35 } 36 37 38 bool cmp(const string &s) 39 { 40 return s.size() >= 5; 41 } 42 43 void biggies(vector<string> &words, vector<string>::size_type len) 44 { 45 elimDups(words); 46 print(words); 47 stable_sort(words.begin(), words.end(), [](const string &s1, const string &s2) 48 { return s1.size() < s2.size(); }); 49 print(words); 50 auto wc = find_if(words.begin(), words.end(), [len](const string &s) 51 { return s.size() >= len; }); 52 auto count = words.end() - wc; 53 cout << "There are " << count << " " << make_plural(count, "word", "s") 54 << "'s length no shorter than " << len << endl; 55 for_each(wc, words.end(), [](const string &s) { cout << s << " "; }); 56 cout << endl; 57 } 58 59 int main() 60 { 61 vector<string> vec = {"the", "quick", "red", "fox", "jumps", "over", "the", "slow", "red", "turtle"}; 62 vector<string>::size_type len; 63 cin >> len; 64 biggies(vec, len); 65 66 return 0; 67 }
10.17
sort(vec.begin(), vec.end(), [](Sales_data sa1, Sales_data sa2) {return sa1.isbn() < sa2.isbn();});
10.18
//让长度小于len的排在前半部分 auto wc = partition(words.begin(), words.end(), [len](const string &s) { return s.size() < len; });
10.19
auto wc = stable_partition(words.begin(), words.end(), [len](const string &s) { return s.size() < len; });
10.20
1 #include <iostream> 2 #include <fstream> 3 #include <iterator> 4 #include <vector> 5 #include <string> 6 #include <deque> 7 #include <list> 8 #include <forward_list> 9 #include <stack> 10 #include <queue> 11 #include <algorithm> 12 13 using namespace std; 14 15 string make_plural(size_t cnt, const string &s1, const string &ending) 16 { 17 return (cnt > 1 ? s1 + ending : s1); 18 } 19 20 void biggies(vector<string> &words, vector<string>::size_type len) 21 { 22 auto count = count_if(words.begin(), words.end(), [=](const string &s) 23 { return s.size() > 6; }); 24 cout << "There are " << count << " " << make_plural(count, "word", "s") 25 << "'s length longer than " << len << endl; 26 } 27 28 int main() 29 { 30 vector<string> vec = {"jensen", "clearlove", "scout", "iboy", "justin", "jackeylove", "the", "slow", "red", "turtle"}; 31 vector<string>::size_type len; 32 biggies(vec, 6); 33 34 return 0; 35 }
10.21
1 #include <iostream> 2 #include <fstream> 3 #include <iterator> 4 #include <vector> 5 #include <string> 6 #include <deque> 7 #include <list> 8 #include <forward_list> 9 #include <stack> 10 #include <queue> 11 #include <algorithm> 12 13 using namespace std; 14 15 int main() 16 { 17 int a = 3; 18 while (a) { 19 auto f = [&a]() -> bool { --a; return a > 0 ? false : true; }; 20 auto bl = f(); 21 cout << a << (bl == true ? " = 0 " : " > 0 ") << endl; 22 } 23 return 0; 24 }
10.22
1 #include <iostream> 2 #include <fstream> 3 #include <iterator> 4 #include <vector> 5 #include <string> 6 #include <deque> 7 #include <list> 8 #include <forward_list> 9 #include <stack> 10 #include <queue> 11 #include <algorithm> 12 #include <functional> 13 14 using namespace std; 15 using namespace std::placeholders; 16 17 string make_plural(size_t cnt, const string &s1, const string &ending) 18 { 19 return (cnt > 1 ? s1 + ending : s1); 20 } 21 22 bool cmp(const string &s, int len) 23 { 24 return s.size() <= len; 25 } 26 27 void biggies(vector<string> &words, vector<string>::size_type len) 28 { 29 auto count = count_if(words.begin(), words.end(), bind(cmp, _1, 6)); 30 cout << "There are " << count << " " << make_plural(count, "word", "s") 31 << "'s length no longer than " << len << endl; 32 } 33 34 int main() 35 { 36 vector<string> vec = {"jensen", "clearlove", "scout", "iboy", "justin", "jackeylove", "rookie", "doinb", "peanut", "martin"}; 37 vector<string>::size_type len; 38 biggies(vec, 6); 39 return 0; 40 }
10.23
根据算法接受几元谓词,bind就有几个“占位符”,而参数数量则是和其接受的可调用参数一样多。
10.24
1 #include <iostream> 2 #include <fstream> 3 #include <iterator> 4 #include <vector> 5 #include <string> 6 #include <deque> 7 #include <list> 8 #include <forward_list> 9 #include <stack> 10 #include <queue> 11 #include <algorithm> 12 #include <functional> 13 14 using namespace std; 15 using namespace std::placeholders; 16 17 bool check_size(int a, int len) 18 { 19 return a > len; 20 } 21 22 int main() 23 { 24 vector<int> vec = {2, 5, 3, 7, 4, 10}; 25 string s = {"peanut"}; 26 auto it = find_if(vec.begin(), vec.end(), bind(check_size, _1, s.size())); 27 cout << *it << endl; 28 return 0; 29 }
10.25
1 #include <iostream> 2 #include <fstream> 3 #include <iterator> 4 #include <vector> 5 #include <string> 6 #include <deque> 7 #include <list> 8 #include <forward_list> 9 #include <stack> 10 #include <queue> 11 #include <algorithm> 12 #include <functional> 13 14 using namespace std; 15 using namespace std::placeholders; 16 17 string make_plural(size_t cnt, const string &s1, const string &ending) 18 { 19 return (cnt > 1 ? s1 + ending : s1); 20 } 21 22 bool cmp(const string &s, int len) 23 { 24 return s.size() >= len; 25 } 26 27 void biggies(vector<string> &words, vector<string>::size_type len) 28 { 29 //使谓词为真的元素排在前面 30 auto wc = partition(words.begin(), words.end(), bind(cmp, _1, len)); 31 auto count = wc - words.begin(); 32 cout << "There are " << count << " " << make_plural(count, "word", "s") 33 << "'s length no shorter than " << len << endl; 34 } 35 36 int main() 37 { 38 vector<string> vec = {"the", "quick", "red", "fox", "jumps", "over", "the", "slow", "red", "turtle"}; 39 vector<string>::size_type len; 40 cin >> len; 41 biggies(vec, len); 42 return 0; 43 }
10.26
back_inserter:每个新插入的元素都成为容器的尾元素
front_inserter:每个新插入的元素都成为容器的首元素
inserter:每个新插入的元素都被插在传给 inserter 的迭代器所指向的元素之前
10.27
1 #include <iostream> 2 #include <fstream> 3 #include <iterator> 4 #include <vector> 5 #include <string> 6 #include <deque> 7 #include <list> 8 #include <forward_list> 9 #include <stack> 10 #include <queue> 11 #include <algorithm> 12 #include <functional> 13 14 using namespace std; 15 using namespace std::placeholders; 16 17 int main() 18 { 19 vector<int> vec = {1, 2, 3, 3, 4, 3, 5, 7, 9}; 20 list<int> lst; 21 unique_copy(vec.begin(), vec.end(), back_inserter(lst)); 22 for (auto &i : lst) 23 cout << i << " "; 24 cout << endl; 25 return 0; 26 }
10.28
1 #include <iostream> 2 #include <fstream> 3 #include <iterator> 4 #include <vector> 5 #include <string> 6 #include <deque> 7 #include <list> 8 #include <forward_list> 9 #include <stack> 10 #include <queue> 11 #include <algorithm> 12 #include <functional> 13 14 using namespace std; 15 using namespace std::placeholders; 16 17 void print(deque<int> &deq) 18 { 19 for (auto &i : deq) { 20 cout << i << " "; 21 } 22 cout << endl; 23 } 24 25 int main() 26 { 27 vector<int> vec = {1, 2, 3, 4, 5, 6, 7, 8, 9}; 28 deque<int> deq2, deq3, deq4; //vector不支持front_inserter 29 copy(vec.begin(), vec.end(), inserter(deq2, deq2.begin())); 30 print(deq2); 31 copy(vec.begin(), vec.end(), back_inserter(deq3)); 32 print(deq3); 33 copy(vec.begin(), vec.end(), front_inserter(deq4)); 34 print(deq4); 35 return 0; 36 }
10.29
题意版:
1 #include <iostream> 2 #include <fstream> 3 #include <iterator> 4 #include <vector> 5 #include <string> 6 #include <deque> 7 #include <list> 8 #include <forward_list> 9 #include <stack> 10 #include <queue> 11 #include <algorithm> 12 #include <functional> 13 14 using namespace std; 15 using namespace std::placeholders; 16 17 void print(vector<string> &vec) 18 { 19 for (auto &i : vec) { 20 cout << i << " "; 21 } 22 cout << endl; 23 } 24 25 int main() 26 { 27 vector<string> vec; 28 ifstream in("word.txt"); 29 istream_iterator<string> it(in), eof; 30 while(it != eof) { 31 vec.push_back(*it++); 32 } 33 print(vec); 34 return 0; 35 }
进阶版:
1 #include <iostream> 2 #include <fstream> 3 #include <iterator> 4 #include <vector> 5 #include <string> 6 #include <deque> 7 #include <list> 8 #include <forward_list> 9 #include <stack> 10 #include <queue> 11 #include <algorithm> 12 #include <functional> 13 14 using namespace std; 15 using namespace std::placeholders; 16 17 void print(vector<string> &vec) 18 { 19 ostream_iterator<string> it(cout, " "); 20 for (auto &i : vec) { 21 *it++ = i; 22 } 23 cout << endl; 24 } 25 26 int main() 27 { 28 vector<string> vec; 29 ifstream in("word.txt"); 30 istream_iterator<string> it(in), eof; 31 while(it != eof) { 32 vec.push_back(*it++); 33 } 34 print(vec); 35 return 0; 36 }
完美版:
1 #include <iostream> 2 #include <fstream> 3 #include <iterator> 4 #include <vector> 5 #include <string> 6 #include <deque> 7 #include <list> 8 #include <forward_list> 9 #include <stack> 10 #include <queue> 11 #include <algorithm> 12 #include <functional> 13 14 using namespace std; 15 using namespace std::placeholders; 16 17 int main() 18 { 19 vector<string> vec; 20 ifstream in("word.txt"); 21 istream_iterator<string> it(in), eof; 22 ostream_iterator<string> it2(cout, " "); 23 while(it != eof) { 24 vec.push_back(*it++); 25 } 26 copy(vec.begin(), vec.end(), it2); 27 return 0; 28 }
超凡版:
1 #include <iostream> 2 #include <fstream> 3 #include <iterator> 4 #include <vector> 5 #include <string> 6 #include <deque> 7 #include <list> 8 #include <forward_list> 9 #include <stack> 10 #include <queue> 11 #include <algorithm> 12 #include <functional> 13 14 using namespace std; 15 using namespace std::placeholders; 16 17 int main() 18 { 19 vector<string> vec; 20 ifstream in("word.txt"); 21 istream_iterator<string> it(in), eof; 22 ostream_iterator<string> it2(cout, " "); 23 copy(it, eof, back_inserter(vec)); 24 copy(vec.begin(), vec.end(), it2); 25 return 0; 26 }
10.30
1 #include <iostream> 2 #include <fstream> 3 #include <iterator> 4 #include <vector> 5 #include <string> 6 #include <deque> 7 #include <list> 8 #include <forward_list> 9 #include <stack> 10 #include <queue> 11 #include <algorithm> 12 #include <functional> 13 14 using namespace std; 15 using namespace std::placeholders; 16 17 int main() 18 { 19 vector<int> vec; 20 istream_iterator<int> it(cin), eof; 21 ostream_iterator<int> it2(cout, " "); 22 while (it != eof) { 23 vec.push_back(*it++); 24 } 25 sort(vec.begin(), vec.end(), [](int a, int b) 26 { return a < b; }); 27 copy(vec.begin(), vec.end(), it2); 28 cout << endl; 29 return 0; 30 }
10.31
unique_copy(vec.begin(), vec.end(), it2);
10.32(未完成)
1 #include <iostream> 2 #include <fstream> 3 #include <iterator> 4 #include <vector> 5 #include <string> 6 #include <deque> 7 #include <list> 8 #include <forward_list> 9 #include <stack> 10 #include <queue> 11 #include <algorithm> 12 #include <functional> 13 14 using namespace std; 15 using namespace std::placeholders; 16 17 class Sales_date{ 18 friend istream& read(istream &in, Sales_date &book); 19 friend ostream& print(ostream &out, Sales_date &book); 20 string bookNo; 21 unsigned units_sold = 0; 22 double revenue = 0.0; 23 public: 24 Sales_date() {} 25 Sales_date(string isb, unsigned num, double mey): bookNo(isb), units_sold(num), revenue(mey) {} 26 Sales_date(string isb): bookNo(isb) {} 27 string isbn() { return bookNo; } 28 Sales_date& combine(const Sales_date &lsn); 29 }; 30 31 Sales_date& Sales_date::combine(const Sales_date &book) 32 { 33 units_sold += book.units_sold; 34 revenue += book.revenue; 35 return *this; 36 } 37 38 istream& read(istream &in, Sales_date &book) 39 { 40 in >> book.bookNo >> book.units_sold >> book.revenue; 41 return in; 42 } 43 44 ostream& print(ostream &out, Sales_date &book) 45 { 46 out << book.bookNo << book.units_sold << book.revenue; 47 return out; 48 } 49 50 int main() 51 { 52 vector<Sales_date> vec; 53 Sales_date total; 54 istream_iterator<Sales_date> it(cin), eof; 55 return 0; 56 }
参考:
1 #include "Sales_item.h" 2 #include <iostream> 3 #include <vector> 4 #include <algorithm> 5 #include <iterator> 6 #include <numeric> //accumulate 7 int main() 8 { 9 using namespace std; 10 istream_iterator<Sales_item> sit(std::cin),eof; 11 vector<Sales_item> vs; 12 copy(sit, eof, back_inserter(vs)); 13 sort(vs.begin(), vs.end(), [](const Sales_item &s1, const Sales_item &s2){return s1.isbn < s2.isbn; }); 14 for (auto beg = vs.begin(), tmp = beg; beg != vs.end(); beg = tmp) //beg和tmp用于记录相同的元素的范围 15 { 16 tmp = find_if(beg, vs.end(), [beg](const Sales_item &s){return s.isbn != beg->isbn; }); //找到范围,(beg,tmp]之间的元素同名的(isbn值) 17 accumulate(beg, tmp, Sales_item(beg->isbn)); //初始值为0的Sales_item对象 18 } 19 for (auto &x : vs) 20 cout << x << endl; 21 system("pause"); 22 return 0; 23 }
10.33
1 #include <iostream> 2 #include <fstream> 3 #include <iterator> 4 #include <vector> 5 #include <string> 6 #include <deque> 7 #include <list> 8 #include <forward_list> 9 #include <stack> 10 #include <queue> 11 #include <algorithm> 12 #include <functional> 13 14 using namespace std; 15 using namespace std::placeholders; 16 17 int main(int argc, char *argv[]) 18 { 19 ifstream in(argv[1]); 20 ofstream out1(argv[2]), out2(argv[3]); 21 istream_iterator<int> it(in), eof; 22 ostream_iterator<int> onput1(out1, " "), onput2(out2, " "); 23 while (it != eof) { 24 if (*it % 2) 25 onput1 = *it; 26 else 27 onput2 = *it; 28 ++it; 29 } 30 return 0; 31 }
10.34
1 #include <iostream> 2 #include <fstream> 3 #include <iterator> 4 #include <vector> 5 #include <string> 6 #include <deque> 7 #include <list> 8 #include <forward_list> 9 #include <stack> 10 #include <queue> 11 #include <algorithm> 12 #include <functional> 13 14 using namespace std; 15 using namespace std::placeholders; 16 17 int main() 18 { 19 vector<string> vec = {"clearlove", "iboy", "martin"}; 20 for (auto it = vec.crbegin(); it != vec.crend(); ++it) { 21 cout << *it << " "; 22 } 23 cout << endl; 24 return 0; 25 }
10.35
1 #include <iostream> 2 #include <fstream> 3 #include <iterator> 4 #include <vector> 5 #include <string> 6 #include <deque> 7 #include <list> 8 #include <forward_list> 9 #include <stack> 10 #include <queue> 11 #include <algorithm> 12 #include <functional> 13 14 using namespace std; 15 using namespace std::placeholders; 16 17 int main() 18 { 19 vector<string> vec = {"clearlove", "iboy", "martin"}; 20 for (auto it = vec.cend() - 1; it != vec.cbegin() - 1; --it) { 21 cout << *it << " "; 22 } 23 cout << endl; 24 return 0; 25 }
10.36
list<int> lst = {1, 2, 3, 0, 4, 5, 0, 6, 7, 8, 0, 9}; auto it = find(lst.crbegin(), lst.crend(), 0);
10.37
vector<int> vec = {1, 2, 3, 0, 4, 5, 0, 6, 7, 8}; list<int> lst; copy(vec.rbegin() + 3, vec.rend() - 2, back_inserter(lst));
10.38
迭代器类别 | 支持的操作 | 说明 |
输入迭代器 | ==/!=、++、*it | 只用于顺序访问,不能保证其状态可以保存下来并用来访问元素,单遍扫描算法 |
输出迭代器 | ++、* | 只写而不读元素,用作目的位置的迭代器,单遍扫描算法 |
前向迭代器 | ==/!=、++、*it | 只能在序列中沿一个方向移动,可保存其状态(可多次读写同一元素),多遍扫描 |
双向迭代器 | ==/!=、++/--、*it | 支持正向/反向读写序列中的元素 |
随机访问迭代器 | 同上、>/<=、+/-=、[]、- | 支持双向迭代器的所有功能,可在常量时间内访问序列中任意元素,it[n]与*(it[n])等价 |
10.39
list上的迭代器:属于双向迭代器
vector上的迭代器:属于随机访问迭代器
10.40
copy:前两个参数是输入迭代器,第三个参数是输出迭代器
reverse:需要双向迭代器
unique:需要前向迭代器
10.41
replace:将迭代器beg到end所表示的范围中的old_val替换为new_val
replace_if:将迭代器beg到end所表示的范围中的令谓词pred为真的元素替换为new_val
replace_copy:将迭代器beg到end所表示的范围中的old_val替换为new_val,将形成的新序列添加到dest(原范围中的数的值不变)
replace_copy_if:将迭代器beg到end所表示的范围中的令谓词pred为真的元素替换为new_val,将形成的新序列添加到dest(原范围中的数的值不变)
附上代码:
vector<int> vec = {1, 2, 3, 0, 4, 5, 0, 6, 7, 8}; vector<int> vec2; vec2.resize(10); //必不可少,不然vec2是空容器 replace(vec.begin(), vec.end(), 8, 5); replace_if(vec.begin(), vec.end(), [](int a) { return a > 7; }, 5); replace_copy(vec.begin(), vec.end(), vec2.begin(), 0, 1); replace_copy_if(vec.begin(), vec.end(), vec2.begin(), [](int a) { return a > 7; }, 5);
10.42
1 #include <iostream> 2 #include <fstream> 3 #include <iterator> 4 #include <vector> 5 #include <string> 6 #include <deque> 7 #include <list> 8 #include <forward_list> 9 #include <stack> 10 #include <queue> 11 #include <algorithm> 12 #include <functional> 13 14 using namespace std; 15 using namespace std::placeholders; 16 17 void elimDups(list<string> &lst) 18 { 19 lst.sort(); 20 lst.unique(); 21 } 22 23 int main() 24 { 25 list<string> lst = {"the", "quick", "red", "fox", "jumps", "over", "the", "slow", "red", "turtle"};\ 26 elimDups(lst); 27 for (auto &i : lst) cout << i << " "; 28 cout << endl; 29 return 0; 30 }