C++ Standard Template Library (STL) 高级容器

本文详细介绍了C++ STL中的map和set数据结构。包括定义、成员类型、常见操作如count()、find()等,并提供了使用示例。适用于初学者及需要深入理解这两种数据结构的开发者。

更多 STL 数据结构请阅读 NOIp 数据结构专题总结(STL structure 章节)

std::map

Definition:

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;

In a map, the key values are generally used to sort and uniquely identify the elements, while the mapped values store the content associated to this key. The types of key and mapped value may differ, and are grouped together in member type value_type, which is a pair type combining both:

typedef pair<const Key, T> value_type;

[WARNING] 如果不检查,直接返回 map[key],可能会出现意想不到的行为。如果 map 包含 key,没有问题,如果 map 不包含 key,使用下标有一个危险的副作用,会在 map 中插入一个 key 的元素,value 取默认值,返回 value。也就是说,map[key] 不可能返回 null

Functions:

count(): Count elements with a specific key

Searches the container for elements with a key equivalent to k and returns the number of matches.
Because all elements in a map container are unique, the function can only return 1 (if the element is found) or zero (otherwise).

find(): Get iterator to element (Recommend)

Searches the container for an element with a key equivalent to k and returns an iterator to it if found, otherwise it returns an iterator to map::end.

iter = m.find(key);
if (iter != m.end()) {
    return iter->second;
}
return null;

clear(): 清空 map

empty(): 判断是否为空 返回 0,1

std::set

Definition:

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;

In a set, the value of an element also identifies it (the value is itself the key, of type T), and each value must be unique. The value of the elements in a set cannot be modified once in the container (the elements are always const), but they can be inserted or removed from the container.

Functions:

clear() , 删除set容器中的所有的元素

empty() , 判断set容器是否为空

max_size() , 返回set容器可能包含的元素最大个数

size() , 返回当前set容器中的元素个数

count() 用来查找set中某个某个键值出现的次数。这个函数在set并不是很实用,因为一个键值在set只可能出现0或1次,这样就变成了判断某一键值是否在set出现过了。

begin() , 返回set容器的第一个元素

end() , 返回set容器的最后一个元素

erase(iterator) , 删除定位器iterator指向的值

erase(first,second) , 删除定位器first和second之间的值

erase(key_value) , 删除键值key_value的值

find() ,返回给定值值得定位器,如果没找到则返回end()

lower_bound(key_value),返回第一个大于等于key_value的定位器

upper_bound(key_value),返回最后一个大于等于key_value的定位器


References

  1. http://www.cplusplus.com/reference/map/map/
  2. http://www.cnblogs.com/nzbbody/p/3409298.html
  3. internal (blog/86) by dph
  4. http://www.cplusplus.com/reference/set/set/

转载于:https://www.cnblogs.com/greyqz/p/7691181.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值