swap() in C++

Learn via video course
FREE
View all courses
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
Topics Covered

Overview

The swap C++ function is a simple and useful function used to swap the values stored in variables. Using the swap C++ function, we can swap the value stored in two variables without using a third or temporary variable. Let us learn more about the swap C++ function and its implementation.

Transform Your Career

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program

Syntax of swap() in C++

The syntax of the swap() in C++ is as follows:

Parameters of swap() in C++

Now that we know the syntax of the swap() function in C++ Let's look at the parameters passed to the swap() function in C++.

  • a - The variable whose value needs to be swapped with the value of variable b.
  • b - The variable whose value needs to be swapped with the value of variable a.

Return Value of swap() in C++

The Return Value of the swap function is nothing. Therefore the swap C++ function has a void return type.

Exceptions of swap() in C++

The swap function in C++ works in most cases and has no exceptions.

Free Courses by top Scaler instructors
Python Course for Beginners With Certification: Mastering the Essentials
Java Course - Mastering the Fundamentals
DBMS Course - Master the Fundamentals and Advanced Concepts
JavaScript Course With Certification: Unlocking the Power of JavaScript
C++ Course: Learn the Essentials
Python and SQL for Data Science Course
Python Course for Beginners With Certification: Mastering the Essentials
Java Course - Mastering the Fundamentals
DBMS Course - Master the Fundamentals and Advanced Concepts
JavaScript Course With Certification: Unlocking the Power of JavaScript
C++ Course: Learn the Essentials
Python and SQL for Data Science Course

Complexity

The swap function in C++ has constant complexity. If the swap function is used on an array of n elements, then the swap function has the complexity of n.

Scaler Placement Report and Statistics

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more

Examples

Now we know the syntax of swap() in C++. Let us look at some examples and understand how to use the swap() function in our programs.

Using std::swap() with variables

code

output

In the above code, The swap function swaps the value stored in variables a and b.

Using std::swap() with Arrays

output

In the above code, The swap function swaps the value stored in the array a and b.

Turn Learning into Career Growth

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

Using std::swap() with Characters

output

In the above code, The swap function swaps the char value stored in the array a and b.

Using std::swap() with String

code

output

In the above code, The swap function swaps the value stored in the string a and b.

How to Swap Two Variables in C++ without Using the std

Swapping two numbers using a temporary variable

output

In the above code, We created a temporary variable temp to swap the values stored in the variables a and b.

You can learn more about swapping two numbers in the Swapping of Two Numbers in Java article.

Swapping two numbers using an arithmetic operation

output

In the above code, We used a basic arithmetic operation to swap the value stored in two variables, a and b.

How Does std::swap() in C++ Work?

The swap C++ function is used to swap the value of two variables. Using the swap C++ function, we need not use a third temporary variable or the Arithmetic Operators method to swap the value stored in two variables. The swap C++ function takes two variables as it's a parameter. The swap C++ function swaps the values stored in the variables, passed as the parameter. The swap C++ function swaps the variable's value and doesn't return anything hence swap C++ function has a void return type.

Conclusion

  • The swap C++ function swaps the values stored in two variables.
  • The syntax of the swap function is swap(a,b).
  • The swap C++ function takes two variables as parameters.
  • The value of the variables passed as the parameter will be swapped.
  • The swap C++ swaps the value of the variables passed as the parameter.