STL笔记之【map之添加元素】

//---------------------------------------------------------
// 向map中插入元素的方法比较
//---------------------------------------------------------
class A
{
public:
 A()
 {
  cout << "A()" << endl;
 }函数

 A(const A& rhs)
 {
  cout << "A(const A&)" << endl;
 }class

 ~A()
 {
  cout << "~A()" << endl;
 }
};构造函数

map<int, A> mapTest;map

//*****************
// 方法一
//*****************
mapTest.insert(map<int, A>::value_type(0, a));
输出:(3次构造函数)
A()
A(const A&)
A(const A&)
~A()
~A()
~A()方法

//*****************
// 方法二
//*****************
mapTest.insert(pair<const int, A>(0, a));
输出:(3次构造函数)
A()
A(const A&)
A(const A&)
~A()
~A()
~A()总结

//*****************
// 方法三
//*****************
mapTest.insert(pair<int, A>(0, a));
输出:(4次构造函数)
A()
A(const A&)
A(const A&)
A(const A&)
~A()
~A()
~A()
~A()make

//*****************
// 方法四
//*****************
mapTest[0] = a;
输出:(4次构造函数,实际上还调用了一次operator=)
A()
A()
A(const A&)
A(const A&)
~A()
~A()
~A()
~A()co

//*****************
// 方法五
//*****************
mapTest.insert(make_pair(0, a));
输出:(5次构造函数)
A()
A(const A&)
A(const A&)
A(const A&)
A(const A&)
~A()
~A()
~A()
~A()
~A()opera

//---------------------------------------------------------
// 总结
//---------------------------------------------------------
很显然,方法1、二是最优的,成本最少。
        方法五是最差的,成本最高。const

相关文章
相关标签/搜索