C-String
In C + +, there are two types of strings, one is inherited from C, called C-string, referred to as C-strings, not called ASCII string, C-string is a full 0-bit (integer 0) as a terminator character sequence, the full 0 bytes is both 8-bit integer 0, also ASCII code 0.
The space length of the C-string is string length plus 1, for example:
<span style= "FONT-SIZE:14PX;" ><span style= "FONT-SIZE:14PX;" >char buffer[7] = "hello!"; /If char buffer[6] = "hello!"; is an error. </span></span>
What is the type of C-string? The C-String type is char* (character pointer), which is more precise and is a const char*. It is the same as the character array, but the operation is the same, representing the starting address of the C-string
<span style= "FONT-SIZE:14PX;" ><span style= "FONT-SIZE:14PX;" >char* str = "Hello";
cout << *str <<endl; Show h
cout << str << Endl;//Show hello</span></span>
STR is a character pointer variable, *STR is an indirect reference to a character pointer variable, an indirect reference to the output character pointer, or a single character output. Str points to the first address of "Hello", so the output *str represents the value on the space represented by the address-"H". The output character pointer is the output C-string, so when you output STR, it's starting with the address of the ' H ' character, outputting all the characters until you encounter 0, because the C-string type is a character pointer, so comparing two C-strings is different depending on the space position, in the library function, A function specifically designed to perform a series of operations on a C-string: char* strcpy (char* x1, char* x2);//copy function, which means that the X2 string is copied to the location of the X1, the original content of X1 is overwritten, the function is called to return the first address of the X1, The purpose is to allow the result of the invocation to participate directly in the string operation after the note that because the length of the X2 string may be longer than the X1 string space, strcpy is not safe to use, and if the length of a character array is 3 (less than the length of the S1), execute strcpy (A, S1) will make the data in the adjacent memory space close to the array of a arrays also modified, causes unpredictable error int strcmp (char* x1, char* x2);//comparison function, which indicates that the X2 string is compared to the X1 string, if the X1 is small, the return value is negative, and the X1 returns a positive value, The equivalent returns 0 char* strcat (char* x1, char* x2)//Join function, which concatenates the X2 words to the X1 string, which obviously adds a long X1 string, or, after the end character 0, when the remainder of the X1 string does not have enough space to accept the X2 string, Invoking the operation will not secure char* Strrev (char* x);//inverted function, which inverts the x string and changes the original storage char* Strset (char* x, char b);/Set function, which replaces each character of the x string with B characters char* Strstr (char* x, char* s);/lookup function, find the S string in the X string, if found, return 1, otherwise return 0 char* strchr (char* x, char b);/lookup function, find B character in x string, if found, return 1, otherwise return 0
These are some long C-string library functions ..., these library functions operate by default in the header file of string.h string
String is a custom type that the STL provides, it handles the space occupation question is automatic, needs how many, uses how many, does not like the character pointer, is afraid in the pointer decoupling space free, it can from C string conversion obtains, can also convert to C-string ...
String itself is designed for convenient strings, as in the previous C-string function, string can be done, the use of simple, for example:
<span style= "FONT-SIZE:14PX;"
> #include <iostream> #include <algorithm> using namespace std;
------------------int Main () {string A, S1 = "Hello", S2 = "123"; A = S1; <span style= "color: #3333FF;" >//copy </span> cout << (A = = S1? "": "Not") << "equal" << Endl; <span style= "color: #3333FF;" >//comparison </span> cout << (A + + s2) << Endl; <span style= "color: #3333FF;" >//Connection </span> Reverse (A.begin (), A.end ());
Inverted cout << a << Endl; cout << a.replace (0, 9, 2, ' C ') << Endl; setting, a string of 9 substrings starting with subscript 0 is replaced by 2 ' C ' characters cout << (S1.find ("ell")!=-1? "": "Not") << "found\n" << endl;//Find string cout << (S1.find (' C ')!=-1? ":" Not ") <<" found\n " << endl;//Lookup character} </span>
From the above code you can see the simple and powerful function of string strings, in the C + + STL standard, the iostream resource is not containing string resources, and the BCB compiler is friendly, The default contains string and String.h resources, and note that string and string.h are not the same, string resources refer to string literals, and String.h header files are collections of library functions for many character space operations ... Yes, the reverse function is contained in the resource algorithm, which functions to invert all elements in the container
How string is defined:
<span style= "FONT-SIZE:14PX;" >//the original definition
string s = "Hello", t;</span>
Similar to integer definition
<span style= "FONT-SIZE:14PX;" ><span style= "FONT-SIZE:12PX;" >//the definition of parameterization </span>
int A (a);
String S ("Hello");
String T (3, ' H '); <span style= "font-size:12px;" >//3 h constitutes a string t</span> string
u (3); <span style= "font-size:12px;" >//3 a space to form a string </span>
</span>
In the action string, string is char* (character pointer), which is elegant and flexible, and the disadvantage is that in a large number of character operations, performance is not as good as char*
Some input and output of string:
<span style= "FONT-SIZE:14PX;" ><span style= "FONT-SIZE:12PX;" >//input by Word </span> for
(string s; Cin >>s;)
cout << s << "";</span>
Entire line of input
<span style= "FONT-SIZE:14PX;" >string s;
Getline (CIN, s);
cout << S <<endl;</span>
String stream
A string stream is explained by an example
With a file (a.txt) There are several lines, do not know how many integers per line, programming output per line of integers and how to implement.
The code is as follows:
<span style= "FONT-SIZE:14PX;" > #include <iostream>
#include <sstream>
#include <fstream>
using namespace std;
------------------
int Main () {
ifstream in ("A.txt");
for (string S; getline (in, s);) {
int a, sum = 0;
for (Istringstream sin (s); sin >> A; sum = a);
cout << sum << endl;
}
} </span>
Getline (in, s), a row of values in a file (one line of string) to S, each row loops once, until it is empty, Istringstream is a string input stream, a sin stream is created in the resource is a stream, the argument is a string object, It sees the string entity as an input stream, so sin >> A is to enter an integer from a string stream into a, lose and lose until the last integer is lost.
By comparing C-string with string, string seems to be more suitable for writing C + + programs, but that's just my personal opinion. Yes, it depends on your personal habits.
, the summary is here ... Yes, if there's something wrong, please tell me, thank you first.