C++17 Structured Bindings optimization guide as Expected with std::map - Getting Unexpected Values
I'm migrating some code and I'm relatively new to this, so bear with me... I'm experiencing an scenario with structured bindings in C++17 while trying to iterate over a `std::map`. I expected to access both the key and value directly, but I'm getting unexpected results when the keys are non-contiguous. Hereโs the relevant code snippet: ```cpp #include <iostream> #include <map> int main() { std::map<int, std::string> myMap = {{1, "First"}, {3, "Third"}, {5, "Fifth"}}; for (auto &[key, value] : myMap) { std::cout << "Key: " << key << ", Value: " << value << std::endl; } return 0; } ``` I expected the output to be: ``` Key: 1, Value: First Key: 3, Value: Third Key: 5, Value: Fifth ``` However, Iโm sometimes getting the following output when I run it multiple times: ``` Key: 1, Value: First Key: 3, Value: Fifth Key: 5, Value: Third ``` Iโve double-checked my map initialization, and it seems correct. Iโm compiling with GCC 9.3.0. I also tried using `std::tie` instead of structured bindings, but that didnโt change the output. Is there something Iโm misunderstanding about how structured bindings work with `std::map`, or is this a compiler bug? Any insights would be appreciated! This is part of a larger CLI tool I'm building. Is there a better approach? This is happening in both development and production on Debian. Is this even possible?