C++ multimap


multimap을 사용하기 위해서는 <map>을 인클루드 해야한다.


map에서 중복된 key가 가능하다는 차이점 외에는 다 같다.  라고 책에는 나와있는데 multimap은 중복된 key를 허용하기 때문에, 앞서 map과는 다르게 [] 사용시 multimap에서는 오류가 발생한다. 이 차이점 외에는 다 같은것이 맞는 것 같다.


생성자 사용 예제

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>
#include <map>
#include <functional>
 
int main()
{
    std::multimap<charint> first;
 
    first.insert(std::pair<charint>('a'10));
    first.insert(std::pair<charint>('b'30));
    first.insert(std::pair<charint>('c'50));
    first.insert(std::pair<charint>('d'70));
 
    std::multimap<charint> second(first.begin(), first.end());
 
    std::multimap<charint> third(second);
 
    std::multimap<charintstd::greater<int> > fourth = { { 'a'10 },{ 'b'30 },{ 'c'50 },{ 'd'70 } };
    
    std::cout << "first : ";
    for (auto it = first.begin(); it != first.end(); it++)
        std::cout << (*it).first << ' ' << (*it).second << ' ';
    std::cout << '\n';
 
    std::cout << "second : ";
    for (auto it = second.begin(); it != second.end(); it++)
        std::cout << (*it).first << ' ' << (*it).second << ' ';
    std::cout << '\n';
 
    std::cout << "third : ";
    for (auto it = third.begin(); it != third.end(); it++)
        std::cout << (*it).first << ' ' << (*it).second << ' ';
    std::cout << '\n';
 
    std::cout << "fourth : ";
    for (auto it = fourth.begin(); it != fourth.end(); it++)
        std::cout << (*it).first << ' ' << (*it).second << ' ';
    std::cout << '\n';
 
    return 0;
}
 
cs


실행 결과


멤버 함수 사용 예제

insert()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
#include <map>
 
int main ()
{
  std::multimap<char,int> mymultimap;
  std::multimap<char,int>::iterator it;
 
  // first insert function version (single parameter):
  mymultimap.insert ( std::pair<char,int>('a',100) );
  mymultimap.insert ( std::pair<char,int>('z',150) );
  it=mymultimap.insert ( std::pair<char,int>('b',75) );
 
  // second insert function version (with hint position):
  mymultimap.insert (it, std::pair<char,int>('c',300));  // max efficiency inserting
  mymultimap.insert (it, std::pair<char,int>('z',400));  // no max efficiency inserting
 
  // third insert function version (range insertion):
  std::multimap<char,int> anothermultimap;
  anothermultimap.insert(mymultimap.begin(),mymultimap.find('c'));
 
  // showing contents:
  std::cout << "mymultimap contains:\n";
  for (it=mymultimap.begin(); it!=mymultimap.end(); ++it)
    std::cout << (*it).first << " => " << (*it).second << '\n';
 
  std::cout << "anothermultimap contains:\n";
  for (it=anothermultimap.begin(); it!=anothermultimap.end(); ++it)
    std::cout << (*it).first << " => " << (*it).second << '\n';
 
  return 0;
}
cs


erase()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
#include <map>
 
int main ()
{
  std::multimap<char,int> mymultimap;
 
  // insert some values:
  mymultimap.insert(std::pair<char,int>('a',10));
  mymultimap.insert(std::pair<char,int>('b',20));
  mymultimap.insert(std::pair<char,int>('b',30));
  mymultimap.insert(std::pair<char,int>('c',40));
  mymultimap.insert(std::pair<char,int>('d',50));
  mymultimap.insert(std::pair<char,int>('d',60));
  mymultimap.insert(std::pair<char,int>('e',70));
  mymultimap.insert(std::pair<char,int>('f',80));
 
  std::multimap<char,int>::iterator it = mymultimap.find('b');
 
  mymultimap.erase (it);                     // erasing by iterator (1 element)
 
  mymultimap.erase ('d');                    // erasing by key (2 elements)
 
  it=mymultimap.find ('e');
  mymultimap.erase ( it, mymultimap.end() ); // erasing by range
 
  // show content:
  for (it=mymultimap.begin(); it!=mymultimap.end(); ++it)
    std::cout << (*it).first << " => " << (*it).second << '\n';
 
  return 0;
}
cs


count()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <map>
 
int main ()
{
  std::multimap<char,int> mymm;
 
  mymm.insert(std::make_pair('x',50));
  mymm.insert(std::make_pair('y',100));
  mymm.insert(std::make_pair('y',150));
  mymm.insert(std::make_pair('y',200));
  mymm.insert(std::make_pair('z',250));
  mymm.insert(std::make_pair('z',300));
 
  for (char c='x'; c<='z'; c++)
  {
    std::cout << "There are " << mymm.count(c) << " elements with key " << c << ":";
    std::multimap<char,int>::iterator it;
    for (it=mymm.equal_range(c).first; it!=mymm.equal_range(c).second; ++it)
      std::cout << ' ' << (*it).second;
    std::cout << '\n';
  }
 
  return 0;
}
cs



참조: http://www.cplusplus.com/reference/map/multimap/

참고문헌: 뇌를 자극하는 C++ STL

'컴퓨터공학 > STL' 카테고리의 다른 글

[STL] C++ STL algorithm2  (0) 2018.07.30
[STL] C++ STL algorithm1  (0) 2018.07.30
[STL] C++ STL map  (0) 2018.07.22
[STL] C++ STL multiset  (0) 2018.07.22
[STL] C++ STL set  (0) 2018.07.22

+ Recent posts