📜  C++ STL中的多集clear()函数

📅  最后修改于: 2021-05-30 06:54:34             🧑  作者: Mango

multiset :: clear()函数是C++ STL中的内置函数,可从多集容器中删除所有元素。移除后多容器的最终大小为0。

句法:

multiset_name.clear()


参数:该函数不接受任何参数。
返回值:该函数不返回任何内容。

下面的程序说明了multiset :: clear()函数:

程序1:

C++
// C++ program to demonstrate the
// multiset::clear() function
#include 
using namespace std;
int main()
{
 
    int arr[] = { 15, 10, 15, 11, 10 };
 
    // initializes the set from an array
    multiset 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;
}


C++
// C++ program to demonstrate the
// multiset::clear() function
#include 
using namespace std;
int main()
{
 
    int arr[] = { 15, 10, 15, 11, 10, 18, 18, 20, 20 };
 
    // initializes the set from an array
    multiset 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 
The size after clear() is: 0






程式2:

C++

// C++ program to demonstrate the
// multiset::clear() function
#include 
using namespace std;
int main()
{
 
    int arr[] = { 15, 10, 15, 11, 10, 18, 18, 20, 20 };
 
    // initializes the set from an array
    multiset 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++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”