How to use wstring in C++?
- How to initialize and print a std::wstring?
- std::basic_string
- std::wstring
- The C++ wstring type, the wide character string program example
- std::to_wstring
- std::to_wstring in c++
- How to cout a wstring or wchar_t in C++
- 17.1 — std::string and std::wstring
- 17.2 — std::string construction and destruction
- 17.3 — std::string length and capacity
- 17.4 — std::string character access and conversion to C-style arrays
- 17.5 — std::string assignment and swapping
- 17.6 — std::string appending
- 17.7 — std::string inserting
- 18.1 — Input and output (I/O) streams
- basic_string Class
-
- C++ Code Examples for wstring file path
- C++ Code Examples for wstring wide
- C++ (Cpp) std::wstring Examples
- 对std::string和std::wstring区别的解释,807个赞同,有例子
If you got multi-bytes characters in std::wstring, two more things need to be done to make it work:
Include headers
1. #include
_setmode(_fileno(stdout), _O_U16TEXT)
These are the two classes that you will actually use. std::string is used for standard ascii and utf-8 strings. std::wstring is used for wide-character/unicode (utf-16) strings. There is no built-in class for utf-32 strings (though you should be able to extend your own from basic_string<> if you need one).
Although you will directly use std::string and std::wstring, all of the string functionality is implemented in the basic_string<> class. String and wstring are able to access that functionality directly by virtue of being templated. Consequently, all of the functions presented will work for both string and wstring. However, because basic_string is a templated class, it also means the compiler will produce horrible looking template errors when you do something syntactically incorrect with a string or wstring. Don’t be intimidated by these errors; they look far worse than they are!
In this lesson, we’ll take a look at how to construct objects of std::string, as well as how to create strings from numbers and vice-versa.
Once you’ve created strings, it’s often useful to know how long they are. This is where length and capacity operations come into play. We’ll also discuss various ways to convert std::string back into C-style strings, so you can use them with functions that expect strings of type char*.
Character access There are two almost identical ways to access characters in a string. The easier to use and faster version is the overloaded operator[]:
String assignment The easiest way to assign a value to a string is to use the overloaded operator= function. There is also an assign() member function that duplicates some of this functionality.
Appending Appending strings to the end of an existing string is easy using either operator+=, append(), or push_back() function.
Inserting Inserting characters into an existing string can be done via the insert() function.
Microsoft string class
The sequences controlled by an object of type basic_string are the Standard C++ string class and are usually referred to as strings, but they shouldn't be confused with the null-terminated C-style strings used throughout the C++ Standard Library. The Standard C++ string is a container that enables the use of strings as normal types, such as comparison and concatenation operations, iterators, C++ Standard Library algorithms, and copying and assigning with class allocator managed memory. If you need to convert a Standard C++ string to a null-terminated C-style string, use the basic_string::c_str member.
No comments:
Post a Comment