sliver__

[Mastering C++ Programming] - Multimap 본문

CS/C++

[Mastering C++ Programming] - Multimap

sliver__ 2022. 12. 5. 22:34
728x90
  • multimap은 multimap 컨테이너가 동일한 키로 여러 값을 저장할 수 있다는 점을 제외하면 정확히 맵과 동일하게 작동합니다.

 

  • 예제 코드는 아래와 같다.
#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;
    pos++;
    ++index;
    if ( index == count )
      break;
  }

  return 0;
}

 

  • 결과는 아래와 같다.
Mobile number of Nitesh is 6234324343
Mobile number of Nitesh is 5534327346
728x90
Comments