共用方式為


multimap::find

傳回迭代器,其指向 multimap 中索引鍵等於指定索引鍵的第一個元素的位置。

iterator find(const Key& key);  const_iterator find(const Key& key) const; 

參數

  • key
    要以所搜尋之 multimap 中元素的排序鍵比對的索引鍵值。

傳回值

迭代器,表示具有指定索引鍵的元素位置,或者,如果沒有找到索引鍵的符合項,表示 multimap 中最後一個元素之後的位置 (multimap::end())。

備註

成員函式會傳回迭代器,其表示 multimap 中排序鍵 (在二元述詞下根據較少的比較性關聯引發排序) 等於引數索引鍵的元素。

如果 find 的傳回值指派給 const_iterator,則不能修改 multimap 物件。 如果 find 的傳回值指派給 iterator,則可以修改 multimap 物件。

範例

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

using namespace std;

template <typename A, typename B> void print_elem(const pair<A, B>& p) {
    cout << "(" << p.first << ", " << p.second << ") ";
}

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

    for (const auto& p : t) {
        print_elem(p);
    }
    cout << endl;
}

template <typename C, class T> void findit(const C& c, T val) {
    cout << "Trying find() on value " << val << endl;
    auto result = c.find(val);
    if (result != c.end()) {
        cout << "Element found: "; print_elem(*result); cout << endl;
    } else {
        cout << "Element not found." << endl;
    }
}

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

    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;
    print_collection(v);

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

    cout << "The modified multimap m1 is (key, value):" << endl;
    print_collection(m1);
    cout << endl;
    findit(m1, 45);
    findit(m1, 6);
}

輸出

  

需求

標頭:<map>

命名空間: std

請參閱

參考

multimap 類別

標準樣板程式庫