有序关联容器之 map

1. std::pair

1
2
3
4
template<
    class T1,
    class T2
> struct pair;

该类模板用于创建“键值对”,T1,T2可以内置类型,也可以是自定义类型。

2. map

1
2
3
4
5
6
7
Defined in header <map>
template<
    class Key,
    class T,
    class Compare = std::less<Key>,
    class Allocator = std::allocator<std::pair<const Key, T>>
> class map;

std:map,基于红黑树,树的每一个节点都是一个std::pair,节点的pair.first的类型必须支持“<”运算,或者自定义比较逻辑,相对于set,map才是真正关联容器。

map的迭代器所指向的对象的“Key”是const的,“Key”本身是不能修改的,这样避免红黑树的平衡结构被改变,当然“value”是可以改变的。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
#include <iostream>
#include <map>

int main()
{
    std::map<int,bool> m{{3, true}, {4, false}, {1, true}};
    for (auto p : m) 
    {
        std::cout << p.first << ' ' <<p.second << std::endl;
    }
}

       

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
#include <iostream>
#include <map>

int main()
{
    std::map<int,bool> m{{3, true}, {4, false}, {1, true}};
    for (auto [k, v] : m) 
    {
        std::cout << k << ' ' << v << std::endl;
    }
}

3. 迭代器

支持双向迭代器。

4. 插入元素

1
2
3
4
5
int main()
{
    std::map<int, bool> m;
    m.insert(std::pair<const int, bool>(3,true));  // 插入一个std::pair
}

5. 删除元素

1
2
3
4
5
6
7
iterator erase( iterator pos );                              
iterator erase( const_iterator pos );                         
iterator erase( const_iterator first, const_iterator last );  
size_type erase( const Key& key );                            

template< class K > 
    size_type erase( K&& x );                

6. 查找

1
2
3
4
bool contains( const Key& key ) const;  

template< class K >
bool contains( const K& x ) const;      

7. 随机访问

使用[key]或者at()访问元素。
对于[key]

1
2
3
4
5
6
int main()
{
    std::map<int, bool> m;
    m.insert(std::pair<const int, bool>(6, true));
    std::cout << m[6] << std::endl;
}

对于m[6],如果树的Key中没有6这个Key,map会向树中插入一个新元素,该节点的Key为6,value使用value类型的默认初始化方式。也就是说,“[]”操作可能会插入新节点,所以要注意const的map对象是不能使用“[]”的,请看如下代码:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
void fun(const std::map<int, int>& m)
{
    m[3];  //编译期报错,因为m是const的,是不能修改的。
}

int main()
{
    std::map<int, int> m;
    m.insert(std::pair<const int, int>(3, 100));
    fun(m);
}

对于at()

1
2
3
4
5
6
int main()
{
    std::map<int, bool> m;
    m.insert(std::pair<const int, bool>(6, true));
    std::cout << m.at(6) << std::endl;
}

对于m[6],树的Key中没有6这个Key,代码会报错,程序终止。

8. 其他

extract等等。

comments powered by Disqus