STL::set help

Joined
Jan 23, 2009
Messages
2
Reaction score
0
When we iterate through the set, can we somehow change the value of the object in the set? So I have a class Student which have a name and a num.
Add all these student in the set. When I iterate through the set, how can I change the value of "num"? In another word, I want to do something like this
Code:
Interator it->num = 10; //Sucks!!! this does not work :( :(
Below is my code. Thank you
Code:
#include <set>
#include <iostream>
using namespace std;

class Student{
   public:
      int num;
      string name;
};


class Comp
{
   public:
      bool operator()(Student s1, Student s2)
      {
         if(s1.num < s2.num )
            return true;
         else
            return false;
      }
};

int main()
{
   set<Student, Comp> myStudent;
   Student student1;
   Student student2;
   student1.num = 4;
   student1.name = "Tom";
   student2.num = 7;
   student2.name = "Arab";
   myStudent.insert(student2);
   myStudent.insert(student1);
   
   set<Student, Comp>::iterator it;
   for(it=myStudent.begin(); it!=myStudent.end(); it++)
   {
      (*it).num = 10;
      cout<<it->num<<"   "<<it->name<<endl;
   }
}

So I want the output to come out like this
Code:
10  Tom
10  Arab
Instead of
Code:
4  Tom
7  Arab
I know change the value of "num" is a bad idea, because the set will not reorder by itself, but I just want to know and plus what if I want to change Tom and Arab instead of num. Please help
 
Last edited:

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,150
Latest member
MakersCBDReviews
Top