C++ STL 中的映射运算符 =


在这篇文章中,我们将讨论 C++ STL 中映射等号“=”运算符的工作原理、语法和示例。

什么是 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

更新时间:2020 年 4 月 15 日

150 次浏览

开启你的 职业生涯

完成课程获得认证

立即开始
广告
© . All rights reserved.