public member function
<map>

std::multimap::find

      iterator find (const key_type& k);const_iterator find (const key_type& k) const;
Get iterator to element
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 multimap::end.

Notice that this function returns an iterator to a single element (of the possibly multiple elements with equivalent keys). To obtain the entire range of equivalent elements, see multimap::equal_range.

Two keys are considered equivalent if the container's comparison object returnsfalsereflexively (i.e., no matter the order in which the elements are passed as arguments).

参数

k
Key to be searched for.
成员类型key_typeis the type of the keys for the elements in the container, defined in multimap as an alias of its first template parameter ().

返回值

An iterator to the element, if an element with specified key is found, or multimap::end otherwise.

如果multimap对象是const限定的,则该函数返回一个const_iterator。否则,它返回一个iterator.

成员类型iteratorconst_iterator是指向元素的(类型为value_type).
请注意,value_typein multimap containers is an alias ofpair<const key_type, mapped_type>.

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// multimap::find
#include <iostream>
#include <map>

int main ()
{
  std::multimap<char,int> mymm;

  mymm.insert (std::make_pair('x',10));
  mymm.insert (std::make_pair('y',20));
  mymm.insert (std::make_pair('z',30));
  mymm.insert (std::make_pair('z',40));

  std::multimap<char,int>::iterator it = mymm.find('x');
  mymm.erase (it);
  mymm.erase (mymm.find('z'));

  // print content:
  std::cout << "elements in mymm:" << '\n';
  std::cout << "y => " << mymm.find('y')->second << '\n';
  std::cout << "z => " << mymm.find('z')->second << '\n';

  return 0;
}

输出
elements in mymm:
y => 20
z => 40


复杂度

相对于size对数复杂度。

迭代器有效性

没有变化。

数据竞争

访问容器(const 和非 const 版本都不会修改容器)。
不访问映射值:并发访问或修改元素是安全的。

异常安全

强保证:如果抛出异常,容器没有发生变化。

另见