C++ STL中的无序序集()函数

这个 无序集合::大小() 方法是C++中的一个内置函数,用来返回无序的集合容器中的元素数量。

null

语法 :

unordered_set_name.size()

参数 :它不接受任何参数。

返回值 :函数返回容器中的元素数。

下面的程序说明了 无序集合::大小() 功能:

项目1:

// C++ program to illustrate the
// unordered_set.size() function
#include <iostream>
#include <unordered_set>
using namespace std;
int main()
{
unordered_set< int > arr1 = { 1, 2, 3, 4, 5 };
// prints the size of arr1
cout << "size of arr1:" << arr1.size();
// prints the element
cout << "The elements are: " ;
for ( auto it = arr1.begin(); it != arr1.end(); it++)
cout << *it << " " ;
return 0;
}


输出:

size of arr1:5
The elements are: 5 1 2 3 4

项目2:

// C++ program to illustrate the
// unordered_set::size() function
// when container is empty
#include <iostream>
#include <unordered_set>
using namespace std;
int main()
{
unordered_set< int > arr2 = {};
// prints the size
cout << "Size of arr2 : " << arr2.size();
return 0;
}


输出:

Size of arr2 : 0

© 版权声明
THE END
喜欢就支持一下吧
点赞12 分享