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

std::string 使用迭代器,删除\r的时候,没办法删除干净 #12

Closed
qd1308504206 opened this issue Jul 20, 2022 · 4 comments
Closed

Comments

@qd1308504206
Copy link

qd1308504206 commented Jul 20, 2022

	std::string src = "\r\nabc\r\r\r\r\r\r\r\r123456\nABCDEF\r\n\r\n\r\r\r\r";
	std::remove_if(src.begin(), src.end(), [](char c) {
		return c == '\r';
	});

	auto size = src.size();


	//我认为应该是 \nabc123456\nABCDEF\n\n
	//实际却是:src = "\nabc123456\nABCDEF\n\n\nABCDEF\r\n\r\n\r\r\r\r"

最近在学习彭老师的课程,学到了 string其实也有迭代器。然后就找了一个remove_if函数试了一下。

请彭老师帮忙解答一下。这是为什么呢?

我的电脑是win10 64位, 使用的是virtual stdio 2022 preview

image

@qd1308504206
Copy link
Author

必须写成这样子,才能成功。很是疑惑
image

@archibate
Copy link
Collaborator

archibate commented Jul 20, 2022 via email

@archibate
Copy link
Collaborator

archibate commented Jul 20, 2022 via email

@qd1308504206
Copy link
Author

感谢大佬解答

看了教程https://en.cppreference.com/w/cpp/algorithm/remove 之后,才恍然大悟。
image

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

2 participants