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

更多 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/

Post author 作者: Grey
Copyright Notice 版权说明: Except where otherwise noted, all content of this blog is licensed under a CC BY-NC-SA 4.0 International license. 除非另有说明,本博客上的所有文章均受 知识共享署名 - 非商业性使用 - 相同方式共享 4.0 国际许可协议 保护。
原文地址:https://www.cnblogs.com/greyqz/p/7691181.html