J
Johan
Hi,
Why does my vector not sort. What I understand is you have to overload
the < operator, but that does not work.
see code below
Thanks
Johan
class Demo
{
public :
Demo(string filename) : filename(filename)
{
}
bool operator<(const Demo& d)
{
return filename < d.filename;
}
string getFilename()
{
return filename;
}
private :
string filename;
};
int main(int argc, char** argv[])
{
vector<Demo*> v;
v.push_back(new Demo("z"));
v.push_back(new Demo("c"));
v.push_back(new Demo("a"));
v.push_back(new Demo("v"));
v.push_back(new Demo("t"));
v.push_back(new Demo("g"));
sort(v.begin(), v.end());
typedef vector<Demo*>::const_iterator iter;
for(iter i = v.begin(); i != v.end(); i++)
{
cout << (*i)->getFilename() << endl;
}
return 0;
}
output
z
c
a
v
g
t
Johan
Why does my vector not sort. What I understand is you have to overload
the < operator, but that does not work.
see code below
Thanks
Johan
class Demo
{
public :
Demo(string filename) : filename(filename)
{
}
bool operator<(const Demo& d)
{
return filename < d.filename;
}
string getFilename()
{
return filename;
}
private :
string filename;
};
int main(int argc, char** argv[])
{
vector<Demo*> v;
v.push_back(new Demo("z"));
v.push_back(new Demo("c"));
v.push_back(new Demo("a"));
v.push_back(new Demo("v"));
v.push_back(new Demo("t"));
v.push_back(new Demo("g"));
sort(v.begin(), v.end());
typedef vector<Demo*>::const_iterator iter;
for(iter i = v.begin(); i != v.end(); i++)
{
cout << (*i)->getFilename() << endl;
}
return 0;
}
output
z
c
a
v
g
t
Johan