2

Another small question about STL:

i have Dictionary:

map <string,vector <Wordy> > Dictionary;

using structure Wordy:

struct Wordy{ int count; string word;}

also this structure have overloaded operator<

bool operator< (Wordy& One, Wordy& Two){return One.count<Two.count;}

but this sort() function from algorithm doesn't work!

sort(Dictionary.find(s)->second.begin(),Dictionary.find(s)->second.end());
1
  • @Martin: The OP isn't sorting the map, she's sorting a particular element of the map. Commented Jul 4, 2011 at 23:43

2 Answers 2

8

Your operator< should take its parameters by reference-to-const, I think that might be it:

bool operator< (const Wordy& One, const Wordy& Two){return One.count<Two.count;}
//              ^^^^^             ^^^^^
Sign up to request clarification or add additional context in comments.

Comments

0

check the post How to use std::sort with a vector of structures and compare function? . it explains how to use sort with a customized predicate function

4 Comments

I don't think that's it. That question deals with a sort function that wasn't returning bool.
Why should you need a custom predicate, if you can (and want) to implement operator<?
@zdan: do you mean the bool operator<(...) function?
@NirMH: Yes, I meant the sort "predicate" not the sort function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.