Can someone explain the output I get from this simple program using std::map . Notice that I insert p into the map, but not q , but he says that he found both of them, but also says that there is only 1 element on the map!
#include <map> #include <iostream> struct screenPoint { float x = 0, y = 0; screenPoint(float x_, float y_): x{x_}, y{y_}{} }; bool operator<(const screenPoint& left, const screenPoint& right){ return left.x<right.x&&left.y<right.y; } std::map<screenPoint, float> positions; int main(int argc, const char * argv[]) { auto p = screenPoint(1,2); auto q = screenPoint(2,1); positions.emplace(p,3); auto f = positions.find(p); auto g = positions.find(q); if (f == positions.end()){ std::cout << "f not found"; } else { std::cout << "f found"; } std::cout << std::endl; if (g == positions.end()){ std::cout << "g not found"; } else { std::cout << "g found"; } std::cout << std::endl; std::cout << "number elements: " << positions.size() << "\n"; return 0; }
Output:
f found g found number elements: 1
source share