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

What is the use of C++ string

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly shows you "what is the use of the C++ string". It is easy to understand and clear. I hope it can help you solve your doubts. Now let the editor lead you to study and learn the article "what is the use of the C++ string?"

1.Classical + traditional string of string

A) char ch2 [] = {"liangdiamond"}

B) char ch3 [] = {"hello world"}

There are several commonly used functions about traditional strings

A) strcpy () function

B) strcat () function

C) strlen () function

D) strcmp () function

E) strlwr () function: change uppercase to lowercase

F) strupr () function, changing lowercase to uppercase

2. Character array of C++ strings

On the surface, a string is an array of characters, but in the C++ language, they are different. A string is a character array that ends with'\ 0'. The following code shows the difference between a string and a character array:

# include

< iostream>

Using namespace std; int main () {char a [] = {"hello"}; char b [] = {'hype, int la, int lb, cout

< < "The length of a[]:"< < sizeof(a)/sizeof(char)< < endl; cout< < "The length of b[]:"< < sizeof(b)/sizeof(char)< < endl; system("Pause"); return 0; } 可以修改程序: #include < iostream>

Using namespace std; int main () {char a [] = {"hello"}; char b [] = {'henceforth; int la = 0; int lb = 0; cout

< < "b="< < b< < endl; cout< < "The length of a[]:"< < sizeof(a)/sizeof(char)< < endl; cout< < "The length of b[]:"< < sizeof(b)/sizeof(char)< < endl; system("Pause"); return 0; } 但是数组名就是数组的首地址,所以数组名本身就可以理解为一个指针,只不过,它是指针常量,所谓指针常量,就是指针不能改变所指向的地址了,但是它所指向的地址中的值可以改变。 例如: #include < iostream>

Using namespace std; int main () {char a [] = {"hello"}; char b [] = {'hindsight system ("Pause"); return 0;}

Compilation error.

Although the character array and character pointer in C++ string are very similar in form, there is still a big difference in the allocation and use of memory space. The array name is not a runtime entity, so the array itself has space, which is allocated by the compiler. The pointer is a variable (runtime entity), and whether the space it points to is legal or not is determined at run time.

# include

< iostream>

Using namespace std; int main () {char s [] = "abc"; char* p = "abc"; s [0] ='x stories; cout < < s < < endl; / / p [0] ='x stories; compilation error cout < < p < < endl; system ("Pause"); return 0;}

The address of S and the address pointed to by p are not the same place, so it can be inferred that 0x00417704 is a constant area. So it doesn't seem to change. You can actually see it more clearly with disassembly. The good thing about the pointer is that it can point to memory, not all ROM areas seem to be able to change.

The above is all the contents of the article "what are the uses of C++ strings?" 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