によってマップ値にアクセスする[0]?インデックス
私はマップが内部的にソートすることを知っていると私はこれで大丈夫だよ、私はインデックスでマップ内の値を取得したいです。私は[0] MYMAPを試みたが、私はエラーを取得:
Error 1 error C2679: binary '[' : no operator found which takes a right-hand operand of type 'int' (or there is no acceptable conversion)
私はこのような何か行うことができます実現:
string getKeyAtIndex (int index){
map<string, int>::const_iterator end = myMap.end();
int counter = 0;
for (map<string, int>::const_iterator it = myMap.begin(); it != end; ++it) {
counter++;
if (counter == index)
return it->first;
}
}
をしかし、確かに、これは非常に非効率的ですか?より良い方法がありますか?
...私の実装です...ランダムアクセスが必要です。インデックス0は単なる例であり、実際の目標ではありませんでした。 –