Posts

Showing posts from November 24, 2018

Is it possible to force STL set to reevaluate predicate?

Image
Clash Royale CLAN TAG #URR8PPP up vote 17 down vote favorite 2 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 : sentencesByFrequency) std::cout << sp->words << std::endl; foo->frequency = 5; for (Sentence* sp : sentencesByFrequency) std::cout << sp->words << std::endl; The output of the above code is