Posts

Showing posts from December 7, 2018

Is it possible to force STL set to reevaluate predicate?

Image
up vote 18 down vote favorite 3 Consider the following data structures and code. struct Sentence { std::string words; int frequency; Sentence(std::string words, int frequency) : words(words), frequency(frequency) {} }; struct SentencePCompare { bool operator() (const Sentence* lhs, const Sentence* rhs) const { if (lhs->frequency != rhs->frequency) { return lhs->frequency > rhs->frequency; } return lhs->words.compare(rhs->words) < 0; } }; std::set<Sentence*, SentencePCompare> sentencesByFrequency; int main(){ Sentence* foo = new Sentence("foo", 1); Sentence* bar = new Sentence("bar", 2); sentencesByFrequency.insert(foo); sentencesByFrequency.insert(bar); for (Sentence* sp : sentencesByFre...