Hello!

Something I blunted at the end of the day ... I can't implement a data set (of my class) using **std:set**. Here is my data type, used for the data type in the container:

 class employee { private: int id; string name; string adress; public: employee(int id2, string name2, string adress2):id(id2), name(name2), adress(adress2) {} employee(){id = 0; name=adress="";} employee(int id2):id(id2){name=adress=""; id = id2;} int getID(){return id;}; string getName(){return name;}; }; 

Nothing remarkable, right? I also overloaded several operations that need to be reloaded in order for some container methods like std :: set :: find () to work :

 // Этот нужен для разнообразия... ostream &operator<<(ostream &s,employee &emp) { s<<emp.getName()<<endl; return s; } bool operator ==(employee a, employee b) { return a.getID()==b.getID(); } bool operator <(employee a, employee b) { return a.getID()<b.getID(); } 

Next, I try to work with the container itself through iterators std :: set <class T> :: iterator :

  set<employee> emp; emp.insert(employee(123)); emp.insert(employee(125)); emp.insert(employee(124)); set<employee>::iterator itr = emp.find(employee(125)); cout<<*itr->getID()<<endl; // не работает cout<<*itr<<endl; // тоже не работает... 

What's wrong, guys? Tell me please...

  • What does "not working" mean? Not compiled? - fogbit
  • Yes, it does not compile. In both cases, it produces different errors, but with one meaning, which, they say, is an unacceptable conversion ... - Salivan
  • I advise you to check the value of the iterator returned by find, whether it points to set :: end, i.e. does this function find the item it is looking for - skegg

2 answers 2

cout << itr-> getID () << endl; // replacements with iter-> getId (), since You are trying to dereference a string, not an iterator cout << itr << endl; // it seems it just displays an empty string (specifically in the example)

    Did not begin to deal with the code, try to change.

     cout << *itr->getID() << endl; // не работает //на cout << (*itr)->getID() << endl; // для работы с указателями //или cout << (*itr).getID() << endl; // а это как у вас с объектами 

    Depending on what is in your container: objects or pointers to them.

    • So also tried - did not work. And in general, you brought a complete get on. This, for example, does not apply to pointers: (* itr) .getID () - Salivan 9:47 pm
    • Without reading and understanding my example, are you a minus one? I changed in your example the current cout << * itr-> getID () << endl; // does not work on cout << (* itr) .getID () << endl; and I have everything compiled! - Roman Goriachevskiy
    • Well, man, I am correcting the correct answer and no longer comes here because he uses it 100%, but he wrote that it was crap. - Roman Goriachevskiy
    • one
      for working with pointers I can offer several options for understanding this comment. We must learn to write without ambiguity. - skegg
    • one
      By the way, you need to write "about what." Back to school - skegg