习题10-31
#include <iostream>
#include<algorithm>
#include <vector>
#include<iterator>
using namespace std;
int main()
{
istream_iterator<int> int_in(cin), int_eof;
ostream_iterator<int> out(cout, " ");
vector<int> vec(int_in, int_eof);
sort(vec.begin(), vec.end());
unique_copy(vec.begin(), vec.end(), out);
cout << endl;
}
习题10-32
略,前三章没写;
习题10-33
#include <iostream>
#include<fstream>
#include<algorithm>
#include <vector>
#include<iterator>
using namespace std;
int main()
{
ifstream file1("data1.txt");
ofstream file2("data2.txt"), file3("data3.txt");
istream_iterator<int> int_in(file1), int_eof;
ostream_iterator<int> out_1(file2, " "), out_2(file3, "\n");
vector<int> vec(int_in, int_eof);
for (auto it = vec.begin(); it != vec.end(); ++it)
{
if (*it % 2 != 0)
*out_1++ = *it;
else
*out_2++ = *it;
}
}
习题10-34
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> vec = { 1,2,3,4,5,6,7,8,9 };
for (auto iter = vec.rbegin(); iter != vec.rend(); ++iter)
cout << *iter << " ";
cout << endl;
}
习题10-35
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> vec = { 1,2,3,4,5,6,7,8,9 };
for (auto iter = vec.end(); iter != vec.begin(); --iter)
cout << *(iter-1) << " ";
cout << endl;
}
习题10-36
#include <iostream>
#include <list>
using namespace std;
int main()
{
list<int> lst = { 1,0,2,0,3,0,4};
auto zero = find(lst.rbegin(), lst.rend(), 0);
cout << *zero << endl;
cout << *(--zero) << endl;
cout << *(++zero) << endl;
cout << *(++zero) << endl;
}
习题10-37
#include <iostream>
#include<algorithm>
#include <list>
#include<vector>
using namespace std;
int main()
{
vector<int> vec = { 1,2,3,4,5,6,7,8,9,10};
list<int> lst(5);
copy(vec.rbegin() + 3, vec.rbegin() + 8, lst.begin());
for (auto iter = lst.begin(); iter != lst.end(); ++iter)
cout << *iter << " ";
}
习题10-38
略,看书
需要注意的事随机访问迭代器是可以加整数的,其他的不可以
习题10-39
list:双向迭代器。vector:随机访问迭代器
list迭代器是双向迭代器,不能加整数,vector可以;
习题10-40
copy的三个参数,第一二个数输入迭代器,第三个是输出迭代器;
reverse要求双向迭代器的;
unique要求前向迭代器;