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:
- #include < iostream>
- using namespace std;
- int main()
- {
- char a[] = {"hello"};
- char b[] = {'h','e','l','l','o'};
- int la = 0;
- int lb = 0;
- cout< < "The length of a[]:"< < sizeof(a)/sizeof(char)< < endl;
- cout< < "The length of b[]:"< < sizeof(b)/sizeof(char)< < endl;
- system("Pause");
- return 0;
- }
You can modify the program:
- #include < iostream>
- using namespace std;
- int main()
- {
- char a[] = {"hello"};
- char b[] = {'h','e','l','l','o','\0'};
- 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;
- }
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:
- #include < iostream>
- using namespace std;
- int main()
- {
- char a[] = {"hello"};
- char b[] = {'h','e','l','l','o','\0'};
- a++;
- system("Pause");
- 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.
- # Include <iostream>
- Using namespace std;
- Int main ()
- {
- Char s [] = "abc ";
- Char * p = "abc ";
- S [0] = 'X ';
- Cout <s <endl;
- // P [0] = 'X'; Compilation Error
- Cout <p <endl;
- System ("Pause ");
- Return 0;
- }
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.