c++ map数组赋值给另外一个map
时间: 2023-07-05 22:31:51 浏览: 212
可以使用赋值运算符(=)或者构造函数进行赋值操作。例如:
```c++
#include <iostream>
#include <map>
using namespace std;
int main() {
map<int, string> m1 {{1, "one"}, {2, "two"}, {3, "three"}};
map<int, string> m2;
// 使用赋值运算符进行赋值
m2 = m1;
// 使用构造函数进行赋值
map<int, string> m3(m1);
// 输出结果
cout << "m1: ";
for (auto& p : m1) {
cout << p.second << " ";
}
cout << endl;
cout << "m2: ";
for (auto& p : m2) {
cout << p.second << " ";
}
cout << endl;
cout << "m3: ";
for (auto& p : m3) {
cout << p.second << " ";
}
cout << endl;
return 0;
}
```
输出结果为:
```
m1: one two three
m2: one two three
m3: one two three
```
可以看到,m2 和 m3 都被赋值为 m1 的内容。
阅读全文
相关推荐


















