0
因此,我正在编写一个小代码以从字符串中删除重复的字符。我已经使用map,vector完成了它,但是想要使用unordered_set。打印unordered_set的元素
#include <iostream>
#include <unordered_set>
#include <string.h>
using namespace std;
int main() {
char* str = "abbcdeffg";
std::unordered_set<char> ump;
for(int i = 0; i < strlen(str) ; i++)
{
ump.insert(str[i]);
}
for (auto it = ump.begin(); it != ump.end(); ++it)
{
cout << *it;
}
return 0;
}
但是,元素正在以与插入顺序相反的顺序打印。输出是gfedcba。请有人解释为什么?
什么是按原始顺序打印元素的最佳方式。在unordered_set中没有运算符 - (),因为它有前向迭代器。
谢谢!
这是因为它是“** unordered **” – avim 2015-04-02 23:19:21
您可能想要将'strlen()'从循环中拉出。 – Arun 2015-04-02 23:23:04
并停止从字符串文字初始化'char *'。 _years_没有被允许。打开你的警告!!!!!!!! – 2015-04-02 23:51:51