The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.


Returns an iterator addressing the location of an element in a map that has a key equivalent to a specified key.

iterator find(const Key& key);

const_iterator find(const Key& key) const;


The key value to be matched by the sort key of an element from the map being searched.

An iterator that refers to the location of an element with a specified key, or the location succeeding the last element in the map (map::end()) if no match is found for the key.

The member function returns an iterator that addresses an element in the map whose sort key is equivalent to the argument key under a binary predicate that induces an ordering based on a less than comparability relation.

If the return value of find is assigned to a const_iterator, the map object cannot be modified. If the return value of find is assigned to an iterator, the map object can be modified

// compile with: /EHsc /W4 /MTd
#include <map>
#include <iostream>
#include <vector>
#include <string>
#include <utility>  // make_pair()

using namespace std;

template <typename M> void print_collection(const M& m) {
    cout << m.size() << " elements: ";

    for (const auto& p : m) {
        cout << "(" << p.first << ", " << p.second << ") ";
    cout << endl;

template <typename T> void find_int(const T& t, int val) {
    cout << "Trying find() on value " << val << endl;
    auto result = t.find(val);
    if (result != t.end()) {
        cout << "Element found: "; cout << "(" << result->first << ", " << result->second << ")"; cout << endl;
    } else {
        cout << "Element not found." << endl;

int main()
    map<int, string> m1({ {40, "Zr"}, {45, "Rh"} });
    cout << "The starting map m1 is (key, value):" << endl;

    vector<pair<int, string>> v;
    v.push_back(make_pair(43, "Tc"));
    v.push_back(make_pair(41, "Nb"));
    v.push_back(make_pair(46, "Pd"));
    v.push_back(make_pair(42, "Mo"));
    v.push_back(make_pair(44, "Ru"));
    v.push_back(make_pair(44, "Ru")); // attempt a duplicate

    cout << "Inserting the following vector data into m1:" << endl;

    m1.insert(v.begin(), v.end());

    cout << "The modified map m1 is (key, value):" << endl;
    cout << endl;
    find_int(m1, 45);
    find_int(m1, 6);

The starting map m1 is (key, value):
2 elements: (40, Zr) (45, Rh)
Inserting the following vector data into m1:
6 elements: (43, Tc) (41, Nb) (46, Pd) (42, Mo) (44, Ru) (44, Ru)
The modified map m1 is (key, value):
7 elements: (40, Zr) (41, Nb) (42, Mo) (43, Tc) (44, Ru) (45, Rh) (46, Pd)

Trying find() on value 45
Element found: (45, Rh)
Trying find() on value 6
Element not found.

Header: <map>

Namespace: std

© 2014 Microsoft