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



描述

C++ 函式std::map::size()返回對映中存在的元素數量。

宣告

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

C++98

size_type size() const;

C++11

size_type size() const noexcept;

引數

返回值

返回對映中實際存在的物件。

異常

此成員函式從不丟擲異常。

時間複雜度

常數,即 O(1)

示例

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

#include <iostream>
#include <map>

using namespace std;

int main(void) {
   map<char, int> m;

   cout << "Initial size of map = " << m.size() << endl;

   m = {
      {'a', 1},
      {'b', 2},
      {'c', 3},
      {'d', 4},
      {'e', 5},
      };

     cout << "Size of map after inserting elements = " << m.size() << endl;

   return 0;
}

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

Initial size of map = 0
Size of map after inserting elements = 5
map.htm
廣告

© . All rights reserved.