C++ unordered_multimap::clear() 函式



C++ 的std::unordered_multimap::clear()函式用於透過刪除所有元素並設定 unordered_multimap 的大小為零來銷燬或丟棄 unordered_multimap。

從 unordered_multimap 中刪除所有元素後,我們可以使用 insert() 和 emplace() 函式將新元素新增到同一個 unordered_multimap 中。

語法

以下是 std::unordered_multimap::clear() 函式的語法。

void clear() noexcept;

引數

此函式不接受任何引數。

返回值

此函式不返回任何內容,因為使用 clear() 函式後 unordered_multimap 的大小為零。

示例 1

在以下示例中,讓我們看看 clear() 函式的使用方法。

#include <iostream>
#include <unordered_map>
using namespace std;
int main(void) {
   unordered_multimap<char, int> umm = {
      {'a', 1},
      {'b', 2},
      {'c', 3},
      {'b', 2},
      {'c', 3},
      {'d', 4},
   };
   cout << "Initial size of unordered multimap = " << umm.size() << endl;
   umm.clear();

   cout << "Size of unordered multimap after use of clear() function = " << umm.size() << endl;
   return 0;
}

輸出

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

Initial size of unordered multimap = 5
Size of unordered multimap after clear operaion = 0

示例 2

考慮以下示例,我們將在此示例中在執行 clear() 函式後將元素新增到對映中。

#include <iostream>
#include <string>
#include <unordered_map>
using namespace std;
int main () {
   unordered_multimap<string,string> ummap = { {"Aman","Kumar"}, {"Vivek","Verma"}, {"Jhon","Satya"} };

   cout << "ummap contains:";
   for (auto& x: ummap)cout << " " << x.first << "=" << x.second;
      cout << endl;
    
   //removing all the element from the map.
   ummap.clear();
    
   //adding two element from the map
   ummap.insert({"tutorialspoint", "Noida"});
   ummap.insert({"tutorialspoint","Hyderabad"});
   cout << "ummap contains:";
   for (auto& x: ummap) cout << " " << x.first << "=" << x.second;
      cout << endl;
   return 0;
}

輸出

如果我們執行上述程式碼,它將生成以下輸出:

ummap contains: Vivek=Verma Jhon=Satya Aman=Kumar
ummap contains: tutorialspoint=Hyderabad tutorialspoint=Noida

示例 3

讓我們看一下以下示例,我們將在此示例中稍後顯示 unordered_multimap 的元素,應用 clear() 函式並顯示 unordered_multimap 的大小。

#include <iostream>
#include <string>
#include <unordered_map>
using namespace std;
int main () {
   unordered_multimap<int, string> uMmap;
   uMmap.insert({1, "tutorialspoint"});
   uMmap.insert({1, "tutorialspoint"});
   uMmap.insert({2, "Hyderabad India"});
   uMmap.insert({3, "Tutorix"});
   uMmap.insert({4, "Noida India"});
   cout<<"Before use of clear() functon";
   for (auto& x: uMmap) cout << "\n" << x.first << "=" << x.second;
      cout << endl;
    
   uMmap.clear();
   cout<<"After use of clear() function \n";
   cout <<uMmap.size();
   return 0;
}

輸出

以下是上述程式碼的輸出:

Before use of clear() functon
4=Noida India
3=Tutorix
2=Hyderabad India
1=tutorialspoint
1=tutorialspoint
After use of clear() function 
0

示例 4

以下是一個示例,我們將使用兩個 unordered_multimap,一個儲存專案,另一個儲存從第一個過濾偶數鍵後獲得的專案,稍後我們應用 clear() 函式並顯示在之前和之後的尺寸。

#include <string>
#include <iostream>
#include <unordered_map>
using namespace std;
int main () {
   unordered_multimap<int, string> uMmap, umm;
   uMmap.insert({1, "tutorialspoint"});
   uMmap.insert({1, "tutorialspoint"});
   uMmap.insert({2, "Hyderabad India"});
   uMmap.insert({3, "Tutorix"});
   uMmap.insert({4, "Noida India"});
   cout<<"Umm Before use of clear() functon"<<endl;
   for (auto& x: uMmap){
      if(x.first % 2 != 0){
         umm.insert({x.first, x.second});
      }
   }
    
   for(auto& x: umm){
      cout<<x.first<<" : "<<x.second<<endl;
   }
   cout<<"size of umm before use of clear function: "<<endl;
    
   cout <<umm.size()<<endl;
   cout<<"size of umm after use of clear() function \n";
   // umm.clear();
   umm.clear();
   cout<<umm.size()<<endl;
   return 0;
}

輸出

上述程式碼的輸出如下:

Umm Before use of clear() functon
1 : tutorialspoint
1 : tutorialspoint
3 : Tutorix
size of umm before use of clear function: 
3
size of umm after use of clear() function 
0
廣告