If the value of an element in the set changes, the order may already be incorrect. As shown in this small program:
#include <algorithm> #include <iostream> #include <set> #include <string> struct Comp { bool operator()(const std::string * lhs, const std::string * rhs) { return *lhs < *rhs; } }; int main() { typedef std::set<std::string*, Comp> MySet; MySet mySet; std::string * a = new std::string("a"); mySet.insert(a); std::string * c = new std::string("c"); mySet.insert(c); std::string * b = new std::string("b"); mySet.insert(b); for (MySet::iterator it = mySet.begin(); it != mySet.end(); ++it) { std::cout << *(*it) << std::endl; } // Ouput has correct order: // a // b // c *b = "z"; std::cout << std::endl; std::string * d = new std::string("d"); mySet.insert(d); for (MySet::iterator it = mySet.begin(); it != mySet.end(); ++it) { std::cout << *(*it) << std::endl; } // Output no longer ordered correctly: // a // d // z // c return 0; }
How can I say that the set "updates" the internal sort?
source share