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 版本都不會修改容器)。
不訪問對映值:併發訪問或修改元素是安全的。

異常安全

強保證:如果丟擲異常,容器沒有發生變化。

另見