TheDeveloperBlog.com

Home | Contact Us

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

C++ set constructor

C++ set constructor 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 constructor

There are following five uses of set constructor:

  1. default constructor: This is used to construct an empty set container with zero elements.
  2. range constructor: This is used to construct a container with the contents of range [first, last).
  3. copy constructor: This is used to construct a set with a copy of the elements of existing container.
  4. move constructor: This is used to construct the container with the elements of other with the use of move semantics.
  5. initializer list constructor: This is used to construct the set with the contents of the initializer list.

Syntax

Default constructor

explicit set (const key_compare& comp = key_compare(),
              const allocator_type& alloc = allocator_type());	//until C++ 11

explicit set (const key_compare& comp = key_compare(),
              const allocator_type& alloc = allocator_type());
explicit set (const allocator_type& alloc);			//since C++ 11	

range constructor

template <class InputIterator>
  set (InputIterator first, InputIterator last,
       const key_compare& comp = key_compare(),
       const allocator_type& alloc = allocator_type());		//until C++ 11

template <class InputIterator>
  set (InputIterator first, InputIterator last,
       const key_compare& comp = key_compare(),
       const allocator_type& = allocator_type());			//since C++ 11

copy constructor

set (const set& x);						//until C++ 11
	
set (const set& x);
set (const set& x, const allocator_type& alloc);			//since C++ 11

move constructor

set (set&& x);
set (set&& x, const allocator_type& alloc);			//since C++ 11

initializer list constructor

set (initializer_list<value_type> il,
     const key_compare& comp = key_compare(),
     const allocator_type& alloc = allocator_type());		//since C++ 11

Parameter

comp: A comparison function object which takes two key arguments and returns true if first argument goes before the second argument otherwise false. By default it uses less<key_type> predicate.

alloc: A allocator object use for all memory allocations of this container.

first: Input iterator to the first position in a range.

last: Input iterator to the last position in a range.

x: Another set object of the same type.

il: An initializer list object from which the elements are to be copied.

Return value

Constructor never returns any value.

Complexity

For empty constructors and move constructors complexity will be constant.

For all other cases, complexity will be linear in the distance between the iterators if the elements are already sorted.

Iterator validity

Invalidate all pointers, iterators, and references related to x if the elements of set container are moved in the move constructor.

Data Races

All copied elements are accessed.

Exception Safety

No effects in case an exception is thrown.

Example 1

Let's see the simple example for default constructor:

#include <iostream>
#include <set>

using namespace std;

int main(void) {
   // Default constructor
   set<char> s;
  
   int size = s.size(); 

   cout << "Size of set s = " << size;
   return 0;
}

Output:

Size of set = 0

In the above example, s is an empty set therefore, size is 0.

Example 2

Let's see a simple example for range constructor:

#include <iostream>
#include <set>

using namespace std;

int main(void) {
   int evens[] = {2,4,6,8,10};
  
   // Range Constructor
   set<int> myset (evens, evens+5);  

   cout << "Size of set container myset is : " << myset.size();
   return 0;
}

Output:

Size of set container myset is: 5

In the above example, set myset is constructed with the elements of evens.

Example 3

Let's see a simple example for copy constructor:

#include <iostream>
#include <set>

using namespace std;

int main(void) {
   //Default Constructor
   std::set<int> s1;
   s1.insert(5);
   s1.insert(10);

   cout << "Size of set container s1 is : " << s1.size();
  
   // Copy constructor
   set<int> s2(s1);
   cout << "\nSize of new set container s2 is : " << s2.size();
   return 0;
}

Output:

Size of set container s1 is : 2
Size of new set container s2 is : 2

In the above example, s2 is a copy of s1 set.

Example 4

Let's see a simple example for move constructor:

#include <iostream>
#include <set>

using namespace std;

int main(void) {
   // Default constructor
   set<char> s1;
   s1.insert('x');
   s1.insert('y');

   cout << "Size of set container s1 is : " << s1.size();

   // Move constructor
   set<char> s2(move(s1));
   cout << "\nSize of new set container s2 is : " << s2.size();
   return 0;
}

Output:

Size of set container s1 is : 2
Size of new set container s2 is : 2

In the above example, contents of s1 are moved to s2 set.

Example 5

Let's see a simple example for initializer list constructor:

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

using namespace std;

int main() {
   // Initializer list constructor
   set<string> fruit {
      "orange", "apple", "mango", "peach", "grape"
   };

   cout << "Size of set container fruit is : " << fruit.size();
   return 0;
}

Output:

Size of set container fruit is : 5

The above example creates a set fruit with string as key and initializes it with initializer_list.


Next TopicC++ Set destructor




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