How can I swap strings? Can I use swap()
method to std::string
in the same ways as I can in std::wstring
in a C++ App?
Generally, in Introduction to C++ lessons, examples are started with string examples and end with them, while Modern C++ uses Strings, Wide Strings and Unicode Strings to support worldwide languages. Strings (std::string) uses char as the character type which means they are ASCII chars and they are an instantiation of the basic_string class template. In C++, there are several typedefs for common character types and one of them is std:string types that are defined in header <string>.
strings are the string class for byte characters represented with string and alphanumeric characters are stored and displayed in string forms. In other words, string stores for alphanumeric text are 1-byte chars, called char. Strings are the instantiation of the basic_string class template that uses char as the character type. In modern C++, simply we can define a string as below,
1 2 3 |
std::string str = "This is a String"; |
String has methods to append, assign, copy, align, replace or operate with other strings. These methods can be used in all string methods with their appropriate syntax. We can swap a string with another string with swap() method. Let’s see this method.
This is how to swap one string with another string in C++
The swap method swap()
, exchanges the content of the one string by the content of another string, generally in C++ string types presented with basic_string object of the same type and the swap method can be displayed as below,
Syntax:
1 2 3 |
void swap (basic_string& str); |
We can swap two strings by using one of its swap() method. For example we can use swap() method of string s1 with another string s2 as below,
1 2 3 |
s1.swap(s2); |
or we can use swap() method of string s2 with another string s1 as below,
1 2 3 |
s2.swap(s1); |
these both will produce the same result. Here string lengths may be different.
In another words, the value of content is exchanged with the value of the another content by calling swap method. In general, the swap method can be used as in this example,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#include <iostream> #include <string> int main() { std::string s1 = "This is The First String"; std::string s2 = "This is The Second String"; s1.swap(s2); std::cout << s1 << std::endl; std::cout << s2 << std::endl; getchar(); return 0; } |
and the string outputs will be,
1 2 3 4 |
This is The Second String This is The First String |
or we can use the method of the second wide string, both results will be same;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#include <iostream> #include <string> int main() { std::string s1 = "This is The First String"; std::string s2 = "This is The Second String"; s2.swap(s1); std::cout << s1 << std::endl; std::cout << s2 << std::endl; getchar(); return 0; } |
and the string outputs will be,
1 2 3 4 |
This is The Second String This is The First String |
As you see we can swap the content of strings easily by using this swap() method, it is very fast and easy, compatible with all CLANG C++ compilers like C++ Builder, Dev-C++, GNU C/C++, Visual C++, and some other C++ compilers.
Note that we can also use this swap() method with std::wstring in C++. Swapping other types i.e. wstring to string or string to wstring may require more conversion techniques and also may cause to loss of wide characters. Because in that situation, we try to convert each 2-bytes char to single-byte ASCII chars, that means we will lose wide chars higher than 255. Generally, swap of these types is not recommended, or check your strings about possible char losses before you do this.