473,237 Members | 1,374 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,237 software developers and data experts.

vector and sort problem

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
Jul 23 '05 #1
4 1874
Johan wrote:
Hi,

Why does my vector not sort. What I understand is you have to overload
the < operator, but that does not work.
Overloading < is one way to do it.

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

using std::string;
using std::cout;
using std::endl;
using std::sort;
using std::vector;
class Demo
{
public :
Demo(string filename) : filename(filename)
{
}

bool operator<(const Demo& d)
bool operator<(const Demo& d) const
{
return filename < d.filename;
}

string getFilename()
string getFilename() const
{
return filename;
}

private :
string filename;
};

int main(int argc, char** argv[])
{
vector<Demo*> v;
Your element type is not Demo, but pointer to Demo. So the vector will be
sorted by pointer values.
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());
If you want to store pointers, you have to provide as a third parameter to
sort a comparison function/functor, since you cannot overload operator< for
pointers. Add before main:

struct cmp
{
bool operator()(const Demo* lhs, const Demo* rhs) const
{
return *lhs < *rhs;
}
};

And then replace your sort with:

sort(v.begin(), v.end(), cmp());

typedef vector<Demo*>::const_iterator iter;
for(iter i = v.begin(); i != v.end(); i++)
{
cout << (*i)->getFilename() << endl;
}

return 0;
}


You don't delete the elements. In this case, it won't make much of a
difference, but it's good to get used to cleaning up after yourself.

Jul 23 '05 #2
"Johan" <jo***********@ns.nl> wrote in message
news:cd**************************@posting.google.c om...
Why does my vector not sort. What I understand is you have to overload
the < operator, but that does not work.

As a complement to Rolf's accurate comment, I would just
like to point out that using a vector of values (instead
of pointers) might be a better solution:

int main(int argc, char** argv[])
{
vector<Demo> v;

v.push_back(Demo("z"));
v.push_back(Demo("c"));
v.push_back(Demo("a"));
v.push_back(Demo("v"));
v.push_back(Demo("t"));
v.push_back(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;
}

Fewer memory allocations, and no memory leaks.
Cheers,
Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Jul 23 '05 #3
Johan wrote:
Hi,

Why does my vector not sort. What I understand is you have to overload the < operator, but that does not work.
It works just fine -- but you've defined your collection in a way that
prevents it from ever being used.

[ ... ]
bool operator<(const Demo& d)
{
return filename < d.filename;
}
This is fine -- it compares one Demo to another just like it should.
string getFilename()
{
return filename;
}
It's only minimally related to the problem at hand, but this is not
nearly so fine -- it exposes the class' internals to the outside world.

[ ... ]
vector<Demo*> v;
Here's your real problem: up above, you've defined how to compare one
Demo to another, but here you're not storing Demo's -- you're storing
pointers to Demo's instead.

There are two ways to deal with this. If it's really crucial that you
store pointers (e.g. you really have a polymorphic hierarchy with Demo
as the base class) then you need to define a function/functor to do the
comparison:

bool cmp(Demo const &a, Demo const &b) {
return a.getFileName() < b.GetFileName();
}

and then when you sort the vector, tell sort to use that to do the
comparisons:

std::sort(v.begin(), v.end(), cmp);

It's usually better, however, to simply store objects instead of
pointers, which will allow your operator< to do the job:

vector<Demo> v;

std::sort(v.begin(), v.end());

As it stands right now, std::sort is comparing the _pointers_ rather
than the Demo's themselves -- in practical terms, this means it's
sorting them into the order in which they're stored in memory, though
from a technical viewpoint, it's simply undefined behavior.
sort(v.begin(), v.end());
typedef vector<Demo*>::const_iterator iter;
for(iter i = v.begin(); i != v.end(); i++)
{
cout << (*i)->getFilename() << endl;
}


Here's why having a (public) getFilename is really a bad idea: now the
rest of your code knows about, and makes use of the internals of a Demo
instead of treating it as an object in its own right. I'd prefer to do
something like:

class Demo {
// your "stuff" but with getFilename private or removed.
public:
friend ostream &operator<<(ostream &os, Demo const &d);
};

ostream &operator<<(ostream &os, Demo const &d) {
return os << d.getFilename();
}

and then when you want to print out your sorted vector of demos, you
can write code that actually writes out the Demos:

for(iter i = v.begin(); i != v.end(); i++)
{
cout << *i << endl;
}

or better yet, use an algorithm and write even less yourself:

std::copy(v.begin(), v.end(), ostream_iterator<Demo>(cout, "\n"));

--
Later,
Jerry.

The universe is a figment of its own imagination.

Jul 23 '05 #4

[snip]
string getFilename()
{
return filename;
}
It's only minimally related to the problem at hand, but this is not
nearly so fine -- it exposes the class' internals to the outside world.


Hmm .. I am not sure I agree. All the getFilename function does is
provide a way to obtain a string (by value), from the object. This does not
say anything about the internals of the Demo class, and it seems quite
reasonable, given that filenames are commonly represented as strings. For
all we know, the string returned by getFilename could be generated at random
(although that would not be very useful).
If you are actually talking about the "in place" definition of the
function, then ok, point taken. But the OP may well have presented it that
way for the sake of simplicity. Certainly if the class is handled in the
canonical way (declarations in .hpp, definitions in .cpp), then there is no
reason that the internal representation of the class can't be changed, given
the definition of getFilename above. As long as getFilename returns a
string, nothing will need to be changed in applications using Demo objects.

[snip]
Here's why having a (public) getFilename is really a bad idea: now the
rest of your code knows about, and makes use of the internals of a Demo
instead of treating it as an object in its own right. I'd prefer to do
something like:
Again, I don't agree that getFilename exposes the representation of the
class(see above). However, I *do* agree that your suggested method below
for handling this particular example is generally a better approach.

class Demo {
// your "stuff" but with getFilename private or removed.
public:
friend ostream &operator<<(ostream &os, Demo const &d);
};

ostream &operator<<(ostream &os, Demo const &d) {
return os << d.getFilename();
}

and then when you want to print out your sorted vector of demos, you
can write code that actually writes out the Demos:

for(iter i = v.begin(); i != v.end(); i++)
{
cout << *i << endl;
}

or better yet, use an algorithm and write even less yourself:

std::copy(v.begin(), v.end(), ostream_iterator<Demo>(cout, "\n"));


Dave Moore
Jul 23 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: David Jones | last post by:
Hi I have a list of values stored in a vector e.g. std::vector<int> x; x = 1; x = 4; x = 2; x = 4;
6
by: John Black | last post by:
I have a sort statement for vectr like this, sort(vec.begin(), vec.end(), EleSmaller); in running I find that the sort procedure falls into an endless loop, by adding some debug statement I...
3
by: A_StClaire_ | last post by:
hi, not sure what I'm doing wrong here. getting "error C2064: term does not evaluate to a function taking 2 arguments" in response to my SortCardVector function...? Card.h: #pragma once
10
by: eiji | last post by:
Hi folks, I have a problem compiling this under VC6! Maybe someone has some minutes to look at this: template<class T> class Compare { public: Compare(){}; virtual ~Compare(){};
8
by: markww | last post by:
Hi, If I have a vector of structs like this: struct IMAGE { unsigned char *pPix; string strName; int nNumber; };
0
by: acosgaya | last post by:
hi, I am working in this problem, where I have a set of N d-dimensional points, e.g. (4,5,6,8) (2,0,4,6), are 4-d points, which I have stored in a vector of vectors. I am trying to partition...
6
by: Matt Chwastek | last post by:
Anyone who can help, I am curretnly attempting to write some code that will allow iteration using a vector<intfrom the highest possilbe degree of a combination of ones & zeros (111, 110, 101,...
3
by: Eric Lilja | last post by:
Hello, consider the following assignment and my code for it: /* Write a program that does the following: * Integer numbers shall be read from a textfile and stored in a std::vector. The name...
5
by: jeremit0 | last post by:
I'm trying to sort a vector<complex<double and can't figure it out. I recognize the problem is that there isn't a default operator< for complex data types. I have written my own operator and can...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.