ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • C++ Primer CH11. Associative Containers
    c++ 2024. 1. 21. 16:27

    정렬하고 싶을 때,

    bool compareIsbn(const Sales_data &lhs, const Sales_data &rhs)
    {
    	return lhs.isbn() < rhs.isbn();
    }
    
    multiset<Sales_data, decltype(compareIsbn) *> bookstroe(compareIsbn);
    

     

    Pair

    pair<string, string> anon;
    pair<string, size_t> word_count;
    pair<string, vector<int>> line;
    p.first
    p.second
    
    

     

    pair 리턴하기

    pair<string, int>
    process(vector<string> &v)
    {
    	if (!v.empty())
    		return {v.back(), v.back().size()};
    	else
    		return pair<string, int>();
    }
    
    set<string>::key_type k1; // string
    set<string>::value_type v1; // string
    
    map<string, int>::key_type k1; // string
    map<string, int>::value_type v1: // pair<const string, int>
    map<string, int>::mapped_type m1; // int
    

    11.3.1 Associative Container Iterators

    이터레이터를 역참조하면, value_type에 해당하는 값이 참조된다.

     

    sets(ordered, unordered)의 이터레이터로 key값을 바꿀 수 없다.(read-only)

    set<int> iset = {0, 1, 2, 3, 4};
    set<int>::iterator set_it = iset.begin();
    if (set_it != iset.end()) {
    	*set_it = 100; // set의 keys는 read_only다.
    	cout << *set_it << endl;
    

     

     

     

    insert 사용법

    1. map인 경우
    int main() {
        map<string, size_t> word_count;
        string word;
        while (cin >> word) {
            auto ret = word_count.insert({word, 1});
    
            if (!ret.second) {
                ++ret.first->second;
            }
        }
    
        for (auto &w : word_count) {
            cout << "word: " << w.first << ", " << w.second << endl;
        }
    }
    
    

     

    2. multimap인 경우

    multimap<string, string> authors;
    authors.insert({"Barth, John", "Sot-Weed Factor"});
    authors.insert({"Barth, John", "Hi Bar"});
    

     

    Erasing Elements

    unique key container: erase() → return 0 or 1

    multi key container: return value could be greater than 1

    if (word_count.erase(removal_word))
    	cout << "OK" << endl;
    else 
    	cout << "NOT OK" << endl;
    
    c.erase(k)
    c.erase(p) // p는 이터레이터
    c.erase(b, e) // b,e는 이터레이터
    

     

    map도 subscript결과가 참조값이라서 ++map[idx] 된다.

     

    Accessing Elements

    unique key인 container인 경우, c.find(val) → iterator 를 사용하는게 좋지만,

    multiple key인 container인 경우, 같은 key에 대해 여러 value가 있을 수 있거나 여러 key가 있을 수 있으므로 c.count(key)를 사용하면 좋다

    set<int> iset = {0, 1, 2, 3, 4, 5};
    iset.find(1); // 1을 가리키는 이터레이터 반환
    iset.find(10); // iset.end()에 해당하는 이터레이터 반환
    iset.count(-1); // 0
    iset.count(1); // 1
    

     

    lower_bound and upper_bound

    일단, ordered에서만 사용 가능하다 → 이진탐색을 통해 이루어지므로.

    그리고, 찾고자 하는 키가 없을 땐 두 함수 둘다 같은 위치를 가리키는 이터레이터를 반환한다.

    이 위치는, 여기에 해당 쿼리키를 넣었을 때 ordered상태를 유지하도록 하는 위치이다.

    만약 키가 하나 존재하면, lower_bound는 해당 키를 가리키고, upper_bound는 해당 키 다음위치를 가리킨다.

    • 밀어내고 넣는 과정을 상상해라
    int main() {
        set<int> s = {1, 2, 3, 4, 5};
    
        auto it = s.lower_bound(2);
        auto it2 = s.upper_bound(2);
    
        cout << *it << " " << *it2 << endl;
    }
    // 결과: 2 3
    

    'c++' 카테고리의 다른 글

    C++ Primer CH13. Copy Control  (0) 2024.01.21
    C++ Primer CH12. Dynamic Memory  (0) 2024.01.21
    C++ Primer CH10. Generic Algorithms  (0) 2024.01.21
    C++ Primer CH9. Sequential Containers  (1) 2024.01.21
    C++ Primer CH8. The IO Library  (0) 2024.01.21
Designed by Tistory.