본문 바로가기
Language/C&C++

[c++] std::map::operator[]

by 긞 2019. 9. 17.

mapped_type& operator[] (const key_type& k);

http://www.cplusplus.com/reference/map/map/operator[]/

 

map::operator[] - C++ Reference

mymap['a'] is an element mymap['b'] is another element mymap['c'] is another element mymap['d'] is mymap now contains 4 elements.

www.cplusplus.com

void function() const {

    std::map<int, std::vector<int>> m;

    std::vector<int> nums = {1, 2,  3, 4};

    for (auto num : nums) {

        m[num].push_back(num);

    }

}

 

[빌드오류] as 'this' argument discards qualifiers [-fpermissive]

 

[원인] std::map의 operator[] 는 const 로 선언되지 않았습니다. 따라서 const 로 선언된 함수 안에서 사용이 불가능합니다.

 

[대책] void function() const; 함수를 void function(); 으로 수정한다.

'Language > C&C++' 카테고리의 다른 글

[c++] C++ Core Guidelines  (0) 2019.11.06