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



描述

C++ 函式std::map::clear() 透過移除所有元素並設定對映大小為零來銷燬對映。

宣告

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

C++98

void clear();

C+11

void clear() noexcept;

引數

返回值

異常

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

時間複雜度

線性,即 O(n)

示例

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

#include <iostream>
#include <map>

using namespace std;

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

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

   m.clear();

   cout << "Size of map after clear opearation = " << m.size() << endl;

   return 0;
}

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

Initial size of map = 5
Size of map after clear opearation = 0
map.htm
廣告