How to use...STL!!!

1. (mathtt{set})

1.1. (mathtt{upper\_bound(),lower\_bound()})

  • (mathtt{upper\_bound()}):第一个大于 ( m val) 的数。
  • (mathtt{lower\_bound()}):第一个大于等于 ( m val) 的数。

需要注意的是如果没有符合条件的数就会返回 (mathtt{set.end()})

1.2. (mathtt{swap()})

(mathtt{swap()}) 的定义:

      /**
       *  @brief  Swaps data with another %set.
       *  @param  x  A %set of the same element and allocator types.
       *
       *  This exchanges the elements between two sets in constant time.
       *  (It is only swapping a pointer, an integer, and an instance of
       *  the @c Compare type (which itself is often stateless and empty), so it
       *  should be quite fast.)
       *  Note that the global std::swap() function is specialized such that
       *  std::swap(s1,s2) will feed to this function.
       */

用法是 (mathtt{set\_1.swap(set\_2)})

1.3. (mathtt{rbegin()})

返回 (mathtt{set}) 的末尾元素。注意不能直接赋值给迭代器,这样会 (mathtt{CE})

2. (mathtt{multiset})

2.1. (mathtt{erase()})

  • (mathtt{erase(})(mathtt )):删除这个值的所有元素。
  • (mathtt{erase(})迭代器(mathtt )):删除这个迭代器指向的元素。

3. (mathtt{map})

3.1. 查询

  • (mathtt{find()}): 如果不存在这个下标,返回值为 (mathtt{map.end()})

  • (mathtt{count()}): 如果存在这个下标,返回值为 (1),否则为 (0)

  • (mathtt{map[]}): 如果存在这个下标,返回值为这个下标的值,否则为 (0)

敲黑板:(mathtt{map[]}) 在询问不存在的下标时,第一次会返回 (0),但之后会填个东西进下标,之后的查询无论使用哪一种操作都是错的!!!

3.2. 插入

  • (mathtt{map[x]=y})
  • (mathtt{map.insert(make\_pair(x,y))})

好像第二种要快一点,因为不用查找。

3.3. (mathtt{upper\_bound(),lower\_bound()})

  • (mathtt{upper\_bound()}):第一个大于 ( m val) 的键值。
  • (mathtt{lower\_bound()}):第一个大于等于 ( m val) 的键值。

返回迭代器为 it->first 为键值,it->second 为值。

原文地址:https://www.cnblogs.com/AWhiteWall/p/13555797.html