C++ map operator[] 函数

C++ map operator[] 函数

C++ map operator[] 函数用于访问具有给定 键值 的映射中的元素。

它类似于 at() 函数。它们之间的唯一区别是,如果映射中没有访问的键,则at抛出异常,而 operator[ ] 会在映射中插入键值,如果该键已经存在于映射中,则什么都不做。

语法

考虑键值 k ,语法如下:

mapped_type& operator[] (const key_type& k);    //until C++ 11
mapped_type& operator[] (const key_type& k);   //since C++ 11
mapped_type& operator[] (key_type&& k); //since C++ 11

参数

k :访问映射值的元素的键值。

返回值

它返回一个对具有键值的元素的映射值的引用。

示例1

让我们看一个简单的访问元素的示例。

#include <iostream>
#include <map>
using namespace std;
int main() 
{

   map<char, int> m = {
            {'a', 1},
            {'b', 2},
            {'c', 3},
            {'d', 4},
            {'e', 5},
            };

   cout << "Map contains following elements" << endl;

   cout << "m['a'] = " << m['a'] << endl;
   cout << "m['b'] = " << m['b'] << endl;
   cout << "m['c'] = " << m['c'] << endl;
   cout << "m['d'] = " << m['d'] << endl;
   cout << "m['e'] = " << m['e'] << endl;

   return 0;
}

输出:

Map contains following elements
m['a'] = 1
m['b'] = 2
m['c'] = 3
m['d'] = 4
m['e'] = 5

在上述示例中,operator[]函数用于访问map的元素。

示例2

让我们看一个简单的示例,使用它们的键值添加元素。

#include <iostream>
#include <string>
#include <map>

using namespace std;

int main ()
{
  map<int,string> mymap = {
                { 101, "" },
                { 102, "" },
                { 103, ""} };

  mymap[101] = "Java"; 
  mymap[102] = "T";
  mymap[103] = "Point";


        // prints value associated with key 101, i.e. Java
  cout<<mymap[101]; 
          // prints value associated with key 102, i.e T
  cout<<mymap[102];
          // prints value associated with key 103, i.e Point    
  cout<<mymap[103];

  return 0;
}

输出:

JavaTPoint 

在上面的示例中,operator[]用于在使用关联的键值进行初始化之后添加元素。

示例3

让我们看一个简单的示例,来改变与键值相关联的值。

#include <iostream>
#include <string>
#include <map>

using namespace std;

int main ()
{
  map<int,string> mymap = {
                { 100, "Nikita"},
                { 200, "Deep"  },
                { 300, "Priya" },
                { 400, "Suman" },
                { 500, "Aman"  }};

  cout<<"Elements are:" <<endl;
  for (auto& x: mymap) {
        cout << x.first << ": " << x.second << '\n';
  }


  mymap[100] = "Nidhi"; // changes the value associated with key 100 to Nidhi
  mymap[300] = "Pinku"; // changes the value associated with key 300 to Pinku
  mymap[500] = "Arohi"; // changes the value associated with key 500 to Arohi


  cout<<"\nElements after make changes are:" <<endl;
  for (auto& x: mymap) {
        cout << x.first << ": " << x.second << '\n';
  }

  return 0;
}

输出:

Elements are:
100: Nikita
200: Deep
300: Priya
400: Suman
500: Aman

Elements after make changes are:
100: Nidhi
200: Deep
300: Pinku
400: Suman
500: Arohi

在上面的示例中,operator[]函数用于更改与它们的键值相关联的值。

示例4

让我们看一个简单的示例来区分operator[]和at()之间的区别。

#include <iostream>
#include <string>
#include <map>

using namespace std;

int main ()
{
  map<char,string> mp = {
                { 'a',"Java"},
                { 'b', "C++"  },
                { 'c', "Python" }};

    cout<<endl<<mp['a'];
    cout<<endl<<mp['b'];
    cout<<endl<<mp['c'];

     mp['d'] = "SQL";   
  /* since there is no key with value 'd' in the map, 
        it insert a key-value pair in map with key 'd' and value = "SQL" */

     cout<<endl<<mp['d'];

    try {
        mp.at('z'); 
          // since there is no key with value z in the map, it throws an exception 


    } catch(const out_of_range &e) {
        cout<<endl<<"\nOut of Range Exception at "<<e.what();
    }
return 0;
}

输出:

Java
C++
Python
SQL

Out of Range Exception at map::at

在上面的示例中,当我们使用at()函数时,它会抛出一个out_of_range异常,因为在map中没有键值为z的。而当我们使用operator[]并在键值为d时添加一个元素,由于在map中没有键值为’d’的键,它会在map中插入一个键为’d’,值为”SQL”的键值对。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程