c++中stl 哈希函数_C ++ STL中的set :: size()函数

末蓝、 2023-03-05 12:38 136阅读 0赞

c++中stl 哈希函数

C ++ STL set :: size()函数 (C++ STL set::size() function)

set::size() function is a predefined function, it is used to get the size of a set, it returns the total number of elements of the set container.

set :: size()函数是预定义的函数,用于获取集合的大小,它返回集合容器中元素的总数。

Prototype:

原型:

  1. set<T> st; //declaration
  2. set<T>::iterator it; //iterator declaration
  3. int sz=st.size();

Parameter: Nothing to pass

参数:无通过

Return type: Integer

返回类型:整数

Usage: The function returns size of the set

用法:该函数返回集合的大小

Example:

例:

  1. For a set of integer,
  2. set<int> st;
  3. set<int>::iterator it;
  4. st.insert(4);
  5. st.insert(5);
  6. set content:
  7. 4
  8. 5
  9. int sz=st.size(); //sz=size of set that is 2
  10. Print sz; //prints 2

Header file to be included:

包含的头文件:

  1. #include <iostream>
  2. #include <set>
  3. OR
  4. #include <bits/stdc++.h>

C++ implementation:

C ++实现:

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. void printSet(set<int> st){
  4. set<int>:: iterator it;
  5. cout<<"Set contents are:\n";
  6. for(it=st.begin();it!=st.end();it++)
  7. cout<<*it<<" ";
  8. cout<<endl;
  9. }
  10. int main(){
  11. cout<<"Example of size function\n";
  12. set<int> st;
  13. set<int>:: iterator it;
  14. cout<<"inserting 4\n";
  15. st.insert(4);
  16. cout<<"inserting 6\n";
  17. st.insert(6);
  18. cout<<"inserting 10\n";
  19. st.insert(10);
  20. printSet(st); //printing current set
  21. //finding set sizeof
  22. cout<<"current set size is: "<<st.size();
  23. return 0;
  24. }

Output

输出量

  1. Example of size function
  2. inserting 4
  3. inserting 6
  4. inserting 10
  5. Set contents are:
  6. 4 6 10
  7. current set size is: 3

翻译自: https://www.includehelp.com/stl/set-size-function-in-cpp-stl.aspx

c++中stl 哈希函数

发表评论

表情:
评论列表 (有 0 条评论,136人围观)

还没有评论,来说两句吧...

相关阅读

    相关 C/C++STL函数

    STL的一个重要特点是数据结构和算法的分离。尽管这是个简单的概念,但这种分离确实使得STL变得非常通用。例如,由于STL的sort()函数是完全通用的,你可以用它来操作几乎任何

    相关 C++STL 仿函数

    1. 概述 仿函数(functors)是早期的命名,C++标准规格定案后采用的新名称是函数对象(function objects)(也就是一种具有函数特质的对象)