C++ Algorithm swap() FunctionLast Updated : 17 Mar 2025 C++ Algorithm swap() function swaps or say interchanges the values of two containers under reference. SyntaxParametera: It is the first container with some value. b: It is another container with some value. Return valueThe function only swaps the values of two containers and does not return something. Example 1Output: sg contains: 14 14 14 14 14 14 Example 2Output: Value of ss before swapping: 9 Value of sg before swapping: 14 Value of ss after swapping: 14 Value of sg after swapping: 9 ComplexityFor arrays the function has N complexity as the operation of swapping is individually performed on each element. For non array the function has constant complexity. Data racesBoth the containers are undergo modification ExceptionsThe function throws an exception if any of the container elements throws one. Next TopicC++ Algorithm fill() |
We request you to subscribe our newsletter for upcoming updates.

We deliver comprehensive tutorials, interview question-answers, MCQs, study materials on leading programming languages and web technologies like Data Science, MEAN/MERN full stack development, Python, Java, C++, C, HTML, React, Angular, PHP and much more to support your learning and career growth.
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India