C++ set constructor30 Aug 2024 | 4 min read There are following five uses of set constructor:
SyntaxDefault constructorrange constructorcopy constructormove constructorinitializer list constructorParametercomp: 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 valueConstructor never returns any value. ComplexityFor 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 validityInvalidate all pointers, iterators, and references related to x if the elements of set container are moved in the move constructor. Data RacesAll copied elements are accessed. Exception SafetyNo effects in case an exception is thrown. Example 1Let's see the simple example for default constructor: Output: Size of set = 0 In the above example, s is an empty set therefore, size is 0. Example 2Let's see a simple example for range constructor: Output: Size of set container myset is: 5 In the above example, set myset is constructed with the elements of evens. Example 3Let's see a simple example for copy constructor: 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 4Let's see a simple example for move constructor: 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 5Let's see a simple example for initializer list constructor: 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 |
set begin() function
C++ set begin() C++ is used to return an iterator referring to the first element of the set container. Syntax iterator begin(); ...
3 min read
set destructor
C++ ~ C++ is used to destroy all the elements of set container and deallocate all the storage memory allocated by the set container. Syntax ~set(); Parameter None Return value None Complexity Linear in set::size (destructors). Iterator validity All iterators, references and pointers are invalidated. Data Races The container set and all its elements are modified. Exception Safety This function...
1 min read
set emplace_hint() function
C++ set emplace_hint() C++ is used to extend the set container by inserting new elements into the container using hint as a position for element. Elements are built directly (neither copied nor moved). The constructor of the element is called by giving the arguments args passed to...
4 min read
set key_comp() function
C++ set key_comp() C++ is used to return a copy of the comparison object which is used by the set container to compare keys. The comparison object can be used to compare key values of two elements in a container. This comparison object is given when constructing...
5 min read
set emplace() function
C++ set emplace() C++ is used to extend the set container by inserting new elements into the container. Elements are built directly (neither copied nor moved). The constructor of the element is called by giving the arguments args passed to this function. Insertion takes place only if...
5 min read
set count() function
C++ set count() C++ is used to return the number of elements found in the container. Since, the set container does not contain any duplicate element, this function actually returns 1 if the element with value val is present in the set container or 0 otherwise....
4 min read
set value_comp() function
C++ set value_comp() C++ returns a comparison object. This function is used to compare two elements to check whether the key of the first one goes before the second. It takes two arguments of the same type and returns true if the first argument precedes the second...
4 min read
set operator=() function
C++ set operator= There are following three uses of operator= in set: Operator= is used to assign new content to the set container by replacing its old content (or copy the content) and modifies size if necessary. Operator= is used to move the content of one set container into...
4 min read
set rend() function
C++ set rend() C++ is used to return an iterator to the end of the set (not the last element but the past last element) in reverse order. This is similar to the element preceding the first element of the non-reversed container. Note:- This is a...
4 min read
set get_allocator() function
C++ set get_allocator() C++ is used to return the copy of allocator object which helps to construct the set container. Syntax allocator_type get_allocator() const; //until C++ 11 allocator_type get_allocator() const noexcept; //since C++ 11 Parameter None Return value Returns an allocator associated...
4 min read
We request you to subscribe our newsletter for upcoming updates.

We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India