C++ 對映庫 - find() 函式



描述

C++ 函式 std::map::find() 查詢與鍵關聯的元素k.

如果操作成功,則方法返回指向該元素的迭代器,否則返回指向map::end().

宣告

以下是來自 std::map 標頭檔案的 std::map::find() 函式的宣告。

C++98

iterator find (const key_type& k);
const_iterator find (const key_type& k) const;

引數

k - 要搜尋的鍵。

返回值

如果物件被限定為常量,則方法返回一個常量迭代器,否則返回非常量迭代器。

異常

此成員函式不丟擲任何異常。

時間複雜度

對數,即 O(log n)

示例

以下示例顯示了 std::map::find() 函式的使用。

#include <iostream>
#include <map>

using namespace std;

int main(void) {
   map<char, int> m = {
            {'a', 1},
            {'b', 2},
            {'c', 3},
            {'d', 4},
            {'e', 5},
            };

   auto it = m.find('c');

   cout << "Iterator points to " << it->first << 
      " = " << it->second << endl;

   return 0;
}

讓我們編譯並執行上述程式,這將產生以下結果:

Iterator points to c = 3
map.htm
廣告

© . All rights reserved.