TheDeveloperBlog.com

Home | Contact Us

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

C++ set max_size() Function

C++ set max_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 max_size()

C++ max_size() function is used to get the maximum number of size a set container can hold.

Syntax

Member type size_type is an unsigned integral type.

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

Parameter

None

Return value

It returns the maximum allowed length of the set container.

Complexity

Constant.

Iterator validity

No changes.

Data Races

The container is accessed.

Concurrently accessing the elements of a set is safe.

Exception Safety

This member function never throws exception.

Example 1

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

#include <iostream>
#include <set>

using namespace std;
 
int main()
{
    set<char,char> s;
    cout << "Maximum size of a 'set' is " << s.max_size() << "\n";
    return 0;
}

Output:

 Maximum size of a 'set' is 461168601842738790

In the above example, max_size() function returns the maximum size of the set.

Example 2

Let's see a simple example:

#include <iostream>
#include <set>

using namespace std;

int main ()
{
  int i;
  set<int> myset;

  if (myset.max_size()>1000)
  {
    for (i=0; i<1000; i++) myset.insert(0);
    cout << "The set contains 1000 elements.\n";
  }
  else cout << "The set could not hold 1000 elements.\n";

  return 0;
}

Output:

The set contains 1000 elements.

In the above example, member max_size is used to check beforehand whether the set will allow for 1000 elements to be inserted.

Example 3

Let's see a simple example to find the max size of an empty set and a non-empty set:

#include <set>
#include <iostream>

using namespace std;
 
int main()
{
 
    // initialize container
    set<int> mp1, mp2;
    mp1 = {1111};
 
    // max size of Non-empty set
    cout << "The max size of mp1 is " << mp1.max_size();
 
    // max size of Empty-set
    cout << "\nThe max size of mp2 is " << mp2.max_size();
    return 0;
}

Output:

The max size of mp1 is 461168601842738790
The max size of mp2 is 461168601842738790

In the above example, there are two sets i.e. m1 and m2. m1 is a non-empty set and m2 is an empty set. But the maximum size of both sets is the same.

Example 4

Let's see a simple example:

#include <iostream>
#include <set>
#include <string>
using namespace std;

int main() {

  typedef set<string> city;  
   string name;
   city fmly ;
   int n;

   cout<<"Enter the number of family members :";
   cin>>n;

   cout<<"Enter the name of each member: \n";
   for(int i =0; i<n; i++)
   {
       cin>> name;      // Get key
       fmly.insert(name);   // Put them in set
   }
   
      cout<<"\nTotal number of population of city set: "<<fmly.max_size();
      cout<<"\nTotal member of family is:"<< fmly.size();

      cout<<"\nName of family members: \n";
      cout<<"\nName \n ________________________\n";
      city::iterator p;
      for(p = fmly.begin(); p!=fmly.end(); p++)
      {
          cout<<(*p)<<" \n ";
      }
    
   return 0;
}

Output:

Enter the number of family members: 5
Enter the name of each member: 
Aman
Nikita
Divya
Amita
Kashish

Total number of population of city set: 461168601842738790
Total member of family is:5
Name of family members: 

Name 
 ________________________
Aman 
 Amita 
 Divya 
 Kashish 
 Nikita

In the above example, the program first creates city set interactively with given number of size. Then it displays the total size a city set can contain, total size of a fmly and all the names and their age 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