小编use*_*393的帖子

C++:如何检测vector <string>中的重复项并打印一份副本?

我是C++的新手.我想知道如何在向量中找到重复的字符串并打印出该字符串的一个副本.例如,如果我有<"猫","狗","狗","鸟",>它会打印出猫,狗,鸟.我已经对我的向量进行了排序,并使用了adjacent_find函数并迭代了向量(因为我必须找到是否有任何单词重复).我的代码检测到重复,但它只打印出非重复的代码.我想改变它以打印出所有非重复项,也只打印其中一个重复项,因此矢量中的所有字符串都会打印出来.这是我到目前为止的代码:

public: void print(vector<string> in) // print method for printing a vector and it's key
{ 

  sort(in.begin(), in.end()); // sort the vector alphabetically first

  vector<string>::iterator it; 

      for( it = in.begin(); it != in.end(); it++ ) // iterate through it


             if(adjacent_find(in.begin(), in.end()) == in.end()) // don't print duplicates


             cout << *it<<endl; // and print out each string in the vector
}
Run Code Online (Sandbox Code Playgroud)

c++ duplicates stdvector

3
推荐指数
1
解决办法
2140
查看次数

标签 统计

c++ ×1

duplicates ×1

stdvector ×1