[C++ Primer] C11 Associative Containers

Chapter 11. Associative Containers

Associative containers support efficient lookup and retrieval by a key.

  • Common types:
    • Elements ordered by key:
        1. map: key-value pairs
        1. set: key is the value
        1. multimap: map in which a key can appear multiple times
        1. multiset: set in which a key can appear multiple times
    • Unordered Collections:
        1. unordered_map: map organized by a hash function
        1. unordered_set: set organized by a hash function

11.2.3. The pair Type

pair<int, pair<int, int>> p = {1, {1, 2}};

pair<int, pair<int, int>> p(1,(1,2));

pair’s member are named as first and second.

p.first = 1;

p.second.first = 1;

p.second.first = 2;

How to return a pair object:

pair<string, int> process(vector<string> &v)
{
    // process v
    if (!v.empty()) {
        return {v.back(), v.back().size()}; // list initialize
    } else
        return pair<string, int>("empty", 0); // explicitly constructed return value
    }
}
If v isn’t empty, we return a pair composed of the last string in v and the size of
that string. Otherwise, we return empty and 0 as a pair.

自定义比较map

class A{
public:
    int a_;
    A(int a): a_(a){};
};

bool WannaBigger(const A& a, const A& b) {
    return a.a_> b.a_;
}

void func() {
    // key, value
    map<A, int, decltype(WannaBigger)*> map(WannaBigger);
    // 我们自定义了变量A,也要同时定义映射关系(comparator),map本身就是一种hash结构,他把key映射到空间上
    // 注意定义的map也要有WannaBigger!!!

    map.insert({A(1), 1});
    map.insert({A(2), 2});
    map.insert({A(3), 3});
    map.insert({A(4), 4});

    for (auto it = map.begin(); it != map.end(); it++) {
        cout << it->second << endl; // 4 3 2 1
    }
}

int main() {
    func();
    return 0;
}

operators:

c[k]: return element with key k, if not in c, adds a new element with key k.

c.at(k): checked access to the element with key k; throws an out_of_range exception if k not in c.

c.find(k): returns an iterator to the first element with key k. For the containers with unique key (not multimap or multiset), the result is always zero and one.

c,count(k): return the num of elements with key k. For the containers with unique key (not multimap or multiset), the result is always zero and one.

c.lower_bound(k): returns an iterator to the first element with key not less than k.

c.upper_bound(k): returns an iterator to the first element with key greater than k.

However, as we’ve just seen, using a subscript has an
important side effect: If that key is not already in the map, then subscript inserts an
element with that key!!!

Buckets for unordered containers

The unordered containers are organized as a collection of buckets, each of which
holds zero or more elements. These containers use a hash function to map elements
to buckets

Summary

Ordered containers use a comparison function to order the elements by key. By default, the comparison is the < operator on the keys. Unordered containers use the
key type’s == operator and an object of type hash<key_type> to organize their elements.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值