c++中map的insert函数,请问是拷贝,还是引用?
myMap.insert(pair<int, myclass>(n, *cla));
请问是不是在map中复制了一份cla? 而不是引用? 展开
// map_Test.cpp : 定义控制台应用程序的入口点。
#include "stdafx.h"
#include <map>
#include <iostream>
using namespace std;
int main()
{
class a
{
public:
int z = 8;
};
map<int, a> my_map;
a a1;
cout << "显示初始化a类的值" << std::endl;
cout << a1.z << std::endl;
cout << "加入map中..." << std::endl;
my_map.insert(::pair<int, a>(1, a1));
a1.z = 6;
cout << "++显示map外修改a类值" << std::endl;
cout << a1.z << std::endl;
map<int, a>::iterator iter = my_map.begin();
while (iter != my_map.end())
{
cout << "__打印map中a值" << std::endl;
cout <<iter->second.z<< std::endl;
cout << "获取map中a并修改" << std::endl;
a a2 = iter->second;
a2.z = 3;
cout << a2.z << std::endl;
cout << "__map中a值再打印打印" << std::endl;
cout << iter->second.z << std::endl;
iter->second.z = 9;
cout << "__map中指针直接修改a值再打印打印" << std::endl;
cout << iter->second.z << std::endl;
iter++;
}
cout << "++显示外部的a值" << std::endl;
cout << a1.z << std::endl;
system("pause");
return 0;
}