本章大意
详细分析了各类迭代器。
细节摘录
1. 一些别的类型的迭代器可以当作“容器”或者“函数”看待
2.
课后习题
1. 差别仅在于插入元素位置的不同
2. 不明白题意
3.
4.
习题较多 暂留
详细分析了各类迭代器。
细节摘录
1. 一些别的类型的迭代器可以当作“容器”或者“函数”看待
2.
课后习题
1. 差别仅在于插入元素位置的不同
2. 不明白题意
3.
#include <iostream>
#include <string>
#include <list>
#include <vector>
#include <iterator>
#include <algorithm>
using namespace std;
int main()
{
list<string> l;
string s;
while (cin >> s) {
l.push_back(s);
}
cout << "录入结果" << endl;
for (list<string>::iterator it = l.begin(); it!=l.end(); it++) {
cout << *it << " ";
}
cout << endl << "....处理中.." << endl;
//sort(l.begin(), l.end()); // 问题出在这句,sort算法不能用在list容器之上面? 那么有什么办法呢?(除了复制到Vector再调用sort这种方法)
l.sort();
vector<string> v;
unique_copy(l.begin(), l.end(), inserter(v, v.begin())); // 使用之前应当进行排序,因为只能删除相邻的元素。
cout << "处理完毕" << endl;
for (vector<string>::iterator it = v.begin(); it!=v.end(); it++) {
cout << *it << " ";
}
cout << endl;
return 0;
}
4.
#include <iostream>
#include <string>
#include <fstream>
#include <algorithm>
#include <iterator>
using namespace std;
int main()
{
ifstream in;
string filename;
cout << "请输入要打开的文件名" << endl;
cin >> filename;
in.open(filename.c_str());
if (!in) {
cout << "打开文件失败" << endl;
}
else
cout << "文件打开成功" << endl;
istream_iterator<int> cin_it(in);
istream_iterator<int> end;
ostream_iterator<int> out(cout, " ");
copy(cin_it, end, out);
cout << endl;
return 0;
}
习题较多 暂留