How can I sort this set?

P

petertwocakes

Hi

I have

typedef vector<string> TokenVector
which is an array of string (words) making up a sentence.
For the purposes of my app I need it structured like this, rather than
a single composite string.

then I have

typedef set<TokenVector, compare > TokenVectorSet;

which is a set of such sentences.

I want to sort the set in to alphabetical order, as if I was dealing
with composite strings.

I think I need something like this to use as a compare function:

class compare
{
public:
std::vector<TokenVector> * myObjects;
compare(std::vector<TokenVector>* obj) : myObjects(obj) {}
bool operator ()(const int p1,const int p2)
{
.... construct composite strings from the TokenVectors and compare
them
}
};


Once I get down to operator (), I know to re-construct the strings and
compare them. It's the protocol/syntax of setting up the class/
function I'm struggling with.

Am I barking up the wrong tree? ...the compiler is just going mental!

Thanks for any clues.
 
J

Jiøí Paleèek

Hi
Hello,

I have

typedef vector<string> TokenVector
which is an array of string (words) making up a sentence.
For the purposes of my app I need it structured like this, rather than
a single composite string.

then I have

typedef set<TokenVector, compare > TokenVectorSet;

which is a set of such sentences.

I want to sort the set in to alphabetical order, as if I was dealing
with composite strings.

I think I need something like this to use as a compare function:

You mean functor.
class compare
{
public:
std::vector<TokenVector> * myObjects;

No, this is totally unneeded here.
compare(std::vector<TokenVector>* obj) : myObjects(obj) {}
bool operator ()(const int p1,const int p2)

No, the operator should take two "TokenVector const&", and compare them.
{
.... construct composite strings from the TokenVectors and compare
them
}
};

Am I barking up the wrong tree? ...the compiler is just going mental!

Thanks for any clues.

Just for other possibilities:

- you can use an ordinary function, and use some library function object
(boost::function or std::pointer_to_binary_function) to eg. use it with a
container.

- you can check if the default (lexicographic) ordering on vectors isn't
consistent with your needs. Note that the lexicographic ordering of lists
of words is the same as the lexicographic ordering of sentences formed by
joining the list with a character with a lower value than any of the
characters used in the words.

Regards
Jiri Palecek
 
P

petertwocakes

On Fri, 27 Nov 2009 13:47:46 -0800 (PST) in comp.lang.c++, petertwocakes







Make life easy for yourself, construct the composite strings, then use
std::map<string, TokenVector>

Thanks Sam, Jiří & David

Changed const int p1 to const TokenVector &a etc. (My silly mistake)

Got rid of std::vector<TokenVector> * myObjects; as suggested.
All the googled examples I found before asking here, seemed to include
that, though I didn't understand what it was doing.

It works now :)

" - you can check if the default (lexicographic) ordering on vectors
isn't
consistent with your needs"

Remarkably it is, even though I've now sub-classed string to 'Token'
to use in TokenVector.
That's almost like magic ;) But glad I've learned about custom
functors anyway.

"Make life easy for yourself, construct the composite strings, then
use
std::map<string, TokenVector>"

Thanks, ultimately I probably will, but initially this was a learning
exercise as much as anything.

Thanks.
 
J

James Kanze

petertwocakes writes:
As such the correct prototype for a comparison object [for an
std::set< TokenVector >], that you intend to use with your
actual set should be:
bool operator()(const TokenVector &a, const TokenVector &b) const;
It's arguable whether or not the comparison operator should be
a constant function, or not.

It a requirement that the comparison operator be a constant
function, since std::set will call it on a copy which is a
component of the set object, and you want to be able to be able
to call const functions (like set<>::find) on the set.
 

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,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top