본문 바로가기
[C++] Data Structure & Algorithm/STL

[STL] set, multimap, multiset

by song.ift 2023. 5. 20.

set

#include <iostream>
#include <set>
#include <multimap>
#include <multiset>

using namespace std;

주제 : set, multimap, multiset

int main()
{
    // (key = value)
    set<int> s;
    
    // 넣고
    s.insert(10);
    s.insert(20);
    s.insert(30);
    s.insert(40);
    
    // 뺴고
    s.erase(40);
    
    // 찾고
    set<int>::iterator findit = s.find(50);
    if (findit == s.end())
        cout << "못 찾음" << endl;
    else
        cout << "찾음" << endl;
    
    // 순회하고
    for (set<int>::iterator it = s.begin(); it != s.end(); ++it)
        cout << (*it) << endl;
}

 

multimap

#include <iostream>
#include <set>
#include <multimap>
#include <multiset>

using namespace std;

주제 : set, multimap, multiset

int main()
{
    multimap<int, int> mm;
    
    // 넣고
    mm.insert(make_pair(1, 100));
    mm.insert(make_pair(1, 200));
    mm.insert(make_pair(1, 300));
    mm.insert(make_pair(2, 400));
    mm.insert(make_pair(2, 500));
    
    // mm[1] = 500; // map과는 다르게 이런식으로 데이터 삽입이 안됨.
    
    // 빼고
    unsigned int count = mm.erase(1); // 1의 데이터가 총 3이므로 3.
    
    // 찾고
    multimap<int, int>::iterator ifFind = mm.find(1); // key값 1인 첫번째 데이터.
    if (itFind != mm.end())
        mm.erase(itFind);
    
    pair<multimap<int, int>::iterator, multimap<int, int>::iterator> itPair;
    itPair = mm.equal_range(1);
    
    for (multimap<int, int>::iterator it = itPair.first; it != itPair.second; ++it)
        cout << it->first << " " << it->second << endl;
        
    multimap<int, int>::iterator itBegin = mm.lower_bound();
    multimap<int, int>::iterator itEnd = mm.upper_bound();
    
    for (multimap<int, int>::iterator it = itBegin; it != itEnd; ++it)
        cout << it->first << " " << it->second << endl;
}

 

multiset

#include <iostream>
#include <set>
#include <multimap>
#include <multiset>

using namespace std;

주제 : set, multimap, multiset

int main()
{
    multiset<int, int> ms;
    
    // 넣고
    ms.insert(100);
    ms.insert(100);
    ms.insert(100);
    ms.insert(200);
    ms.insert(200);
    
    // 찾고
    multiset<int>::iterator ifFind = ms.find(100);

    pair<multiset<int, int>::iterator, multimap<int, int>::iterator> itPair;
    itPair = ms.equal_range(100);
    
    for (multiset<int, int>::iterator it = itPair.first; it != itPair.second; ++it)
        cout << *it << endl;
        
    multiset<int>::iterator itBegin = mm.lower_bound(100);
    multiset<int>::iterator itEnd = mm.upper_bound(100);
    
    for (multiset<int, int>::iterator it = itBegin; it != itEnd; ++it)
        cout << *it << endl;
}

'[C++] Data Structure & Algorithm > STL' 카테고리의 다른 글

[STL] algorithm  (0) 2023.05.22
[STL] map  (1) 2023.05.19
[STL] deque  (0) 2023.05.19
[STL] list #2  (0) 2023.05.19
[STL] list #1  (0) 2023.05.15

댓글