std::string str1 = "hello"; std::string str2 = "world"; str1.swap(str2); std::cout << str1 << " " << str2 << std::endl;
world helloIn this example, the values of str1 and str2 are swapped using the swap function. The output shows that the values of the two strings have been successfully exchanged. Package library: C++ Standard Library.