Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The basic concept of English string in C++

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "the basic concept of English string in C++". The content of the explanation in the article is simple and clear, and it is easy to learn and understand. let's study and learn the basic concept of English string in C++.

The template prototype of string for English string class in C++ is basic_string.

Template

< class _Elem, class traits = char_traits< _Elem>

Class _ Ax = allocator

< _Elem>

>

Class basic_string {}

* * the parameter _ Elem indicates the type. The default value of the second parameter, traits, uses the char_traits type, which defines functions for type and character manipulation, such as comparison, equivalence, allocation, and so on. The default value of the third parameter _ Ax is the allocator class, which represents the memory mode, and different memory structures will manipulate the different behavior of the pointer, such as stack, heap, or segment memory mode, and so on.

Two strings string and wstring are defined in the C++ standard

Typedef basic_string

< char>

String; typedef basic_string

< wchar_t>

Wstring

The former string is a commonly used type and can be regarded as char [], which is in fact consistent with _ Elem=char in the definition of string. Wstring, on the other hand, uses the wchar_t type, which is a wide character and is used to meet the requirements of non-ASCII characters, such as Unicode encoding, Chinese, Japanese, Korean and so on. For wchar_t types, C++ actually uses the functions of wchar_t corresponding to the char function, because they are all defined from the same template in a similar way. Therefore, there are also wcout, wcin, werr and other functions.

In fact, string can also use Chinese, but it writes one Chinese character in two char. If a Chinese character is regarded as a unit wchar_t, then only one unit is occupied in the wstring, and the same is true of other non-English characters and codes. Only in this way can we really meet the requirements of string operation, especially internationalization and other work.

Take a look at the following procedure, you will understand the difference between C++ Chinese and English strings.

# include

< iostream>

# include

< string>

Using namespace std

# define tab "\ t"

Int main ()

{

Locale def

Cout < < def.name () < < endl

Locale current = cout.getloc ()

Cout < < current.name () < < endl

Float val=1234.56

Cout < < val < < endl

/ / chage to french/france

Cout.imbue (locale ("chs"))

Current=cout.getloc ()

Cout < < current.name () < < endl

Cout < < val < < endl

/ / the above explains the use of locale, and the following is the content of this example, because the imbue function is used

Cout < < "* *" < < endl

/ / to ensure localized output (text / time / currency, etc.)

Chs represents China, and wcout must use localized parsing coding

Wcout.imbue (std::locale ("chs"))

/ / string English, the position is reversed correctly, and the second character is displayed correctly

String str1 ("ABCabc")

String str11 (str1.rbegin (), str1.rend ())

Cout < < "UK\ ts1\ t:" < < str1 < < tab < < str1 [1] < <

Tab < < str11 < < endl

/ / wstring English, the position is reversed correctly, and the second character is displayed correctly

Wstring str2=L "ABCabc"

Wstring str22 (str2.rbegin (), str2.rend ())

Wcout < < "UK\ tws4\ t:" < < str2 < < tab < < str2 [1]

< < tab < < str22 < < endl

/ / string Chinese, turned upside down, becomes garbled, and the second character is also read incorrectly

String str3 ("how are you?")

String str33 (str3.rbegin (), str3.rend ())

Cout < < "CHN\ ts3\ t:" < < str3 < < tab < < str3 [1]

< < tab < < str33 < < endl

/ / the correct way to print the second character

Cout < < "CHN\ ts3\ t:RIGHT\ t" < < str3 [2] < < str3 [3] < < endl

/ / Chinese, the position is reversed correctly, and the second character is displayed correctly

Wstring str4=L "how are you?"

Wstring str44 (str4.rbegin (), str4.rend ())

Wcout < < "CHN\ tws4\ t:" < < str4 < < tab < < str4 [1]

< < tab < < str44 < < endl

Wstring str5 (str1.begin (), str1.end ())

/ / only string of type char can be constructed in this way

Wstring str55 (str5.rbegin (), str5.rend ())

Wcout < < "CHN\ tws5\ t:" < < str5 < < tab < <

Str5 [1] < < tab < < str55 < < endl

Wstring str6 (str3.begin (), str3.end ()); / / this construction will fail!

Wstring str66 (str6.rbegin (), str6.rend ())

Wcout < < "CHN\ tws6\ t:" < < str6 < < tab < <

Str6 [1] < < tab < < str66 < < endl

Return 0

}

Thank you for your reading. the above is the content of "the basic concept of English string in C++". After the study of this article, I believe you have a deeper understanding of the basic concept of English string in C++. The specific use also needs to be verified by practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report