Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/string/basic string/swap"

From cppreference.com
< cpp‎ | string‎ | basic string
Line 39: Line 39:
  
 
  | output=
 
  | output=
 +
before swap
 +
a: AAA
 +
b: BBB
 +
after swap
 +
a: BBB
 +
b: AAA
 
}}
 
}}
  

Revision as of 05:02, 18 October 2013

 
 
 
std::basic_string
Member functions
Element access
Iterators
Capacity
Modifiers
basic_string::swap
Search
Operations
Constants
Non-member functions
I/O
Comparison
(until C++20)(until C++20)(until C++20)(until C++20)(until C++20)(C++20)
Numeric conversions
(C++11)(C++11)(C++11)
(C++11)(C++11)
(C++11)(C++11)(C++11)
(C++11)
(C++11)
Literals
Helper classes
Deduction guides (C++17)

 
void swap( basic_string& other );

Exchanges the contents of the string with those of other. All iterators and references remain valid.

Contents

Parameters

other - string to exchange the contents with

Return value

(none)

Example

#include <string>
#include <iostream>
 
int main() {
  std::string a = "AAA";
  std::string b = "BBB";
 
  std::cout << "before swap" << std::endl;
  std::cout << "a: " << a << std::endl;
  std::cout << "b: " << b << std::endl;
 
  a.swap(b);
 
  std::cout << "after swap" << std::endl;
  std::cout << "a: " << a << std::endl;
  std::cout << "b: " << b << std::endl;
 
  return 0;
}

Output:

before swap
a: AAA
b: BBB
after swap
a: BBB
b: AAA

Complexity

constant