C++ map at() 函数

C++ map at() 函数

C++ map at() 函数用于通过给定的 键值 访问map中的元素。如果访问的键不在map中,则抛出 out_of_range 异常。

语法

假设键值为 k ,则语法应为:

mapped_type& at (const key_type& k);
const mapped_type& at (const key_type& k) const;

参数

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

返回值

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

示例1

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

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

using namespace std;

int main ()
{
  map<string,int> m = {
                { "A", 10 },
                { "B", 20 },
                { "C", 30 } };

  for (auto& x: m) {
    cout << x.first << ": " << x.second << '\n';
  }
  return 0;
}

输出:

A: 10
B: 20   
C: 30

在上述代码中,at() 函数被用于访问 map 的元素。

示例2

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

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

using namespace std;

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

  mymap.at(101) = "Java"; 
  mymap.at(102) = "T";
  mymap.at(103) = "Point";


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

  return 0;
}

输出:

JavaTPoint 

在上面的示例中,使用at()函数可以在使用关联键值初始化后添加元素。

示例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.at(100) = "Nidhi"; // changes the value associated with key 100 to Nidhi
  mymap.at(300) = "Pinku"; // changes the value associated with key 300 to Pinku
  mymap.at(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

在上面的示例中,at()函数被用来改变与它们的键值相关联的值。

示例4

让我们看一个简单的示例来处理“范围超出”的异常。

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

using namespace std;

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

    cout<<endl<<mp.at('a');
    cout<<endl<<mp.at('b');
    cout<<endl<<mp.at('c');

    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<<"Out of Range Exception at "<<e.what();
}

输出:

Java
C++
Python
Out of Range Exception at map::at

上面的示例抛出了一个out_of_range异常,因为在map中没有值为z的键。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程