unordered_set :: clear()函数是C++ STL中的内置函数,用于清除unordered_set容器。也就是说,此函数从unordered_set中删除所有元素并将其清空。所有对容器的迭代器,指针和引用均无效。这将容器的大小减小到零。
语法:
unordered_set_name.clear()
参数:此函数不接受任何参数。
返回值:该函数不返回任何值。
下面的程序说明了unordered_set :: clear()函数:
程序1 :
// C++ program to illustrate the
// unordered_set::clear() function
#include
#include
using namespace std;
int main()
{
unordered_set sampleSet;
// Inserting elements
sampleSet.insert(5);
sampleSet.insert(10);
sampleSet.insert(15);
sampleSet.insert(20);
sampleSet.insert(25);
// displaying all elements of sampleSet
cout << "sampleSet contains: ";
for (auto itr = sampleSet.begin(); itr != sampleSet.end(); itr++) {
cout << *itr << " ";
}
// clear the set
sampleSet.clear();
// size after clearing
cout << "\nSize of set after clearing elemets: "
<< sampleSet.size();
return 0;
}
输出:
sampleSet contains: 25 5 10 15 20
Size of set after clearing elemets: 0
程序2 :
// C++ program to illustrate the
// unordered_set::clear() function
#include
#include
using namespace std;
int main()
{
unordered_set sampleSet;
// Inserting elements
sampleSet.insert("Welcome");
sampleSet.insert("To");
sampleSet.insert("GeeksforGeeks");
sampleSet.insert("Computer Science Portal");
sampleSet.insert("For Geeks");
// displaying all elements of sampleSet
cout << "sampleSet contains: ";
for (auto itr = sampleSet.begin(); itr != sampleSet.end(); itr++) {
cout << *itr << " ";
}
// clear the set
sampleSet.clear();
// size after clearing
cout << "\nSize of set after clearing elemets: "
<< sampleSet.size();
return 0;
}
输出:
sampleSet contains: Welcome To GeeksforGeeks For Geeks Computer Science Portal
Size of set after clearing elemets: 0
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程” 。