C++ 关联容器

1. 有序容器

#include <map>
#include <set>
  • C++ STL 中的有序关联容器分为 mapmultimapsetmultiset
  • mapset 容器会保证元素的互异性,即不能存储多个相同的元素;而 multimapmultiset 则允许存储多个相同的元素。
  • mapmultimapsetmultiset 均是使用红黑树来实现的。
  • 通过 begin(), end() 返回的迭代器来遍历容器时,它会按序进行遍历!

map,multimap

template < class Key,                                     // map::key_type
           class T,                                       // map::mapped_type
           class Compare = less<Key>,                     // map::key_compare
           class Alloc = allocator<pair<const Key,T> >    // map::allocator_type
           > class map;
template < class Key,                                     // multimap::key_type
           class T,                                       // multimap::mapped_type
           class Compare = less<Key>,                     // multimap::key_compare
           class Alloc = allocator<pair<const Key,T> >    // multimap::allocator_type
           > class multimap;

元素类型为 pair<const Key, T>,键为常量!


set,multiset

template < class T,                        // set::key_type/value_type
           class Compare = less<T>,        // set::key_compare/value_compare
           class Alloc = allocator<T>      // set::allocator_type
           > class set;
template < class T,                        // multiset::key_type/value_type
           class Compare = less<T>,        // multiset::key_compare/value_compare
           class Alloc = allocator<T> >    // multiset::allocator_type
           > class multiset;

返回的迭代器指向的元素类型为 const value_type,即无法修改容器中的元素!


自定义键类型

struct Key {
	int key;
	int value;
};

struct KeyComparator {
	bool operator()(const Key& k1, const Key& k2) const {
		return k1.key < k2.key;
	}
};

int main() {
	map<Key, int, KeyComparator> mi;

	Key key{ 1, 2 };
	mi.insert(make_pair(key, 1));

	return 0;
}

对于自定义 setmultiset 的值类型也同上。


2. 无序容器

#include <unordered_map>
#include <unordered_set>
  • C++ STL 中的无序关联容器分为 unordered_mapunordered_multimapunordered_setunordered_multiset
  • unordered_mapunordered_set 容器会保证元素的互异性,即不能存储多个相同的元素;而 unordered_multimapunordered_multiset 则允许存储多个相同的元素。
  • unordered_mapunordered_multimapunordered_setunordered_multiset 均是使用哈希表来实现的。
  • 通过 begin(), end() 返回的迭代器来遍历容器时,它会进行无序遍历!

unordered_map,unordered_multimap

template < class Key,                                    // unordered_map::key_type
           class T,                                      // unordered_map::mapped_type
           class Hash = hash<Key>,                       // unordered_map::hasher
           class Pred = equal_to<Key>,                   // unordered_map::key_equal
           class Alloc = allocator< pair<const Key,T> >  // unordered_map::allocator_type
           > class unordered_map;
template < class Key,                                    // unordered_multimap::key_type
           class T,                                      // unordered_multimap::mapped_type
           class Hash = hash<Key>,                       // unordered_multimap::hasher
           class Pred = equal_to<Key>,                   // unordered_multimap::key_equal
           class Alloc = allocator< pair<const Key,T> >  // unordered_multimap::allocator_type
           > class unordered_multimap;

元素类型为 pair<const Key, T>,键为常量!


unordered_set,unordered_multiset

template < class Key,                        // unordered_set::key_type/value_type
           class Hash = hash<Key>,           // unordered_set::hasher
           class Pred = equal_to<Key>,       // unordered_set::key_equal
           class Alloc = allocator<Key>      // unordered_set::allocator_type
           > class unordered_set;
template < class Key,                         // unordered_multiset::key_type/value_type
           class Hash = hash<Key>,            // unordered_multiset::hasher
           class Pred = equal_to<Key>,        // unordered_multiset::key_equal
           class Alloc = allocator<Key>       // unordered_multiset::allocator_type
           > class unordered_multiset;

返回的迭代器指向的元素类型为 const value_type,即无法修改容器中的元素!


自定义键类型

#include <iostream>
#include <unordered_map>
#include <string>

struct Key {
    std::string first;
    std::string second;
};

struct KeyHash {
    std::size_t operator()(const Key& k) const {
        return std::hash<std::string>()(k.first) ^
               (std::hash<std::string>()(k.second) << 1);
    }
};

struct KeyEqual {
    bool operator()(const Key& lhs, const Key& rhs) const {
        return lhs.first == rhs.first && lhs.second == rhs.second;
    }
};

int main() {
    Key k1{ "John", "Doe" }, k2{ "Mary", "Sue" };

    std::unordered_map<Key, std::string, KeyHash, KeyEqual> m = {
    	{ k1, "example"},
        { k2, "another"}
    };
    
    return 0;
}

对于自定义 unordered_setunordered_multiset 的值类型也同上。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值