map的使用(c++)

1.map类的声明

template < class Key,
           class T,
           class Compare = less<Key>, 
           class Alloc = allocator<pair<const Key,T>>> class map;

2.pari类型介绍

map底层的红黑树节点中的数据,使⽤pair<Key,T>存储键值对数据。就是对,key和value进行了多一层的封装。

typedef pair<const Key, T> value_type;
template <class T1, class T2>
struct pair
{

   typedef T1 first_type;
   typedef T2 second_type;
   T1 first;
   T2 second;
   pair(): first(T1()), second(T2())
       {}
   pair(const T1& a, const T2& b): first(a), second(b)
       {}
   template<class U, class V>
   pair (const pair<U,V>& pr): first(pr.first), second(pr.second)
       {}
};
template <class T1,class T2>
inline pair<T1,T2> make_pair (T1 x, T2 y)
{
    return ( pair<T1,T2>(x,y) );
}

3.map的构造

无参默认构造:

explicit map (const key_compare& comp = key_compare(),
              const allocator_type& alloc = allocator_type());

迭代区间构造:

template <class InputIterator>
map (InputIterator first, InputIterator last,
     const key_compare& comp = key_compare(),
     const allocator_type& = allocator_type());

拷贝构造:

map (const map& x);

列表构造:

map (initializer_list<value_type> il,
     const key_compare& comp = key_compare(),
     const allocator_type& alloc = allocator_type());

正向迭代器:

iterator begin();
iterator end();

反向迭代器:

reverse_iterator rbegin();
reverse_iterator rend();

4.map的增删查

map增接口,插入的pair键值对数据,跟set不同,但是查和删的接口只用关键字key跟set是完全类似的,不过find返回的是iterator,不仅仅可以确认key在不在,还找到key映射的value,同时通过迭代还可以修改value。

单个数据插入:

如果已经key存在则插入失败,key存在相等value不相等也会插入失败

pair<iterator,bool> insert (const value_type& val);

列表插入:

已经在容器中存在的值不会插入

void insert (initializer_list<value_type> il);

eg:

map<string, string> dict = { {"left", "左边"}, {"right", "右边"},
                           {"insert", "插⼊"},{ "string", "字符串" } };

迭代器区间插入:

已经在容器中存在的值不会插入

template <class InputIterator>
void insert (InputIterator first, InputIterator last);

查找k:

返回k所在的迭代器,没有找到返回end()

iterator find (const key_type& k);

查找k:

返回k的个数

size_type count (const key_type& k) const;

删除⼀个迭代器位置的值:
 

iterator erase (const_iterator position);

删除k:

k存在返回0,存在返回1

size_type erase (const key_type& k);

删除⼀段迭代器区间的值:

iterator erase (const_iterator first, const_iterator last);

返回大于等于val的迭代器:

iterator lower_bound (const key_type& k);

返回大于val的迭代器:

const_iterator lower_bound (const key_type& k) const;

5.map的数据修改

set不支持修改,但是map支持修改value。

map第⼀个支持修改的式式时通过迭代器,迭代器遍历时或者find返回key所在的iterator修改,map还有⼀个非常重要的修改接口operator[],但是operator[]不仅支持修改,还支持插入数据和查找数据

insert插入⼀个pair<key, T>对象
    1、如果key已经在map中,插⼊失败,则返回⼀个pair<iterator,bool>对象,返回pair对象
first是key所在结点的迭代器,second是false
    2、如果key不在在map中,插⼊成功,则返回⼀个pair<iterator,bool>对象,返回pair对象
first是新插⼊key所在结点的迭代器,second是true
    无论插入成功还是失败,返回pair<iterator,bool>对象的first都会指向key所在的迭代器
    也就意味着insert插入失败时充当了查找的功能,因为这点,insert可以用来实现
operator[]
注意:这里有两个pair,⼀个是map底层红黑树节点中存的pair<key, T>,另⼀个是insert返回值pair<iterator,bool>

operator[]的实现:

mapped_type& operator[] (const key_type& k)
{
   pair<iterator, bool> ret = insert({ k, mapped_type() });
   iterator it = ret.first;
   return it->second;
}

6.map和multimap的差异

multimap和map的使用基本完全类似,主要区别点在于multimap支持关键值key冗余,并且multimap不支持[]。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值