设置没有删除重复项
Set is not removing the duplicates
我很难将单词文件放入集合中。我可以读取文件,并且单词进入集合,但该集合不会丢弃重复的单词。这是我认为正在引起问题的代码段。
using namespace std;
while(readText >> line){
set<string> wordSet;
wordSet.insert(line);
for (std::set<std::string>::iterator i = wordSet.begin(); i != wordSet.end(); i++)
{
cout << *i << " ";
}
}
示例文件是 1 2 2 3 4 5 5
,输出完全相同
如注释中所述,您无法正确使用std::set
。您需要移动它,而for
循环在while
循环之外:
using namespace std;
set<string> wordSet;
while(readText >> line) {
wordSet.insert(line);
}
for (set<string>::iterator i = wordSet.begin(); i != wordSet.end(); i++) {
cout << *i << " ";
}
相关文章:
- 如何在自删除后将对象设置为nullptr
- 将指针设置为"nullptr"并不能防止双重删除?
- 复制后删除原始数组指针将前 3 个字节设置为 0
- 删除对象与将指针设置为 null
- 如何将项目属性设置为删除警告MSB8004:中间目录不会以落后的斜线结束
- 设置位置时QT删除对象()
- 删除地图并从标题中设置
- 在设置 if-else 语句以删除特定货币并保持更新时遇到问题
- QlistWidget删除选定的项目及其设置QT
- 优雅的解决方案,通过精确设置删除尾随 0
- 如何使用put_time()设置日期格式以删除前面的空格和零
- 设置没有删除重复项
- 删除指针并将指针设置为null之间有什么区别
- 设置/删除窗口子类时,正确传递/删除数据
- 当我要删除它时,指针甚至无法设置为null
- 当通过其中一个指针删除对象时,C++将所有指针设置为null
- 如何设置CMAKE_INTDIR或CMAKE_CFG_INTDIR来删除项目配置
- std::在C++中设置删除程序不起作用
- 当我替换对象时,我需要删除我在类成员中设置的对象吗
- (通常)最快的方法来删除设置的交叉