我有一个 vector ,用户可以输入一些字符串。我想保留用户输入的顺序,但删除任何重复的单词。我唯一能在网上找到的东西是排序和独特的,但由于我无法对 vector 进行排序,所以我被卡住了。预先感谢您的任何帮助。
例如 来自用户的输入 -> hello there dog cat hello cat book
vector should have -> hello there dog cat book
现在我只有...
string s;
vector <string> myVec;
while (cin >> s){
myVec.push_back(s);
}
{code to sort vector}
最佳答案
在您的 vector
旁边, 你可以测试这个词是否已经在 std::set<std::string>
中,如果是则忽略它,否则将其插入两个容器:
while (cin >> s)
if (mySet.insert(s).second) // newly inserted in set?
myVec.push_back(s);
关于c++ - 如何删除 vector 中的重复项(不排序)C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36733552/