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

An example Analysis of the underlying principle of string in C++

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

Share

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

Editor to share with you an example analysis of the underlying principles of string in C++, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to understand it!

1. Deep and shallow copy

Shallow copy:

If you do not string a copy construct first when implementing string, a copy constructor is automatically generated, but it is only a shallow copy. Two string objects point to the same address. When two objects call the destructor, the destructor called by the former object has released the internal followers of this address, and the latter will repeatedly release the block space, resulting in an error.

The breakpoint will be triggered and an error will be reported.

Class string {public:/*string (): _ str (new char [1]) {* _ str ='\ 0examples;} * / / string (const char* str = "\ 0") error demonstration / / string (const char* str = nullptr) error demonstration string (const char* str = "") {/ / when constructing string class objects, if the nullptr pointer is passed and the program is considered illegal, if (nullptr = str) {assert (false); return is asserted here } _ str = new char [strlen (str) + 1]; strcpy (_ str, str);} ~ string () {if (_ str) {delete [] _ str;_str = nullptr;}} private:char* _ str;}; / / Test void Teststring () {string S1 ("hello bitbacks!"); string S2 (S1);}

Note: the above string class does not explicitly define its copy constructor and assignment operator overload, at this time the compiler will synthesize the default, when using S1 to construct S2, the compiler will call the default copy construction. The final problem is that S1 and S2 share the same memory space, and the same space is released many times when it is released, which causes the program to crash. This copy mode is called shallow copy.

If a resource is managed in an object, it will eventually cause multiple objects to share the same resource, and when an object is destroyed, the resource will be released, while other objects do not know that the resource has been released and think it is still valid. therefore, when you continue to enter the resource, an access violation will occur. To solve the problem of shallow copy, deep copy is introduced into C++.

Deep copy

If resource management is involved in a class, its copy constructor, assignment operator overload, and destructor must be explicitly given. It is generally provided in the form of a deep copy.

2. Principle of string iterator

The string iterator actually looks like this

Typedef char* Iterator; typedef const char* const_Iterator; typedef char* reserve_Iterator

In fact, the pointer traverses the string by using the interfaces begin (), end (), rend (), rbegin (), cend (), and dbegin () to move the pointer forward and backward.

Typedef char* Iterator; Iterator begin () {return str;} Iterator end () {return str + _ size;} string::iterator it=s.begin () While (itineraries. End ()) {coutstr = s; strcpy (this- > str, st.str);} return * this;} * /

The idea and copy structure are basically the same. Deep copy is used to create a space as large as this to copy the contents of str to this.

Reserve ()

Void reserve (size_t num) {if (num > = _ capasity) {char* str1 = new char [num + 1]; strcpy (str1,this- > str) Delete [] str; this- > str = str1; _ capasity = num }}

If num is smaller than capacity and larger than capacity, it will expand its capacity, open up a memory with the size of num, and then copy the contents of this to the newly opened memory.

Push_back () and append ()

Void push_back (char ch) {if (_ size > = _ capasity) {size_t num = _ capasity = = 0? 4: 2 * _ capasity; this- > reserve (num);} str [_ size] = ch _ size++; str [_ size] ='\ 0marker; / /\ 0 flag string end} void append (const char* ch) {size_t len = strlen (ch) If (_ size + len > _ capasity) {this- > reserve (_ size + len);} strcpy (this- > str+_size,ch); _ size + = len;}

Resize ():

Void resize (size_t num,char ch='\ 0') {if (num _ size) {this- > str [num] ='\ 0mm; this- > _ size = num } else {if (num > _ capasity) {reserve (num) } for (int I = _ size; I < num; iTunes +) {str [I] = ch;} _ size = num; str [num] ='\ 0' }} size_t size () {return _ size;} size_t capacity () {return _ capasity;}

It is divided into three categories:

1.num is smaller than size (), just add\ 0 to strSize.

2.num is bigger than size and smaller than capacpty. Copy size to num in str to ch.

3.num is larger than capacpty. First, expand the capacity, and then copy size to num to ch.

The above is all the contents of this article entitled "sample Analysis of the underlying principles of string in C++". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

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