C++ set emplace()C++ set emplace() function 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 key is not present already. SyntaxParameterargs: The arguments forwarded to construct an element to be inserted into the set. Return valueThe emplace() function returns a bool pair that will indicate if the insertion is occurred or not and returns an iterator pointing to the newly inserted element. ComplexityLogarithmic in the container size. Iterator validityNo changes. Data RacesThe container is modified. Iterating ranges in the container is not safe although concurrently accessing exiting elements is safe. Exception SafetyIf an exception is thrown, there are no changes in the container. Example 1Let's see the simple example to insert the elements into the set: Output: Set contains following elements a, b, c, d, e, In the above example, it simply insert the element into the set m with the given key value pairs. Example 2Let's see a simple example to insert the element and check for the duplicate key: Output: set modified, now contains 1 elements: (ten) Emplace failed, element with value "ten" already exists. The existing element is (ten) In the above example, elements are inserted in the set and when you try to use the same key "ten" then it will display an error message that key "ten" is already exist. Example 3Let's see a simple example to find the sum of the inserted elements: Output: Sum of elements is: 30 Example 4Let's see a simple example to insert the element: Output: Enter the number of family members: 3 Enter the name of each member: Bob Robin David Total member of family is: 3 Details of family members: Name ________________________ Bob David Robin In the above example, it simply inserts the elements by the user's choice. Next TopicSet emplace_hint() Function |
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