0

There is a textfile containing names:

Jack Sparrow
David Linch
Jack Sparrow
Umble Gamble
Lee Sion
Truman Bruman
Lee Sion

I have tried to remove duplicates from it using <set>, but the problem is that it sorts automatically. I want to do it without being sorted.

Here is my code:

int main()
{
    string name;
    set <string> nameList;

    char fileName[50];

    cout << "Please Enter file name: ";
    cin >> fileName;

    ifstream read(fileName);
    if(!read){
        cout << "File not found!" << endl;
        return -1;
    }


    while(read.good()){

         if(!read.eof()){

              copy(istream_iterator<string>(read),istream_iterator<string>(),inserter(nameList,nameList.end()));
    }
    }
    read.close();
        cout<<endl;
        copy(nameList.begin(),nameList.end(),ostream_iterator<string>(cout," "));

        cout<<endl;

    return 0;

}
Mad Physicist
  • 76,709
  • 19
  • 122
  • 186
  • 1
    Then don't use a `std::map`? – Rakete1111 Jun 14 '17 at 16:46
  • 4
    [`std::set`](http://en.cppreference.com/w/cpp/container/set) and [`std::map`](http://en.cppreference.com/w/cpp/container/map) are *ordered* on the key. If you want *unordered* then use [`std::unordered_set`](http://en.cppreference.com/w/cpp/container/unordered_set) and [`std::unordered_map`](http://en.cppreference.com/w/cpp/container/unordered_map). – Some programmer dude Jun 14 '17 at 16:47
  • @Coldspeed No it doesn't - a std::map is a tree, not a hashtable. –  Jun 14 '17 at 16:48
  • 1
    Also read [Why is iostream::eof inside a loop condition considered wrong?](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong). Using `read.good()` is just the same. – Some programmer dude Jun 14 '17 at 16:49
  • Sounds like you want something like Python's `OrderedDict`, which keeps the order in which things get added. – Mad Physicist Jun 14 '17 at 16:49
  • Is your question that you want to remove dupes but preserve insertion order? –  Jun 14 '17 at 16:49
  • Here is what I think the dupe is: https://stackoverflow.com/q/2551905/2988730 – Mad Physicist Jun 14 '17 at 16:50
  • I would store the names in a `std::vector` (to preserve the order) and additionally registering their indices in a `std::set` whereby using a special comparator which accesses the vector and considers the names at the resp. indices. – Scheff's Cat Jun 14 '17 at 16:52
  • You could use the set just to check if the value was seen before and use a vector for actual storage of the result. – zstewart Jun 14 '17 at 16:53
  • @Someprogrammerdude Sorry, (in opposition to at least 4 other guys) I don't get it. AFAIK, neither `std::unordered_set` nor `std::unordered_map` preserve the insertion order. What am I missing? – Scheff's Cat Jun 14 '17 at 17:01
  • @Scheff The OP doesn't actually say anything about preserving insertion order, just that the contents should not be sorted (ordered). – Some programmer dude Jun 14 '17 at 17:04
  • @Someprogrammerdude So, you tried to teach something else (e.g. how important it is to clearly specify the intention). Somehow, I was expecting such an answer. But, you are right: "Preserving order" was my own interpretation... – Scheff's Cat Jun 14 '17 at 17:06
  • @Someprogrammerdude I will try using unordered_set, but I am not sure whether task asking me this – Dantes Dantonovich Jun 14 '17 at 17:09
  • How to read from text file into unordered_set? – Dantes Dantonovich Jun 14 '17 at 17:19
  • @Scheff I don't wanna use vector dude – Dantes Dantonovich Jun 14 '17 at 17:20
  • Actually, it could be very simple: Use a `std::set` for book-keeping (i.e. to detect whether a name is a duplicate). If a name is inserted the 1st time then output it otherwise discard it. Did your assignment require that it _must_ be stored somewhere or that input and output may not happen "interleaved"? – Scheff's Cat Jun 14 '17 at 17:24
  • And, now the highest-voted comment of Some programmer dude makes somehow sense to me. Iff the assignment says that it may not be sorted (whyever it shouldn't) then just use an `std::unorder_set` instead. The APIs of `std::set<>` and `std::unordered_set<>` are (as for every container template) quite similar. – Scheff's Cat Jun 14 '17 at 17:28
  • @Scheff Thanks man – Dantes Dantonovich Jun 14 '17 at 17:37

0 Answers0