C++中的关联数组

关联数组也称为映射或字典。在C++中。这是一种特殊的数组,可以在其中创建索引 数字的 任何其他数据类型 i、 e可以是数字0、1、2、3。。或者角色a,b,c,d…或者字符串极客,计算机… 这些索引被称为 钥匙 存储在该位置的数据称为 价值 . 所以在关联数组中我们有 (关键,价值) 一对 我们使用 STL地图 在C++中实现关联数组的概念。

null

图片[1]-C++中的关联数组-yiteyi-C++库

实例 : 现在我们必须打印计算机极客的标记,他们的名字和标记如下

  Name        Marks       Jessie       100       Suraj        91       Praveen      99          Bisa         78       Rithvik      84

CPP

// CPP program to demonstrate associative arrays
#include <bits/stdc++.h>
using namespace std;
int main()
{
// the first data type i.e string represents
// the type of key we want the second data type
// i.e int represents the type of values we
// want to store at that location
map<string, int > marks{ { "Rithvik", 78 },
{ "Suraj", 91 }, { "Jessie", 100 },
{ "Praveen", 99 }, { "Bisa", 84 } };
map<string, int >::iterator i;
cout << "The marks of all students are" << endl;
for (i = marks.begin(); i != marks.end(); i++)
cout << i->second << " ";
cout << endl;
// the marks of the students based on there names.
cout << "the marks of Computer geek Jessie are"
<< " " << marks["Jessie"] << endl;
cout << "the marks of geeksforgeeks contributor"
" Praveen are " << marks["Praveen"] << endl;
}


输出:

The marks of all students are84 100 99 78 91 the marks of Computer geek Jessie are 100the marks of geeksforgeeks 
 Praveen are 99

© 版权声明
THE END
喜欢就支持一下吧
点赞10 分享