STL Container - Associate Container

Associate Container(์—ฐ๊ด€ ์ปจํ…Œ์ด๋„ˆ)๋Š” ํ‚ค์™€ ๊ฐ’์ฒ˜๋Ÿผ ๊ด€๋ จ์žˆ๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ํ•˜๋‚˜์˜ ์Œ์œผ๋กœ ์ €์žฅํ•˜๋Š” ์ปจํ…Œ์ด๋„ˆ์ด๋‹ค. ํ‚ค์™€ ๊ฐ’์„ ์ด์šฉํ•˜์—ฌ ์š”์†Œ๋“ค์— ๋Œ€ํ•ด ๋น ๋ฅธ ์ ‘๊ทผ์ด ๊ฐ€๋Šฅํ•˜๋‹ค. ํ•˜์ง€๋งŒ ์‚ฝ์ž…๋˜๋Š” ์š”์†Œ์˜ ์œ„์น˜๋ฅผ ์ง€์ •ํ•  ์ˆ˜ ์—†๋‹ค.

๋ณดํ†ต balanced binary search tree๋‚˜ hash table์„ ์ด์šฉํ•˜์—ฌ ๊ตฌํ˜„ํ•œ๋‹ค.

set & multiset

์ง‘ํ•ฉ(set)์€ ์ €์žฅํ•˜๋Š” ๋ฐ์ดํ„ฐ ๊ทธ ์ž์ฒด๋ฅผ ํ‚ค๋กœ ์‚ฌ์šฉํ•˜๋Š” ๊ฐ€์žฅ ๋‹จ์ˆœํ•œ ์—ฐ๊ด€ ์ปจํ…Œ์ด๋„ˆ์ด๋‹ค. set์™€ ๋‹ฌ๋ฆฌ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌ๋œ ์œ„์น˜์— ์š”์†Œ๋ฅผ ์‚ฝ์ž…ํ•˜๋ฏ€๋กœ ๊ฒ€์ƒ‰์†๋„๊ฐ€ ๋งค์šฐ ๋น ๋ฅด๋‹ค.

์ง‘ํ•ฉ์—์„œ ํ‚ค๋Š” ์œ ์ผํ•ด์•ผํ•˜๋ฏ€๋กœ, ํ‚ค์˜ ์ค‘๋ณต์„ ํ—ˆ์šฉํ•˜์ง€ ์•Š๋Š”๋‹ค.

๋ฉ€ํ‹ฐ์ง‘ํ•ฉ(multiset)์€ ํ‚ค์˜ ์ค‘๋ณต์„ ํ—ˆ์šฉํ•˜๋ฉฐ, ๊ฐ™์€ ๊ฐ’์„ ์—ฌ๋Ÿฌ๋ฒˆ ์ €์žฅํ•  ์ˆ˜ ์žˆ๋‹ค.

#include <set>

๊ตฌ์กฐ

set<T> ๊ฐ์ฒด๋ช…;
multiset<T> ๊ฐ์ฒด๋ช…;
set<int> st;
multiset<int> ms;

์ƒ์„ฑ์ž

์ƒ์„ฑ์ž

์„ค๋ช…

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 ์กฐ๊ฑด์ž๋ฅผ ์ด์šฉํ•œ๋‹ค.

๋ฉค๋ฒ„ํ•จ์ˆ˜

ํ•จ์ˆ˜

์„ค๋ช…

s.clear()

s์˜ ๋ชจ๋“  ์›์†Œ๋ฅผ ์ œ๊ฑฐ

n=s.count(k)

์›์†Œ k์˜ ๊ฐœ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

s.empty()

v๊ฐ€ ๋น„์—ˆ๋Š”์ง€ ์กฐ์‚ฌ

p=s.begin()

p๋Š” s์˜ ์ฒซ ์›์†Œ๋ฅผ ๊ฐ€๋ฆฌํ‚จ๋‹ค.

p=s.end()

p๋Š” s์˜ ๋์„ ํ‘œ์‹ํ•˜๋Š” ๋ฐ˜๋ณต์ž

pr=s.equeal_range(k)

pr๋Š” ์›์†Œ k์˜ ๋ฐ˜๋ณต์ž ๊ตฌ๊ฐ„์ธ pair ๊ฐ์ฒด

q=s.erase(p)

p๊ฐ€ ๊ฐ€๋ฆฌํ‚ค๋Š” ์›์†Œ๋ฅผ ์ œ๊ฑฐํ•œ๋‹ค. q๋Š” ๋‹ค์Œ ์›์†Œ๋ฅผ ๊ฐ€๋ฆฌํ‚จ๋‹ค.

q=s.erase(b,e)

๋ฐ˜๋ณต์ž ๊ตฌ๊ฐ„ [b,e)์˜ ๋ชจ๋“  ์›์†Œ๋ฅผ ์ œ๊ฑฐํ•œ๋‹ค. q๋Š” ๋‹ค์Œ ์›์†Œ๋ฅผ ๊ฐ€๋ฆฌํ‚จ๋‹ค.

p=s.find(k)

p๋Š” k์›์†Œ์˜ ์œ„์น˜๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋Š” ๋ฐ˜๋ณต์ž์ด๋‹ค.

pr=s.insert(k)

s ์ปจํ…Œ์ด๋„ˆ์— k๋ฅผ ์‚ฝ์ž…ํ•œ๋‹ค. pr์€ ์‚ฝ์ž…ํ•œ ์›์†Œ๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋Š” ๋ฐ˜๋ณต์ž์™€ ์„ฑ๊ณต ์—ฌ๋ถ€์˜ bool์ธ pair ๊ฐ์ฒด์ด๋‹ค.

q=s.insert(p,x)

p๊ฐ€ ๊ฐ€๋ฆฌํ‚ค๋Š” ์œ„์น˜์— k๊ฐ’์„ ์‚ฝ์ž…ํ•œ๋‹ค. q๋Š” ์‚ฝ์ž…ํ•œ ์›์†Œ๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋Š” ๋ฐ˜๋ณต์ž๋‹ค.

s.insert(b,e)

๋ฐ˜๋ณต์ž ๊ตฌ๊ฐ„ [b,e)์˜ ์›์†Œ๋ฅผ ์‚ฝ์ž…ํ•œ๋‹ค.

pred = s.key_comp()

pred๋Š” s์˜ key ์ •๋ ฌ๊ธฐ์ค€์ธ ์กฐ๊ฑด์ž์ด๋‹ค.(key_compare ํƒ€์ž…)

pred = s.value_comp()

pred๋Š” s์˜ value ์ •๋ ฌ๊ธฐ์ค€์ธ ์กฐ๊ฑด์ž์ด๋‹ค.(value_compare ํƒ€์ž…)

p = s.lower_bound(k)

p๋Š” k์˜ ์‹œ์ž‘๊ตฌ๊ฐ„์„ ๊ฐ€๋ฆฌํ‚ค๋Š” ๋ฐ˜๋ณต์ž์ด๋‹ค.

p = s.upper_bound(k)

p๋Š” k์˜ ๋ ๊ตฌ๊ฐ„์„ ๊ฐ€๋ฆฌํ‚ค๋Š” ๋ฐ˜๋ณต์ž์ด๋‹ค.

x=s.max_size()

x๋Š” s๊ฐ€ ๋‹ด์„ ์ˆ˜ ์žˆ๋Š” ์ตœ๋Œ€ ์›์†Œ์˜ ๊ฐœ์ˆ˜(๋ฉ”๋ชจ๋ฆฌ์˜ ํฌ๊ธฐ)

p=s.rbegin()

p๋Š” s์˜ ์—ญ ์ˆœ์ฐจ์—ด์˜ ์ฒซ ์›์†Œ๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋Š” ๋ฐ˜๋ณต์ž๋‹ค.

p=s.rend()

p๋Š” s์˜ ์—ญ ์ˆœ์ฐจ์—ด์˜ ๋์„ ํ‘œ์‹ํ•˜๋Š” ๋ฐ˜๋ณต์ž

s.size()

s์˜ ์›์†Œ ๊ฐฏ์ˆ˜

s.swap(s2)

s์™€ s2๋ฅผ swapํ•œ๋‹ค.

์—ฐ๊ด€ ์ปจํ…Œ์ด๋„ˆ๋Š” ์ •๋ ฌ ๊ธฐ์ค€์ด ์žˆ์œผ๋ฏ€๋กœ insert()์— ์˜ํ•ด ์‚ฝ์ž…๋œ ์›์†Œ๋Š” ์ž๋™ ์ •๋ ฌ๋œ๋‹ค. set์—์„œ ์›์†Œ๋ฅผ key๋ผ ํ•˜๋ฉฐ, ์ด ํ‚ค๋ฅผ ๋น„๊ตํ•˜์—ฌ ๋‚ด๋ถ€ ์›์†Œ๋ฅผ ์ •๋ ฌํ•œ๋‹ค.

์˜ˆ์‹œ

  • set

#include <iostream>
#include <set>

using namespace std;

int main(void){
    set<int> s;

    pair<set<int>::iterator, bool> pr;
    pr = s.insert(50);

    if( pr.second == true ){
        cout << *pr.first << " ์‚ฝ์ž… ์„ฑ๊ณต" << endl;
    }else{
        cout << *pr.first << " ์‚ฝ์ž… ์‹คํŒจ" << endl;
    }

    s.insert(40);
    s.insert(80);

    set<int>::iterator iter;
    // insert๋กœ ์‚ฝ์ž…ํ•ด ์ •๋ ฌ๋œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.
    for(iter=s.begin();iter!=s.end();++iter){
        cout << *iter << ' ';
    }
    cout << endl;

    pr = s.insert(50);
    if( pr.second == true ){
        cout << *pr.first << " ์‚ฝ์ž… ์„ฑ๊ณต" << endl;
    }else{
        cout << *pr.first << " ์‚ฝ์ž… ์‹คํŒจ" << endl;
    }
    copy(s.begin(), s.end(), ostream_iterator<int>(cout, " "));
    cout << endl;
    // 50์œ„์น˜์— 60์‚ฝ์ž…
    s.insert(pr.first, 60);
    copy(s.begin(), s.end(), ostream_iterator<int>(cout, " "));
    cout << endl;

    // ๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌ
    set<int, greater<int> > s2;
    s2.insert(50);
    s2.insert(30);
    s2.insert(80);
    s2.insert(10);

    for(iter=s2.begin();iter!=s2.end();++iter){
        cout << *iter << ' ';
    }
    cout << endl;

    // key_compare(), value_compare()
    cout << "key_compare less : " << typeid(s.key_comp()).name() << endl;
    cout << "key_compare greater : " << typeid(s2.key_comp()).name() << endl;

    cout << "value_compare less : " << typeid(s.value_comp()).name() << endl;
    cout << "value_compare greater : " << typeid(s2.value_comp()).name() << endl;
}
  • multiset

#include <iostream>
#include <set>

using namespace std;

int main(void){
    multiset<int> ms;

    ms.insert(50);
    ms.insert(30);
    ms.insert(40);
    ms.insert(30);
    ms.insert(80);
    ms.insert(50);
    ms.insert(30);
    ms.insert(50);
    ms.insert(10);

    multiset<int>::iterator iter;
    for(iter=ms.begin(); iter!=ms.end(); ++iter){
        cout << *iter << " ";
    }
    cout << endl;

    cout << "30 count : " << ms.count(30) << endl;


    // ์›์†Œ 30์˜ ์ฒซ๋ฒˆ์งธ ์œ„์น˜
    multiset<int>::iterator lower_iter;
    lower_iter = ms.lower_bound(50);
    // ์›์†Œ 30์˜ ๋งˆ์ง€๋ง‰ ์œ„์น˜ ๋‹ค์Œ ์œ„์น˜
    multiset<int>::iterator upper_iter;
    upper_iter = ms.upper_bound(50);
    cout << "lower iter : " << *lower_iter << ", upper_iter : " << *upper_iter << endl;

    pair<multiset<int>::iterator, multiset<int>::iterator> iter_pair;
    iter_pair = ms.equal_range(30);

    for(iter = iter_pair.first; iter != iter_pair.second; ++iter){
        cout << *iter << ' ';
    }
    cout << endl;

    // ํ•ด๋‹น ์š”์†Œ ์ „์ฒด ์‚ญ์ œ
    ms.erase(50);
    copy(ms.begin(), ms.end(), ostream_iterator<int>(cout, " "));
}

map & multimap

map์€ ํ‚ค์™€ ๊ฐ’์˜ ์Œ์œผ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ๊ด€๋ฆฌํ•˜๋Š” ์ปจํ…Œ์ด๋„ˆ์ด๋‹ค. ์ง‘ํ•ฉ ์ปจํ…Œ์ด๋„ˆ์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์ •๋ ฌ๋œ ์œ„์น˜์— ์š”์†Œ๋ฅผ ์‚ฝ์ž…ํ•˜๋ฏ€๋กœ ๊ฒ€์ƒ‰ ์†๋„๊ฐ€ ๋งค์šฐ ๋น ๋ฅด๋‹ค.

map์—์„œ ํ‚ค๋Š” ์œ ์ผํ•ด์•ผํ•˜๋ฏ€๋กœ, ํ•˜๋‚˜์˜ ํ‚ค์— ํ•˜๋‚˜์˜ ๊ฐ’๋งŒ์ด ์—ฐ๊ฒฐ๋  ์ˆ˜ ์žˆ๋‹ค. multimap์€ ๊ฐ’์˜ ์ค‘๋ณต์„ ํ˜€์šฉํ•˜๋ฏ€๋กœ, ํ•˜๋‚˜์˜ ํ‚ค์— ์—ฌ๋Ÿฌ ๊ฐœ์˜ ๊ฐ’๊ณผ ์—ฐ๊ฒฐ๋  ์ˆ˜ ์žˆ๋‹ค.

#include <map>

๊ตฌ์กฐ

map<T> ๊ฐ์ฒด๋ช…;
multimap<T> ๊ฐ์ฒด๋ช…;

์˜ˆ์‹œ

map์€ set๊ณผ ๊ฐ™์€ ์ธํ„ฐํŽ˜์ด์Šค ๋ฉค๋ฒ„ ํ•จ์ˆ˜๋ฅผ ์ œ๊ณตํ•˜๋ฉฐ, ํ…œํ”Œ๋ฆฟ ํ˜•์‹๊ณผ ๋‚ด์žฅ ๋ฉค๋ฒ„ ํ˜•์‹๋งŒ์ด ์•ฝ๊ฐ„ ์ฐจ์ด๋ฅผ ๋ณด์ธ๋‹ค.

  • map์€ []์—ฐ์‚ฐ์ž๋ฅผ ์ œ๊ณตํ•˜์—ฌ key์— ํ•ด๋‹นํ•˜๋Š” ์›์†Œ์˜ value์— ์‰ฝ๊ฒŒ ์ ‘๊ทผํ•˜๊ฑฐ๋‚˜ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๋‹ค.

  • map

#include <iostream>
#include <map>

using namespace std;

int main(void){

    map<string, int> m;

    m.insert(pair<string,int>("์ˆ˜ํ•™",100));
    m.insert(pair<string,int>("๊ตญ์–ด",80));

    pair<string,int> p("์˜์–ด",50);
    m.insert(p);

    map<string, int>::iterator iter;
    for(iter = m.begin(); iter != m.end(); ++iter){
        cout << (*iter).first << " : " << (*iter).second << endl;
    }

    m["์˜์–ด"] = 90;

    for(iter = m.begin(); iter != m.end(); ++iter){
        cout << iter->first << " : " << iter->second << endl;
    }

    pair<map<string,int>::iterator, bool> pr;

    pr = m.insert(pair<string, int>("๊ณผํ•™", 60));

    if(pr.second){
        cout << pr.first->first << " : " << pr.first->second << endl;
    }
    return 0;
}
  • multimap

#include <iostream>
#include <map>

using namespace std;

int main(void){
    multimap<string, int> mm;

    mm.insert(pair<string, int>("๊ตญ์–ด", 30));
    mm.insert(pair<string, int>("์ˆ˜ํ•™", 100));
    mm.insert(pair<string, int>("์ˆ˜ํ•™", 90));
    mm.insert(pair<string, int>("๊ตญ์–ด", 60));

    multimap<string, int>::iterator iter;
    for(iter=mm.begin(); iter!=mm.end(); ++iter){
        cout << iter->first << " : " << iter->second << endl;
    }
    cout << endl;

    cout << "count('๊ตญ์–ด') : "<< mm.count("๊ตญ์–ด") << endl;

    mm.insert(pair<string, int>("์˜์–ด", 60));
    mm.insert(pair<string, int>("๊ณผํ•™", 80));

    map<string, int>::iterator lower_iter;
    map<string, int>::iterator upper_iter;

    lower_iter = mm.lower_bound("์ˆ˜ํ•™");
    // upper๋Š” ๊ฐ€์žฅ ํฐ ์ˆ˜ํ•™ ๋‹ค์Œ!
    upper_iter = mm.upper_bound("์ˆ˜ํ•™");

    cout << "lower " << lower_iter->first << " : " << lower_iter->second << endl;
    cout << "upper " << upper_iter->first << " : " << upper_iter->second << endl;

    pair<map<string, int>::iterator, map<string,int>::iterator> iter_pair;
    iter_pair = mm.equal_range("์ˆ˜ํ•™");

    for(iter=iter_pair.first; iter!=iter_pair.second;++iter){
        cout << iter->first << " : " << iter->second << endl;
    }
    return 0;
}

unordered associate container

์ˆœ์„œ๊ฐ€ ์ง€์ •๋˜์ง€ ์•Š์€ ์—ฐ๊ด€ ์ปจํ…Œ์ด๋„ˆ๋Š” ์ˆœ์„œ๊ฐ€ ์ง€์ •๋œ ๊ธฐ์กด ์—ฐ๊ด€ ์ปจํ…Œ์ด๋„ˆ์™€ ๊ฐ™์€ ๋™์ž‘์„ ํ•œ๋‹ค. ๊ธฐ์กด์˜ ์—ฐ๊ด€ ์ปจํ…Œ์ด๋„ˆ๋Š” tree ๊ตฌ์กฐ๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ๋™์ž‘ํ•˜์ง€๋งŒ, ์ด ์ปจํ…Œ์ด๋„ˆ๋Š” hash table์„ ๊ธฐ๋ฐ˜์œผ๋กœ ๋™์ž‘ํ•œ๋‹ค.

๋”ฐ๋ผ์„œ, ์š”์†Œ์˜ ์ถ”๊ฐ€, ์‚ญ์ œ ์†๋„๊ฐ€ ๋นจ๋ผ์กŒ์œผ๋ฉฐ, ๋‹ค์–‘ํ•œ ๊ฒ€์ƒ‰ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋˜์—ˆ๋‹ค. ์ˆœ์„œ๊ฐ€ ์ง€์ •๋œ ์—ฐ๊ด€ ์ปจํ…Œ์ด๋„ˆ๋Š” ์–‘๋ฐฉํ–ฅ ๋ฐ˜๋ณต์ž๋ฅผ ์ง€์›ํ•˜์ง€๋งŒ, ์ด ์ปจํ…Œ์ด๋„ˆ๋Š” ์ˆœ๋ฐ˜ํ–ฅ ๋ฐ˜๋ณต์ž๋งŒ์„ ์ง€์›ํ•œ๋‹ค.

  • unordered_set

  • unordered_multiset

  • unordered_map

  • unordered_multimap

์ฐธ์กฐ

Last updated