TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C++ set size() Function

C++ set size() Function with tutorial for beginners and professionals with examples on constructor, destructor, operator=(), begin(), cbegin(), cend(), end(), crbegin(), empty(), max_size(), clear(), emplace_hint(), key_comp(), swap() etc.

<< Back to CPP

C++ set size()

C++ set size() function is used to find the number of elements present in the set container.

Syntax

Member type size_type is an unsigned integral type.

size_type size() const;               // until C++ 11
size_type size() const noexcept;    //since C++ 11

Parameter

None

Return value

It returns the number of elements present in the set.

Complexity

Constant.

Iterator validity

No changes.

Data Races

The container is accessed.

Concurrently accessing the elements of a set is safe.

Exception Safety

This function never throws exception.

Example 1

Let's see the simple example to calculate the size of the set:

#include <set>
#include <iostream>
using namespace std;
 
int main()
{ 
    set<char> num {'a', 'b', 'c', 'd'}; 
    cout << "num set contains " << num.size() << " elements.\n";
    return 0;
}

Output:

 num set contains 4 elements.

In the above example, set num contains 4 elements. Therefore size() returns 4 elements.

Example 2

Let's see a simple example to calculate initial size of set and size of set after adding elements:

#include <iostream>
#include <set>

using namespace std;

int main(void) {
   set<int> m;

   cout << "Initial size of set = " << m.size() << endl;

   m = {1,2,3,4,5,6};

     cout << "Size of set after inserting elements = " << m.size() << endl;

   return 0;
}

Output:

Initial size of set = 0
Size of set after inserting elements = 6

In the above example, first set is empty hence, size() function will return 0 and after inserting 6 elements it will return 6.

Example 3

Let's see a simple example:

#include <iostream>
#include <set>

using namespace std;

int main ()
{
  set<int> myset = {100,200,300,400};

  while (myset.size())
  {
    cout << *myset.begin()<< '\n';
    myset.erase(myset.begin());
  }

  return 0;
}

Output:

100
200
300
400

In the above example, It simply use the size() function in while loop and prints the elements of set until the size of set.

Example 4

Let's see a simple example:

#include <iostream>
#include <set>
#include <string>

using namespace std;

int main() {

  typedef set<int> marksSet;
   
   int number;
   marksSet marks;

   cout<<"Enter three sets of marks: \n";
   
   for(int i =0; i<3; i++)
   {
       cin>> number;    // Get value
       marks.insert(number);   // Put them in set
   }
   
      cout<<"\nSize of phone set is:"<< marks.size();
      cout<<"\nList of telephone numbers: \n";
      marksSet::iterator p;
      for(p = marks.begin(); p!=marks.end(); p++)
      {
          cout<<(*p)<<" \n ";
      }
    
   return 0;
}

Output:

Enter three sets of marks: 
78 90 84

Size of phone set is: 3
List of telephone numbers: 
78 
 84 
 90

In the above example, the program first creates marks set interactively. Then it displays the total size of marks set and all the elements available in the set.






Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf