multiset::clear(函數是C++ STL中的內置函數,可從多集容器中刪除所有元素。刪除後多圖容器的最終大小為0。
用法:
multiset_name.clear()
參數:該函數不接受任何參數。
返回值:該函數不返回任何內容。
以下示例程序旨在說明multimap::clear()函數:
示例1:
// CPP program to demonstrate the
// multiset::clear() function
#include <bits/stdc++.h>
using namespace std;
int main()
{
int arr[] = { 15, 10, 15, 11, 10 };
// initializes the set from an array
multiset<int> s(arr, arr + 5);
// prints all elements in set
cout << "The elements in multiset are: ";
for (auto it = s.begin(); it != s.end(); it++)
cout << *it << " ";
cout << "\nThe size after clear() is: ";
// erases all elements
s.clear();
cout << s.size();
return 0;
}
輸出:
The elements in multiset are: 10 10 11 15 15 The size after clear() is: 0
示例2:
// CPP program to demonstrate the
// multiset::clear() function
#include <bits/stdc++.h>
using namespace std;
int main()
{
int arr[] = { 15, 10, 15, 11, 10, 18, 18, 20, 20 };
// initializes the set from an array
multiset<int> s(arr, arr + 9);
// prints all elements in set
cout << "The elements in multiset are: ";
for (auto it = s.begin(); it != s.end(); it++)
cout << *it << " ";
cout << "\nThe size after clear() is: ";
// erases all elements
s.clear();
cout << s.size();
return 0;
}
輸出:
The elements in multiset are: 10 10 11 15 15 18 18 20 20 The size after clear() is: 0
相關用法
- C++ multiset insert()用法及代碼示例
- C++ multiset find()用法及代碼示例
- C++ multiset equal_range()用法及代碼示例
- C++ multiset get_allocator()用法及代碼示例
- C++ multiset begin()、end()用法及代碼示例
- C++ multiset empty()用法及代碼示例
- C++ multiset emplace_hint()用法及代碼示例
- C++ multiset key_comp()用法及代碼示例
- C++ multiset count()用法及代碼示例
- C++ multiset cbegin()、cend()用法及代碼示例
- C++ multiset rbegin()、rend()用法及代碼示例
- C++ multiset crbegin()、crend()用法及代碼示例
- C++ unoredered_set clear()用法及代碼示例
- C++ ios clear()用法及代碼示例
- C++ unordered_multiset clear()用法及代碼示例
注:本文由純淨天空篩選整理自gopaldave大神的英文原創作品 multiset clear() function in C++ STL。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。