에 의해지도 값을 액세스 [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은 실제 목표가 아니라 모범이었습니다. –