C++ set swap()C++ swap() function is used to swap (or exchange) the content of two sets but both the sets must be of same type although sizes may differ. SyntaxParameterx: set container to exchange the contents with. Return valueNone ComplexityConstant. Iterator validityAll references, iterators and pointers referring to elements in both set containers remain valid, but now are referring to elements in the other set container, and iterate in it. Data RacesBoth the container and x are modified. Exception SafetyNo effect on container if exception is thrown. Example 1Let's see the simple example to swap the element of one set to another: Output: Set contains following elements 1 2 3 4 5 In the above example, set m1 has five elements and m2 is empty. When you swap m1 to m2 then all the elements of m1 is swapped to m2. Example 2Let's see a simple example to exchange the content of two sets: Output: first set contains: 40 50 60 second set contains: 10 20 30 Example 3Let's see a simple example to swap the contents of two sets: Output: set1: x y z set2: a b c d In the above example, another form of swap() function is used to swap the contents of two sets. Example 4Let's see a simple example: Output: The original set s1 is: 10 20 30. After swapping with s2, list s1 is: 100 200. After swapping with s3, list s1 is: 300. Next TopicSet clear() 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