在本文中,我们将讨论C ++ STL中映射等于'='运算符的工作,语法和示例。
映射是关联容器,它有助于按特定顺序存储由键值和映射值的组合形成的元素。在映射容器中,数据始终在内部借助其关联的键进行排序。映射容器中的值通过其唯一键访问。
map::operator =等于操作符。通过覆盖容器的当前内容,该运算符用于将元素从一个容器复制到另一个容器。
Map_name.max_size();
在运算符的左侧有一个映射,在容器的右侧有另一个映射。右侧的内容将复制到左侧的映射。
没有运算符的返回值
map<char, int> newmap, themap;
newmap.insert({1, 20});
newmap.insert({2, 30});
themap = newmap输出结果
themap = 1:20
#include <bits/stdc++.h>
using namespace std;
int main() {
map<int, int> TP, temp;
TP.insert({ 2, 20 });
TP.insert({ 1, 10 });
TP.insert({ 3, 30 });
TP.insert({ 4, 40 });
TP.insert({ 6, 50 });
temp = TP;
cout<<"\nData in map TP is: \n";
cout << "KEY\tELEMENT\n";
for (auto i = TP.begin(); i!= TP.end(); ++i) {
cout << i->first << '\t' << i->second << '\n';
}
cout << "\nData in copied map temp is : \n";
cout << "KEY\tELEMENT\n";
for (auto i = TP.begin(); i!= TP.end(); ++i) {
cout << i->first << '\t' << i->second << '\n';
}
return 0;
}输出结果
Data in map TP is: MAP_KEY MAP_ELEMENT 1 10 2 20 3 30 4 40 6 50 Data in copied map temp is : MAP_KEY MAP_ELEMENT 1 10 2 20 3 30 4 40 6 50