C++ multiset constructorThere are following five uses of multiset constructor:
SyntaxDefault constructor range constructor copy constructor move constructor initializer list constructor Parametercomp: A comparison function object which takes two key arguments and returns true if first argument goes before the second argument otherwise, it returns false. By default, it uses less<key_type> predicate. alloc: An 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 multiset 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 multiset 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 multiset = 0 In the above example, s is an empty multiset therefore, size is 0. Example 2Let's see a simple example for range constructor: Output: Size of multiset container mymultiset is: 5 In the above example, multiset mymultiset is constructed with the elements of evens. Example 3Let's see a simple example for copy constructor: Output: Size of multiset container s1 is: 2 Size of new multiset container s2 is: 2 In the above example, s2 is a copy of s1 multiset. Example 4Let's see a simple example for move constructor: Output: Size of multiset container s1 is: 3 Size of new multiset container s2 is: 3 In the above example, contents of s1 are moved to s2 multiset. Example 5Let's see a simple example for initializer list constructor: Output: Size of multiset container fruit is: 5 The above example creates a multiset fruit with string as key and initializes it with initializer_list. Next TopicC++ multiset |
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