当前位置:网站首页>String class and learning documents

String class and learning documents

2022-06-10 10:58:00 Hua Weiyun

【 Write it at the front 】

Here we have to learn to read documents by ourselves , Because that's it string Class, there are more than 100 interface functions , Then I can't remember , We usually use about twenty , Generally, we study it, which is commonly used , Others are probably familiar with their functions , If you really want to use it, then check the documents . You can see that in fact string Is a sequence table that manages character arrays , Because character arrays are widely used ,C++ Just for one string class , Due to coding reasons , It's written as a template . in the light of string, Generally, it has three members —— char* _str、size_t _size、size_t _capacity, Let's simulate and implement string You'll see , Secondly, when learning deep and shallow copies, we only use char* _str.
 Insert picture description here

C++ Strictly speaking, there are two document libraries : Note that the former is not C++ Official website documentation , The latter is . Here we take the official documents as a reference , Because it's messy , Usually use it cplusplus That's enough .

  1. cplusplus
  2. cppreference

In addition, there is a deep and shallow copy of knowledge about classes and objects, which we will add here .

One 、 Why study string class

C Strings in languages

C In language , String is based on ‘\0’ A collection of characters at the end , For ease of operation ,C The standard library provides some str Series of library functions ,
But these library functions are separate from strings , Not quite in line with OOP Thought , And the underlying space needs to be managed by users , A little carelessness can
And cross-border visits .

Two interview questions ( Don't explain for the moment )

String to integer number

String addition

stay OJ in , Questions about strings are basically based on string Class , And in routine work , For simplicity 、 convenient 、 quick , Basically use string class , Few people use C String manipulation functions in the library .

Two 、 In the standard library string class

string class ( understand )

string Class documentation

  1. A string is a class that represents a sequence of characters .
  2. The standard string class provides support for such objects , Its interface is similar to that of a standard character container , However, a design feature has been added specifically for manipulating single byte character strings .
  3. string Class is to use char( As its character type , Use its default char_traits And distributor type ( More information about templates , see also basic_string).
  4. string Class is basic_string An instance of the template class , It USES char To instantiate basic_string Template class , And use char_traits and allocator As basic_string Default parameters ( For more template information, please refer to basic_string).
  5. Be careful , This class handles bytes independently of the encoding used : If used to handle multi byte or variable length characters ( Such as UTF-8) Sequence , All members of this class ( Such as length or size ) And its iterators , Will still be in bytes ( Not actually encoded characters ) To operate .

summary :

  1. string Is a string class that represents a string .
  2. The interface of this class is basically the same as that of a regular container , And added some special operations for string General operation of .
  3. string At the bottom, it's actually :basic_string Alias of template class ,typedef basic_string<char, char_traits, allocator> string;.
  4. Cannot manipulate sequences of multibyte or variable length characters .
  5. In the use of string Class time , Must contain string Header files and using namespace std;.

 Insert picture description here

#include<string>#include<iostream>using namespace std;int main(){	cout << sizeof(char) << endl;	cout << sizeof(wchar_t) << endl;	char arr1[] = "hello bit";	char arr2[] = " The bit ";	return 0;}

explain

For characters ,C++ Two types are proposed in , At this stage, the contact is the first .

You can see that one of the results is 1 and 2, This thing has something to do with coding .

code

Only binary is stored in the computer 0 and 1, How to express words ???

At this time, the corresponding coding table is established :

  1. ASCII > English support
    1Byte = 8Bit,0 - 255 ASCII The coding table is right 256 A value establishes a corresponding representation value
     Insert picture description here

  2. GBK > China has formulated , Commonly used in windows Next
    In the early windows In order to enter the Chinese market , Just use this code

  3. utf-8 > Universal , compatible ASCII, Commonly used in Linux Next
    Countries all over the world are beginning to use computers , In the early days, computers could only express English , Words that cannot represent other countries , So you need to create your own coding table , But it's very messy to do their own things , So there it is utf-8, In the early days UniCode

So according to incomplete statistics, there are about 100000 Chinese characters , So we use 2 Bytes , Probably means 6 Ten thousand states

原网站

版权声明
本文为[Hua Weiyun]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/161/202206101048011562.html