集::在C++ STL中的sie()

设置 是按照特定顺序存储唯一元素的容器。在内部,集合中的元素总是 分类 .集合通常实现为 二叉搜索树 .

null

set::size() 大小() 函数用于返回集合容器的大小或集合容器中的元素数。

语法:

set_name.size()

返回值: 它返回集合容器中的元素数。

例如:

Input  : set1{'a', 'b', 'c', 'd'};
         set1.size();
Output : 4

Input  : set2{};
         set2.size();
Output : 0

错误和异常

1.它有一个无例外的抛出保证。 2.传递参数时显示错误。

// C++ program to illustrate
// size() function on set
#include <bits/stdc++.h>
using namespace std;
int main()
{
// Take any two sets
set< char > set1, set2;
for ( int i = 0; i < 4; i++) {
set1.insert( 'a' + i);
}
// Printing the size of sets
cout << "set1 size: " << set1.size();
cout << endl;
cout << "set2 size: " << set2.size();
return 0;
}


输出:

set1 size: 4
set2 size: 0

时间复杂性: 常数

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