
- C 標準庫
- C 標準庫
- C++ 標準庫
- C++ 庫 - 首頁
- C++ 庫 - <fstream>
- C++ 庫 - <iomanip>
- C++ 庫 - <ios>
- C++ 庫 - <iosfwd>
- C++ 庫 - <iostream>
- C++ 庫 - <istream>
- C++ 庫 - <ostream>
- C++ 庫 - <sstream>
- C++ 庫 - <streambuf>
- C++ 庫 - <atomic>
- C++ 庫 - <complex>
- C++ 庫 - <exception>
- C++ 庫 - <functional>
- C++ 庫 - <limits>
- C++ 庫 - <locale>
- C++ 庫 - <memory>
- C++ 庫 - <new>
- C++ 庫 - <numeric>
- C++ 庫 - <regex>
- C++ 庫 - <stdexcept>
- C++ 庫 - <string>
- C++ 庫 - <thread>
- C++ 庫 - <tuple>
- C++ 庫 - <typeinfo>
- C++ 庫 - <utility>
- C++ 庫 - <valarray>
- C++ STL 庫
- C++ 庫 - <array>
- C++ 庫 - <bitset>
- C++ 庫 - <deque>
- C++ 庫 - <forward_list>
- C++ 庫 - <list>
- C++ 庫 - <map>
- C++ 庫 - <multimap>
- C++ 庫 - <queue>
- C++ 庫 - <priority_queue>
- C++ 庫 - <set>
- C++ 庫 - <stack>
- C++ 庫 - <unordered_map>
- C++ 庫 - <unordered_set>
- C++ 庫 - <vector>
- C++ 庫 - <algorithm>
- C++ 庫 - <iterator>
- C++ 高階庫
- C++ 庫 - <any>
- C++ 庫 - <barrier>
- C++ 庫 - <bit>
- C++ 庫 - <chrono>
- C++ 庫 - <cinttypes>
- C++ 庫 - <clocale>
- C++ 庫 - <condition_variable>
- C++ 庫 - <coroutine>
- C++ 庫 - <cstdlib>
- C++ 庫 - <cstring>
- C++ 庫 - <cuchar>
- C++ 庫 - <charconv>
- C++ 庫 - <cfenv>
- C++ 庫 - <cmath>
- C++ 庫 - <ccomplex>
- C++ 庫 - <expected>
- C++ 庫 - <format>
- C++ 庫 - <future>
- C++ 庫 - <flat_set>
- C++ 庫 - <flat_map>
- C++ 庫 - <filesystem>
- C++ 庫 - <generator>
- C++ 庫 - <initializer_list>
- C++ 庫 - <latch>
- C++ 庫 - <memory_resource>
- C++ 庫 - <mutex>
- C++ 庫 - <mdspan>
- C++ 庫 - <optional>
- C++ 庫 - <print>
- C++ 庫 - <ratio>
- C++ 庫 - <scoped_allocator>
- C++ 庫 - <semaphore>
- C++ 庫 - <source_location>
- C++ 庫 - <span>
- C++ 庫 - <spanstream>
- C++ 庫 - <stacktrace>
- C++ 庫 - <stop_token>
- C++ 庫 - <syncstream>
- C++ 庫 - <system_error>
- C++ 庫 - <string_view>
- C++ 庫 - <stdatomic>
- C++ 庫 - <variant>
- C++ STL 庫速查表
- C++ STL - 速查表
C++ unordered_map::clear() 函式
C++ 的unordered_map::clear()函式用於銷燬或清空unordered_map容器,方法是移除所有元素並將unordered_map的大小設定為零。
即使從unordered_map中移除了所有元素,我們也可以使用[]運算子向同一個unordered_map新增新元素。例如:unordered_map[1] = "tutorialspoint";
語法
以下是unordered_map::clear()函式的語法。
unordered_map.clear();
引數
此函式不接受任何引數。
返回值
此函式不返回任何值,因為使用clear()函式後unordered_map的大小為零。
示例1
在下面的示例中,讓我們演示clear()函式的使用,如下所示
#include <iostream> #include <unordered_map> using namespace std; int main(void) { unordered_map<char, int> um = { {'a', 1}, {'b', 2}, {'c', 3}, {'d', 4}, {'e', 5} }; cout << "Initial size of unordered map = " << um.size() << endl; um.clear(); cout << "Size of unordered map after clear operation = " << um.size() << endl; return 0; }
輸出
以上程式碼的輸出如下:
Initial size of unordered map = 5 Size of unordered map after clear operation = 0
示例2
下面的示例中,我們使用clear()函式清空unordered_map,然後向同一個unordered_map新增兩個元素,如下所示
#include <iostream> #include <string> #include <unordered_map> using namespace std; int main () { unordered_map<string,string> Umap = { {"Aman","Kumar"}, {"Vivek","Verma"}, {"Jhon","Satya"} }; cout << "mymap contains:"; for (auto& x: Umap)cout << " " << x.first << "=" << x.second; cout << endl; //removing all the element from the map. Umap.clear(); //adding two element from the map Umap["TutorialsPoint"]="Tutorix"; Umap["Location"]="Hyderabad"; cout << "mymap contains:"; for (auto& x: Umap) cout << " " << x.first << "=" << x.second; cout << endl; return 0; }
輸出
執行後,我們得到以下輸出:
mymap contains: Jhon=Satya Vivek=Verma Aman=Kumar mymap contains: Location=Hyderabad TutorialsPoint=Tutorix
示例3
讓我們看看下面的例子,我們顯示使用clear()函式之前unordered_map的元素,然後顯示unordered_map的大小,如下所示
#include <iostream> #include <string> #include <unordered_map> using namespace std; int main () { unordered_map<int, string> Umap; Umap[1] = "tutorialspoint"; Umap[2] = "Hyderabad India"; Umap[3] = "Tutorix"; Umap[4] = "Noida India"; cout<<"Before use of clear() functon"; for (auto& x: Umap) cout << "\n" << x.first << "=" << x.second; cout << endl; Umap.clear(); cout<<"After use of clear() function \n"; cout <<Umap.size(); return 0; }
輸出
以上程式碼的輸出如下:
Before use of clear() functon 4=Noida India 3=Tutorix 2=Hyderabad India 1=tutorialspoint After use of clear() function 0
廣告