C++ std::string swap() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
std::string

Definition

In C++, std::string::swap() exchanges the contents of two strings.

After the call, each string holds what the other used to hold. Include <string>.

This is a member of std::string. A string grows as you append, stores characters contiguously, and can reallocate—after growth, old iterators and references may be invalid. Prefer at() when you want bounds checks; remember find returns npos when missing.

Syntax

Call as a member or with std::swap.

C++
a.swap(b);
std::swap(a, b);
CallWhat it does
a.swap(b)Exchange contents of a and b.
std::swap(a, b)Usually calls the member swap.

Swap does not throw for std::string with the default allocator.

Library declarations
C++
void swap(string& other) noexcept;

Parameters

ParameterDescription
otherThe other string to exchange with.

Return Value

Return ValueDescription
voidNo return value.

C++ Examples

Example 1 — Basic swap()

Exchange two words.

C++
#include <iostream>
#include <string>

int main() {
    std::string a = "left";
    std::string b = "right";
    a.swap(b);
    std::cout << a << ' ' << b << '\\n';
    return 0;
}

How It Works

  1. a starts as left, b as right.
  2. swap exchanges their contents.
  3. Printed order shows the exchange.

Example 2 — Using std::swap

Same effect via std::swap.

C++
#include <iostream>
#include <string>
#include <utility>

int main() {
    std::string x = "1";
    std::string y = "2";
    std::swap(x, y);
    std::cout << x << y << '\\n';
    return 0;
}

How It Works

  1. std::swap finds the efficient string swap.
  2. Values are exchanged.
  3. Output is 21.

Example 3 — Swap With Self

Swapping a string with itself is a no-op.

C++
#include <iostream>
#include <string>

int main() {
    std::string s = "same";
    s.swap(s);
    std::cout << s << '\\n';
    return 0;
}

How It Works

  1. Self-swap is allowed.
  2. Contents stay identical.
  3. Useful to know it is safe.

Frequently Asked Questions

It exchanges the contents of two strings.
Usually yes—often pointer exchange rather than copying characters.
For std::string with equal allocators it is noexcept.
Either. std::swap typically calls the member.
Not typically for large strings; implementations exchange buffers.

Next: getline()

Keep going through the string functions. Read a whole line from an input stream into a std::string.

getline() tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

10 people found this page helpful