Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Recommended way to insert elements into map [duplicate]

Tags:

c++

stl

stdmap

Possible Duplicate:
In STL maps, is it better to use map::insert than []?

I was wondering, when I insert element into map, what is the recommended way. Should I

map[key] = value; 

or

map.insert(std::pair<key_type, value_type>(key, value)); 

I did the following quick test:

#include <map> #include <string> #include <iostream>  class Food { public:     Food(const std::string& name) : name(name) { std::cout << "constructor with string parameter" << std::endl; }     Food(const Food& f) : name(f.name) { std::cout << "copy" << std::endl; }     Food& operator=(const Food& f) { name = f.name; std::cout << "=" << std::endl; return *this; }      Food() { std::cout << "default" << std::endl; }     std::string name; };  int main() {     std::map<std::string, Food> m0;  /* 1) constructor with string parameter 2) copy 3) copy 4) copy */     m0.insert(std::pair<std::string, Food>("Key", Food("Ice Cream")));  /* 1) constructor with string parameter 2) default 3) copy 4) copy 5) = */     // If we do not provide default constructor.     // C2512: 'Food::Food' : no appropriate default constructor available     m0["Key"] = Food("Ice Cream"); } 
  1. I realize by using member function insert, less value's function call will be involved. So, is using insert a recommended way?
  2. Why default constructor is needed, when map[key] = value way is being used?

I know that insert doesn't overwrite existence key value pair, but map[key] = value does. However, is this the only factor I take into consideration, when try to choose among the both?

How about

  1. Performance
  2. Availability of value's default constructor
  3. ???
like image 274
Cheok Yan Cheng Avatar asked Aug 05 '11 06:08

Cheok Yan Cheng


People also ask

How do you insert elements in a map?

The standard solution to insert new elements into a map is using the std::map::insert function. It inserts the specified key-value pair into the map only if the key already doesn't exist. If the key already exists in the map, the element is not inserted.

What will happen if we insert duplicate key in map C++?

a map will not throw any compile/run time error while inserting value using duplicate key. but while inserting, using the duplicate key it will not insert a new value, it will return the same exiting value only. it will not overwrite. but in the below case it will be overwritten.

Does map insert make a copy?

Yes -- when you insert an item into an std::map, you pass it by value, so what it contains is a copy of what you passed.

Can a map hold duplicate keys C++?

C++ Software Engineering Multi-map in C++ is an associative container like map. It internally store elements in key value pair. But unlike map which store only unique keys, multimap can have duplicate keys.


2 Answers

  1. insert is not a recommended way - it is one of the ways to insert into map. The difference with operator[] is that the insert can tell whether the element is inserted into the map. Also, if your class has no default constructor, you are forced to use insert.
  2. operator[] needs the default constructor because the map checks if the element exists. If it doesn't then it creates one using default constructor and returns a reference (or const reference to it).

Because map containers do not allow for duplicate key values, the insertion operation checks for each element inserted whether another element exists already in the container with the same key value, if so, the element is not inserted and its mapped value is not changed in any way.

like image 95
BЈовић Avatar answered Sep 22 '22 12:09

BЈовић


Use insert if you want to insert a new element. insert will not overwrite an existing element, and you can verify that there was no previously exising element:

if ( !myMap.insert( std::make_pair( key, value ) ).second ) {     //  Element already present... } 

Use [] if you want to overwrite a possibly existing element:

myMap[ key ] = value; assert( myMap.find( key )->second == value ); // post-condition 

This form will overwrite any existing entry.

like image 41
James Kanze Avatar answered Sep 19 '22 12:09

James Kanze