C++ Map operator=()
C++ Map operator=()函数
在地图中,操作符=()具有以下三种用法:
Operator =()用于通过替换旧内容(或复制内容)将新内容分配给地图容器,并在必要时修改尺寸。
Operator =()用于将一个地图容器的内容移动到另一个地图容器中,并在必要时修改其大小。
Operator = 用于将初始化器列表中的元素复制到映射容器。
语法
copy(1) map& operator= (const map& x); //until C++ 11
copy (1) map& operator= (const map& x); //since C++ 11
move (2) map& operator= (map&& x); //since C++ 11
initializer list (3) map& operator= (initializer_list<value_type> il); //since C++ 11
复制(1):-将x中的所有元素复制到地图容器中。
移动(2):- x的内容到地图容器中。
initializer_list(3):-将il的元素复制到地图容器中。
参数
x : 相同类型的地图对象。
il : 初始化列表对象。
返回值
此指针。
示例1
让我们看一个复制内容的简单示例一张地图到另一张地图。
#include <iostream>
#include <map>
using namespace std;
int main(void) {
map<char, int> m1 = {
{'a', 10},
{'b', 20},
{'c', 30} };
cout << "Map m1 contains following elements" << endl;
for (auto it = m1.begin(); it != m1.end(); ++it)
cout << it->first << " = " << it->second << endl;
map<char, int> m2 = m1;
cout<<"\nAfter Copying the elements from m1 to m2... \n";
cout << "\nMap m2 contains following elements" << endl;
for (auto it = m2.begin(); it != m2.end(); ++it)
cout << it->first << " = " << it->second << endl;
return 0;
}
输出:
Map m1 contains following elements
a = 10
b = 20
c = 30
After copying the elements from m1 to m2...
Map m2 contains following elements
a = 10
b = 20
c = 30
在上面的示例中,使用operator =()函数将一个映射m1的内容复制到另一个映射m2、
示例2
让我们看一个简单的示例,将一个地图的元素移动到另一个。
#include <iostream>
#include <map>
using namespace std;
int main(void) {
map<char, int> m1 = {
{'a', 1},
{'b', 2},
{'c', 3} };
cout << "Map m1 contains following elements" << endl;
for (auto it = m1.begin(); it != m1.end(); ++it)
cout << it->first << " = " << it->second << endl;
map<char, int> m2 = move(m1);
cout<<"\nAfter moving the elements from m1 to m2... \n";
cout << "\nMap m2 contains following elements" << endl;
for (auto it = m2.begin(); it != m2.end(); ++it)
cout << it->first << " = " << it->second << endl;
return 0;
}
输出:
Map m1 contains following elements
a = 1
b = 2
c = 3
After moving the elements from m1 to m2...
Map m2 contains following elements
a = 1
b = 2
c = 3
在上面的示例中,使用operator =()函数将一个地图m1的内容移动到另一地图m2、
示例3
让我们看一个简单的示例,将初始化列表中的内容复制到map。
#include <iostream>
#include <map>
using namespace std;
int main(void) {
map<char, int> m;
m = {
{'a', 100},
{'b', 200},
{'c', 300},
{'d', 400} };
cout << "Map contains the following elements" << endl;
for (auto it = m.begin(); it != m.end(); ++it)
cout << it->first << " = " << it->second << endl;
return 0;
}
输出:
Map contains the following elements
a = 100
b = 200
c = 300
d = 400
在上面的示例中,使用operator =()将内容从初始化列表复制到映射m。
示例4
让我们看一下简单的例子。
#include <iostream>
#include <map>
using namespace std;
int main ()
{
map<char,int> first;
map<char,int> second;
first['x']=8;
first['y']=16;
first['z']=32;
second=first; // second now contains 3 ints
first=map<char,int>(); // and first is now empty
cout << "Size of first: " << first.size() << '\n';
cout << "Size of second: " << second.size() << '\n';
return 0;
}
输出:
Size of first: 0
Size of second: 3
在上面的示例中,首先它将计算空映射图的大小,然后将一些元素添加到第一幅图并复制到第二幅图。