swap string a in place to string b in C++ [closed]

In C++, you typically use std::swap for exchanging two objects, e.g.

std::string a = "Hello", b = "world";
std::swap(a, b);

For std::string, there’s also std::basic_string::swap

Exchanges the contents of the string with those of other. All iterators and references may be invalidated.

a.swap(b);

Leave a Comment