- 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
Below is my code. Thank you
So I want the output to come out like this
Instead of
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
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 :( :(
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
Code:
4 Tom
7 Arab
Last edited: