Home » C++ STL

set::size() function in C++ STL

C++ STL set::size() function: Here, we are going to learn about the size() function of set in C++ STL (Standard Template Library).
Submitted by Radib Kar, on February 16, 2019

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<T> st; //declaration
    set<T>::iterator it; //iterator declaration
    int sz=st.size();

Parameter: Nothing to pass

Return type: Integer

Usage: The function returns size of the set


    For a set of integer,
    set<int> st;
    set<int>::iterator it;
    set content:

    int sz=st.size(); //sz=size of set that is 2
    Print sz; //prints 2

Header file to be included:

    #include <iostream>
    #include <set>
    #include <bits/stdc++.h>

C++ implementation:

#include <bits/stdc++.h>
using namespace std;

void printSet(set<int> st){
    set<int>:: iterator it;
    cout<<"Set contents are:\n";
    cout<<*it<<" ";

int main(){
    cout<<"Example of size function\n";
    set<int> st;
    set<int>:: iterator it;
    cout<<"inserting 4\n";
    cout<<"inserting 6\n";
    cout<<"inserting 10\n";
    printSet(st); //printing current set
    //finding set sizeof
    cout<<"current set size is: "<<st.size();
    return 0;


Example of size function
inserting 4      
inserting 6      
inserting 10     
Set contents are:
4 6 10  
current set size is: 3 

Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.