【C++ Primer | 10】泛型算法

 1 #include<iostream>
 2 #include<algorithm>
 3 #include<vector>
 4 #include<string>
 5 #include<fstream>
 6 using namespace std;
 7 
 8 void elimDups(vector<string> &words)
 9 {
10     sort(words.begin(), words.end());
11     auto unique_end = unique(words.begin(), words.end());
12     words.erase(unique_end, words.end());
13 }
14 
15 void display(vector<string> &words)
16 {
17     for (auto c : words)
18         cout << c << " ";
19     cout << endl;
20 }
21 
22 int main()
23 {
24     ifstream in("test.txt");
25     if (!in)
26     {
27         cout << "打开文件失败" << endl;
28         exit(1);
29     }
30 
31     vector<string> words;
32     string str;
33     while (in >> str)
34         words.push_back(str);
35     elimDups(words);
36     display(words);
37     return 0;
38 }
View Code

输出结果:

 

 定制操作

示例代码:

 1 #include<iostream>
 2 #include<algorithm>
 3 #include<vector>
 4 #include<string>
 5 #include<fstream>
 6 using namespace std;
 7 
 8 void elimDups(vector<string> &words)
 9 {
10     sort(words.begin(), words.end());
11     auto unique_end = unique(words.begin(), words.end());
12     words.erase(unique_end, words.end());
13 }
14 
15 void biggies(vector<string> &words, vector<string>::size_type sz)
16 {
17     elimDups(words);  //将单词按字典排序,删除重复单词
18     stable_sort(words.begin(), words.end(), [](const string &a, const string &b) { return a.size() < b.size(); });
19     auto wc = find_if(words.begin(), words.end(), [sz](const string &a) { return a.size() >= sz; });
20     auto count = words.end() - wc;
21     for_each(wc, words.end(), [](const string &s) { cout << s << " "; });
22     cout << endl;
23 }
24 
25 int main()
26 {
27     ifstream in("test.txt");
28     if (!in)
29     {
30         cout << "打开文件失败" << endl;
31         exit(1);
32     }
33 
34     vector<string> words;
35     string str;
36     while (in >> str)
37         words.push_back(str);
38     auto sz = 5;
39     biggies(words, sz);
40     return 0;
41 }
View Code

输出结果:

 再探迭代器

3. 反向迭代器

 1 #include<iostream>
 2 #include<vector>
 3 #include<iterator>
 4 using namespace std;
 5 
 6 int main()
 7 {
 8     vector<int> vec = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
 9     for (auto r_iter = vec.crbegin(); r_iter != vec.crend(); ++r_iter)
10         cout << *r_iter << " "; 
11     cout << endl;
12     return 0;
13 }

输出结果:

 1 #include <iostream>
 2 #include <deque>
 3 #include <algorithm>
 4 #include <iterator>
 5 using namespace std;
 6 
 7 void print(int elem)
 8 {
 9     cout << elem << ' ';
10 }
11 
12 int main()
13 {
14     deque<int> coll;
15     for (int i = 1; i <= 9; ++i)
16         coll.push_back(i);
17 
18     deque<int>::iterator pos1;
19     pos1 = find(coll.begin(), coll.end(), 2);
20 
21     deque<int>::iterator pos2;
22     pos2 = find(coll.begin(), coll.end(), 7);
23     for_each(pos1, pos2, print);
24     cout << endl;
25 
26     deque<int>::reverse_iterator rpos1(pos1);
27     deque<int>::reverse_iterator rpos2(pos2);
28     for_each(rpos2, rpos1, print);
29     cout << endl;
30     return 0;
31 }

输出结果:

 

【分析】

代码首先在一个deque中插入1到9,然后查找元素值为2和7的位置,分别赋值给迭代器pos1和pos2,然后输出,由于STL中的操作总是左开右闭的区间,即[2,7),所以输出2 3 4 5 6,7不会输出。

接下来将迭代器转换成逆向迭代器,再次输出,对于反向迭代器,由于是反向,所以按逻辑来说它是左开右闭的(这里我尝试了rpos2为iterator.end(),rpos1为iterator.begin(),此时输出全部),即(7,2](事实上还是左闭右开,只不过此时的左和iterator顺序一样)。所以输出6 5 4 3 2,下面的图片解释的很清楚。

原文地址:https://www.cnblogs.com/sunbines/p/9766505.html