Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

练习10.31答案有误 #85

Open
JPLAY0 opened this issue Jan 23, 2022 · 0 comments
Open

练习10.31答案有误 #85

JPLAY0 opened this issue Jan 23, 2022 · 0 comments

Comments

@JPLAY0
Copy link

JPLAY0 commented Jan 23, 2022

题目要求:

修改前一题的程序,使其只打印不重复的元素。你的程序应该使用 unique_copy。

给出的答案:

int main()
{
	vector<int> v;
	istream_iterator<int> int_it(cin), int_eof;

	unique_copy(int_it, int_eof, back_inserter(v));
	sort(v.begin(), v.end());

	copy(v.begin(), v.end(), ostream_iterator<int>(cout, " "));
	cout << endl;
	return 0;
}```
由于输入的整数并不一定是有序的,所以这里采用unique_copy对vector进行插入不能达到去重效果,而应该在vector排完序后再用unique_copy进行输出,就能达到题目要求。
@JPLAY0 JPLAY0 mentioned this issue Jan 23, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant