why use map.insert

why would you use map.insert to insert a pair if you can just use map[NAME] = VALUE?

1
2
3
4
5
6
7
8
9
10
11
int main(){
    std::map<std::string, std::string> m;
    m.insert(std::pair<std::string, std::string>("level", "3"));
    m["money"] = "12.13";
    
    
    std::cout << m["level"] << std::endl;
    std::cout << m["money"] << std::endl;
    
    
}
From: http://www.cplusplus.com/reference/map/map/insert/
Because element keys in a map are unique, the insertion operation checks whether each inserted element has a key equivalent to the one of an element already in the container, and if so, the element is not inserted, returning an iterator to this existing element (if the function returns a value).

map[NAME] = VALUE will always insert or overwrite.
map.insert(make_pair(NAME,VALUE)) will only insert and not overwrite.
Thanks histrungalot
Topic archived. No new replies allowed.