C ++ string usage

Source: Internet
Author: User

As you know, in C ++ programming languages, string operations are a basic and important operation skill. C ++ strings can be expressed in the following two forms: Traditional strings and character arrays.

  • Common usage of C ++ templates
  • C ++ global Function Application Guide
  • C ++ references Basic Concepts
  • C ++ tips for getting file sizes
  • C ++ exception concepts

1. Traditional C ++ strings

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

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

There are several common functions about traditional strings.

A) strcpy () function

B) strcat () function

C) strlen () function

D) strcmp () function

E) strlwr () function: converts uppercase letters to lowercase letters.

F) strupr () function, which is written in uppercase in lowercase.

2. character array of C ++ strings

On the surface, a string is a character array, but they are different in c ++. The string is an array of bytes ending with '\ 0. The following code shows the differences between character strings and character Arrays:

 
 
  1. #include < iostream> 
  2. using namespace std;  
  3. int main()  
  4. {  
  5. char a[] = {"hello"};  
  6. char b[] = {'h','e','l','l','o'};  
  7. int la = 0;  
  8. int lb = 0;  
  9. cout< < "The length of a[]:"< < sizeof(a)/sizeof(char)< < endl;  
  10. cout< < "The length of b[]:"< < sizeof(b)/sizeof(char)< < endl;  
  11. system("Pause");  
  12. return 0;  

You can modify the program:

 
 
  1. #include < iostream> 
  2. using namespace std;  
  3. int main()  
  4. {  
  5. char a[] = {"hello"};  
  6. char b[] = {'h','e','l','l','o','\0'};  
  7. int la = 0;  
  8. int lb = 0;  
  9. cout< < "b="< < b< < endl;  
  10. cout< < "The length of a[]:"< < sizeof(a)/sizeof(char)< < endl;  
  11. cout< < "The length of b[]:"< < sizeof(b)/sizeof(char)< < endl;  
  12. system("Pause");  
  13. return 0;  

But the array name is the first address of the array, so the array name itself can be understood as a pointer, but it is a pointer constant, so-called pointer constant, that is, the pointer cannot change the address it points to, but the value in the address it points to can be changed.
For example:

 
 
  1. #include < iostream> 
  2. using namespace std;  
  3. int main()  
  4. {  
  5. char a[] = {"hello"};  
  6. char b[] = {'h','e','l','l','o','\0'};  
  7. a++;  
  8. system("Pause");  
  9. return 0;  

Compilation error.

Although the character arrays and character pointers in C ++ strings are very similar in form, they are quite different in terms of memory space allocation and usage. The array name is not a runtime object, so the array itself has space, which is allocated by the compiler. The pointer is a variable runtime entity). Whether the space it points to is legal depends on the runtime.

 
 
  1. # Include <iostream>
  2. Using namespace std;
  3. Int main ()
  4. {
  5. Char s [] = "abc ";
  6. Char * p = "abc ";
  7. S [0] = 'X ';
  8. Cout <s <endl;
  9. // P [0] = 'X'; Compilation Error
  10. Cout <p <endl;
  11. System ("Pause ");
  12. Return 0;
  13. }

The S address and the p address are not in the same place. We can infer that 0x000000704 is a constant zone. So it seems that it cannot be changed. In fact, it is clearer to use disassembly. If the pointer is good, it can point to the memory, not the ROM area.

The preceding section describes C ++ strings.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.