C++ map operator[]
C++ map operator[]函数
C++映射 operator [] 函数用于通过给定的键访问映射中的元素值。
与 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 []函数用于访问地图元素。
示例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;
}
输出:
在上面的示例中,使用算子[]在初始化后使用关联的键值添加元素。
示例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()函数时,由于在映射中不存在带有值z的键,并且当我们使用operator []并在键值d中添加元素时,它会引发out_of_range异常,映射中没有值为" d"的键,它将在映射中插入一个键为" d"且值为" SQL"的键-值对。