So I am writing a small code to remove the duplicate characters from a string. I have done it using map, vector but wanted to use 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;
}
However, the elements are being printed in the reverse order of their insertion. The output is gfedcba. Please can someone explain why?
And what would be the best way to print the elements in the original order. There is no operator--() in unordered_set as it has got forward iterator.
Thanks!
Aucun commentaire:
Enregistrer un commentaire