for each バグ

map / unordered_map を for each 文でループして

要素を const 参照で受け取ったらキーが消えました

#include <iostream>
#include <string>
#include <map> // or <unordered_map>

using namespace std;

int main()
{
    map<string, int> dict; // or unordered_map<string, int>

    dict["Akira"]   = 23;
    dict["Johnny"]  = 38;
    dict["Millia"]  = 16;

    for each (const pair<string, int>& item in dict) {
        cout << item.first << "," << item.second << endl;
    }
    // ,23
    // ,38
    // ,16

    return 0;
}

VC++8.0(2005) 〜 VC++9.0(2008) SP1 Beta で確認


バグ報告済み

C++ for each 文で std::map を使うとキーが消える