In this post, you’ll discover how can we swap Wide Strings? Can I apply the swap() method to Wide Strings in the same way that I would to strings?
By learning the wide strings & swap () method to wide strings, it will help you build C++ applications with the use of the C++ app.
Generally, as an introduction to C++, in addition to int, float, double there is another data type, wstring that we use for alphanumeric variables. In C++ there are several typedefs for common character types are provided: String types are defined in header <string>. You can read more about Wide Strings here too: https://learncplusplus.org/it/learn-to-use-wide-strings-wstring-in-c/
Table of Contents
What is a Wide String?
wstrings are the string class for byte characters represented with wstring and alphanumeric characters are stored and displayed in string forms. In another terms, wstring stores for the alphanumeric text with 2-byte chars called wchar_t. Wide Strings are the instantiation of the basic_string class template that uses wchar_t as the character type. In C++ simply we can define a string as below,
1 2 3 |
std::wstring wstr = L"This is a Wide String"; |
string has methods to assign, copy, align, replace or to operate with other strings. These methods can be used in all string methods with their appropriate syntax. Here are the string types defined in std namespace with their char type and C++ standard.
wstring has methods to append, assign, copy, align, replace or operate with other wide strings. These methods can be used in all string methods with their appropriate syntax. We can swap a wide string with another wide string with swap() method. Let’s see this method.
How to Swap a Wide String with Another Wide String in C++
Swap Method swap(), exchanges the content of the one wide string by the content of another wide string, generally in C++ string types presented with basic_string object of the same type and the Syntax for the swap method can be displayed as below,
wstrings are the string class for byte characters represented with wstring and alphanumeric characters are stored and displayed in string forms. In another terms, wstring stores for the alphanumeric text with 2-byte chars called wchar_t. Wide Strings are the instantiation of the basic_string class template that uses wchar_t as the character type. In C++ , we can display a swap method as below,
Syntax:
1 2 3 |
void swap (basic_string& str); |
We can swap two wide strings by using one of its swap() method. For example we can use swap() method of wstring wstr1 with another wstring wstr2 as below,
1 2 3 |
wstr1.swap(wstr2); |
or we can use swap() method of wstring wstr2 with another wstring wstr1 as below,
1 2 3 |
wstr2.swap(wstr1); |
these both usage will result with same. Wide String lengths may be different.
Use the Swap method to exchange Wide strings
In another terms, the value of content is exchanged with the value of the another content by calling swap method. In general, 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::wstring ws1 = L"This is The First Wide String"; std::wstring ws2 = L"This is The Second Wide String"; ws1.swap(ws2); std::wcout << ws1 << std::endl; std::wcout << ws2 << std::endl; getchar(); return 0; } |
and the Wide String outputs will be,
1 2 3 4 |
This is The Second Wide String This is The First Wide String |
or we can use method of 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::wstring ws1 = L"This is The First Wide String"; std::wstring ws2 = L"This is The Second Wide String"; ws2.swap(ws1); std::wcout << ws1 << std::endl; std::wcout << ws2 << std::endl; getchar(); return 0; } |
and the Wide String outputs will be,
1 2 3 4 |
This is The Second Wide String This is The First Wide String |
A summark of Wide Strings
As you see we can swap the content of wide 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 string 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, this is not recommended, or check your strings about possible char losses before you do this.
Design. Code. Compile. Deploy.
Start Free Trial
Free C++Builder Community Edition