我有一个包含以下内容的字符串:
This is a line.
This is another line.
This is yet another line.
This is a line.
[...]
Run Code Online (Sandbox Code Playgroud)
如何从字符串中删除重复的行?
字符串没有线的概念.你必须自己拆分线.一种可能的解决方案是stringstream从字符串构造一个并getline在其上使用.
您可以解析一行std::set<string>或更好的行std::unordered_set<string>(如果c++11是一个选项),您将自动只有一行中存储的每一行的单个实例.该解决方案的缺点是它不会保持线的原始顺序.如果你需要保留它,那么使用一个帮助器组并按下一行中的线std::vector,你把它们放在原始顺序中.