huangmingchuan / Cpp_Primer_Answers

《C++ Primer》第五版中文版习题答案

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

10.31 答案不严谨

CCJ623 opened this issue · comments

unique_copy 只能去除相邻的重复元素,如果在输入流中的序列不是有序的,那么unique_copy并不能完全去掉所有重复元素,所以在使用 unique_copy 之前要先排序

下面是我的代码,先全部拷贝到vector中,进行排序,再用unique_copy 拷贝到输出迭代器中

istream_iterator<int> in_iter(cin), eof;
ostream_iterator<int> out_iter(cout, " ");
vector<int> v;
copy(in_iter, eof, back_inserter(v));
sort(v.begin(), v.end());
unique_copy(v.cbegin(), v.cend(), out_iter);
cout << endl;