C++primer 10.2.3节练习
练习10.9
1 #include<iostream> 2 #include<string> 3 #include <iostream> 4 #include <string> 5 #include <vector> 6 #include <algorithm> 7 #include <list> 8 using namespace std; 9 10 void elmDups(vector<string> &vec); 11 12 int main() 13 { 14 vector<string> str; 15 string str1; 16 while (cin >> str1) 17 { 18 str.push_back(str1); 19 } 20 elmDups(str); 21 for (auto c : str) 22 cout << c << endl; 23 system("pause"); 24 return 0; 25 } 26 27 void elmDups(vector<string>& vec) 28 { 29 sort(vec.begin(), vec.end()); 30 auto it = unique(vec.begin(), vec.end()); 31 vec.erase(it, vec.end()); 32 }
练习10.10
算法基于迭代器来操作以实现泛型,而当需要在容器中添加或删除元素时, 不知道容器和容器元素的具体类型,就不可能知道需要增加或减少多少空间,就无法实现容器添加或删除元素的目的。 添加或删除容器元素的操作是与类型强相关的,而泛型的算法做不到这点
浙公网安备 33010602011771号