S
subramanian100in
For inserting a key-value pair into a map, should I use operator[] or
insert on map ?
Which is more efficient ?
For example consider the following program:
#include <cstdlib>
#include <iostream>
#include <map>
using namespace std;
class Second
{
public:
Second();
Second(const Second& ref);
Second& operator=(const Second& rhs);
inline void value(int arg);
inline int value() const;
private:
int val;
};
Second::Second()
{
cout << "Second default ctor called" << endl;
}
Second::Second(const Second& ref)
{
cout << "Second copy ctor called" << endl;
val = ref.val;
}
Second& Second:perator=(const Second& rhs)
{
cout << "Second operator= called" << endl;
if (this != &rhs)
{
val = rhs.val;
}
return *this;
}
inline void Second::value(int arg)
{
val = arg;
}
inline int Second::value() const
{
return val;
}
int main()
{
Second obj;
obj.value(100);
map<int, Second> m;
cout << "inserting map element in main()" << endl;
cout << "------------------------------" << endl;
m[1] = obj;
obj.value(200);
cout << "------------------------" << endl;
cout << "calling insert from main" << endl;
cout << "------------------------" << endl;
m.insert(map<int,Second>::value_type(2,obj));
return EXIT_SUCCESS;
}
I compiled this program with g++ 3.4.3 as
g++ -std=c++98 -pedantic -Wall -Wextra x.cpp
The output of this program is
Second default ctor called
inserting map element in main()
------------------------------
Second default ctor called
Second copy ctor called
Second copy ctor called
Second operator= called
------------------------
calling insert from main
------------------------
Second copy ctor called
Second copy ctor called
Here, the statement
m[1] = obj;
gives rise to the display of
Second default ctor called
Second copy ctor called
Second copy ctor called
Second operator= called
The statement
m.insert(map<int,Second>::value_type(2,obj));
gives rise to the display of
Second copy ctor called
Second copy ctor called
ie map<int, Second>:perator[] calls one default ctor and assignment
operator corresponding to the mapped value in addition to the two copy
ctor calls which get called for both map<int, Second>:perator[] and
map<int, Second>::insert().
From this, can we conclude that for inserting an element, insert()
member function should be preferred over operator[] ? Will this be the
case with all implementations ? What does the standard say regarding
this ?
Kindly explain
Thanks
V.Subramanian
insert on map ?
Which is more efficient ?
For example consider the following program:
#include <cstdlib>
#include <iostream>
#include <map>
using namespace std;
class Second
{
public:
Second();
Second(const Second& ref);
Second& operator=(const Second& rhs);
inline void value(int arg);
inline int value() const;
private:
int val;
};
Second::Second()
{
cout << "Second default ctor called" << endl;
}
Second::Second(const Second& ref)
{
cout << "Second copy ctor called" << endl;
val = ref.val;
}
Second& Second:perator=(const Second& rhs)
{
cout << "Second operator= called" << endl;
if (this != &rhs)
{
val = rhs.val;
}
return *this;
}
inline void Second::value(int arg)
{
val = arg;
}
inline int Second::value() const
{
return val;
}
int main()
{
Second obj;
obj.value(100);
map<int, Second> m;
cout << "inserting map element in main()" << endl;
cout << "------------------------------" << endl;
m[1] = obj;
obj.value(200);
cout << "------------------------" << endl;
cout << "calling insert from main" << endl;
cout << "------------------------" << endl;
m.insert(map<int,Second>::value_type(2,obj));
return EXIT_SUCCESS;
}
I compiled this program with g++ 3.4.3 as
g++ -std=c++98 -pedantic -Wall -Wextra x.cpp
The output of this program is
Second default ctor called
inserting map element in main()
------------------------------
Second default ctor called
Second copy ctor called
Second copy ctor called
Second operator= called
------------------------
calling insert from main
------------------------
Second copy ctor called
Second copy ctor called
Here, the statement
m[1] = obj;
gives rise to the display of
Second default ctor called
Second copy ctor called
Second copy ctor called
Second operator= called
The statement
m.insert(map<int,Second>::value_type(2,obj));
gives rise to the display of
Second copy ctor called
Second copy ctor called
ie map<int, Second>:perator[] calls one default ctor and assignment
operator corresponding to the mapped value in addition to the two copy
ctor calls which get called for both map<int, Second>:perator[] and
map<int, Second>::insert().
From this, can we conclude that for inserting an element, insert()
member function should be preferred over operator[] ? Will this be the
case with all implementations ? What does the standard say regarding
this ?
Kindly explain
Thanks
V.Subramanian