官术网_书友最值得收藏!

  • Mastering C++ Programming
  • Jeganathan Swaminathan
  • 149字
  • 2021-07-02 18:28:53

Multimap

A multimap works exactly as a map, except that a multimap container will allow multiple values to be stored with the same key.

Let's explore the multimap container with a simple example:

#include <iostream>
#include <map>
#include <vector>
#include <iterator>
#include <algorithm>
using namespace std;

int main() {
multimap< string, long > contacts = {
{ "Jegan", 2232342343 },
{ "Meena", 3243435343 },
{ "Nitesh", 6234324343 },
{ "Sriram", 8932443241 },
{ "Nitesh", 5534327346 }
};

auto pos = contacts.find ( "Nitesh" );
int count = contacts.count( "Nitesh" );
int index = 0;

while ( pos != contacts.end() ) {
cout << "\nMobile number of " << pos->first << " is " <<
pos->second << endl;
++index;
if ( index == count )
break;
}

return 0;
}

The program can be compiled and the output can be viewed with the following commands:

g++ main.cpp -std=c++17

./a.out

The output of the program is as follows:

Mobile number of Nitesh is 6234324343
Mobile number of Nitesh is 5534327346
主站蜘蛛池模板: 天峻县| 彭水| 富川| 文水县| 临江市| 德令哈市| 永州市| 桃源县| 榆树市| 隆德县| 石首市| 定襄县| 南乐县| 澜沧| 义马市| 北川| 凤台县| 思茅市| 长海县| 安岳县| 辽阳市| 七台河市| 永靖县| 广西| 凤台县| 分宜县| 桂东县| 万宁市| 乌苏市| 许昌县| 砀山县| 丰顺县| 米脂县| 延长县| 拉孜县| 盐池县| 桐梓县| 若尔盖县| 南雄市| 泰安市| 莱西市|