C++ multiset


multiset을 사용하기 위해서는 <set> 헤더를 인클루드 해야한다.


중복된 데이터를 사용할 수 있는 것 외에는 set과 모두 같다.




 생성자

 

 set s

 빈 컨테이너 셋 s 생성

 set s(pred)

 s는 빈 컨테이너 이며, 정렬 기준은 pred 조건자를 사용

 set s(s2)

 셋 s는 셋 s2의 복사본

 set s(b, e)

 셋 s는 반복자 구간 [b, e)로 초기화된 원소를 가짐

 set s(b, e, pred)

 셋 s는 반복자 구간 [b, e)로 초기화된 원소를 가지며, 정렬 기준은 pred 조건자를 사용


생성자 사용 예제



#include <iostream>
#include <set>
#include <functional>
int main()
{
    std::multiset<int> first;                           // empty set of ints
 
    int myints[] = { 10203040502030405060 };
    std::multiset<int> second(myints, myints + 10);        // range
 
    std::multiset<int> third(second);                  // a copy of second
 
    std::multiset<int> fourth(second.begin(), second.end());  // iterator ctor.
 
    std::multiset<intstd::greater<int> > fifth = { 10203040502030405060 };
 
    std::cout << "first : ";
    for (auto it = first.begin(); it != first.end(); it++)
        std::cout << *it << ' ';
    std::cout << '\n';
 
    std::cout << "second : ";
    for (auto it = second.begin(); it != second.end(); it++)
        std::cout << *it << ' ';
    std::cout << '\n';
 
    std::cout << "third : ";
    for (auto it = third.begin(); it != third.end(); it++)
        std::cout << *it << ' ';
    std::cout << '\n';
 
    std::cout << "fourth : ";
    for (auto it = fourth.begin(); it != fourth.end(); it++)
        std::cout << *it << ' ';
    std::cout << '\n';
 
    std::cout << "fifth : ";
    for (auto it = fifth.begin(); it != fifth.end(); it++)
        std::cout << *it << ' ';
    std::cout << '\n';
 
    return 0;
}
cs


실행 결과




주요 멤버 함수

insert() 

 셋에 데이터 삽입

 erase()

 셋에서 원소 제거

 find()

 셋에서 원소의 위치 검색

 count()

 셋에서 원소의 개수 검색

 clear()

 셋의 모든 원소 제거


사용예제

insert()


#include <iostream>
#include <set>
 
int main()
{
    std::multiset<int> mymultiset;
    std::multiset<int>::iterator it;
 
    // set some initial values:
    for (int i = 1; i <= 5; i++) mymultiset.insert(i * 10);  // 10 20 30 40 50
 
    it = mymultiset.insert(25);
 
    it = mymultiset.insert(it, 27);    // max efficiency inserting
    it = mymultiset.insert(it, 29);    // max efficiency inserting
    it = mymultiset.insert(it, 24);    // no max efficiency inserting (24<29)
 
    int myints[] = { 5,10,15 };
    mymultiset.insert(myints, myints + 3);
 
    std::cout << "mymultiset contains:";
    for (it = mymultiset.begin(); it != mymultiset.end(); ++it)
        std::cout << ' ' << *it;
    std::cout << '\n';
 
    return 0;
}
cs



erase(), find()


#include <iostream>
#include <set>
 
int main ()
{
  std::multiset<int> mymultiset;
  std::multiset<int>::iterator it;
 
  // insert some values:
  mymultiset.insert (40);                            // 40
  for (int i=1; i<7; i++) mymultiset.insert(i*10);   // 10 20 30 40 40 50 60
 
  it=mymultiset.begin();
  it++;                                              //    ^
 
  mymultiset.erase (it);                             // 10 30 40 40 50 60
 
  mymultiset.erase (40);                             // 10 30 50 60
 
  it=mymultiset.find (50);
  mymultiset.erase ( it, mymultiset.end() );         // 10 30
 
  std::cout << "mymultiset contains:";
  for (it=mymultiset.begin(); it!=mymultiset.end(); ++it)
    std::cout << ' ' << *it;
  std::cout << '\n';
 
  return 0;
}
cs


count()


#include <iostream>
#include <set>
 
int main ()
{
  int myints[]={10,73,12,22,73,73,12};
  std::multiset<int> mymultiset (myints,myints+7);
 
  std::cout << "73 appears " << mymultiset.count(73<< " times in mymultiset.\n";
 
  return 0;
}
cs



참조: http://www.cplusplus.com/reference/set/multiset/

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

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

[STL] C++ STL multimap  (0) 2018.07.22
[STL] C++ STL map  (0) 2018.07.22
[STL] C++ STL set  (0) 2018.07.22
[STL] C++ STL string to int/float/double/long long  (0) 2018.07.19
[STL] C++ STL list  (0) 2018.07.19

+ Recent posts