Home > Back-end >  The use of the c string class (Internet, sharing)
The use of the c string class (Internet, sharing)

Time:11-12

Dropping char * string and selects the string class in c + + standard library, because he was compared to the former, don't have to worry about memory enough, the length of the string, etc., and appear as a class, he integrated operation function is enough to complete our most cases (or even 100%) of need, we can use the assignment=for operation, the==comparison, + do series (simple?) , we can put it as a basic data type, c + +

First of all, in order to use a string type in our program, we must include the header file & lt; String> , as follows:
#include //note that this is not a string. H string. H is C string header file

1. Declare a c + + string
Declare a string variable is simple:
String Str.
So we will declare a string variable, but now that is a class, the constructor and destructor, the above stated that there was no incoming parameters, so it directly using the default constructor of the string, the function is made by the Str initialized to an empty string, the string class constructor and destructor is as follows:
A) the string s;//to generate an empty string s
B) the string s (STR)//copy constructor generated replica of STR
C) string s (STR, stridx)//the string STR "stridx began in position" in part as a string of initial value
D) string STR, stridx, strlen (s)//the string STR within "begins with stridx and length at most strlen" part as a string of initial value
E) string (CSTR)/s/C string as the initial value of s
(f) the string s chars, chars_len)//will be chars_len before C string characters as initial value of the string s,
G) string s (num, c)//generates a string that contains a num c characters
H) string s (beg, end)//beg to interval; End (do not include the end) to the initial value as a string of characters in s
I) s. ~ string ()//destroy all characters, free memory
Is very simple, I will not explain,

2. String manipulation functions
This is the key of c + + string. I put a variety of operating function list, people don't like watching all function can find their favorite function here, to see him behind the detailed explanation,
A)=to assign ()//assigned to a new value
B) swap ()//exchange
the content of the two stringsC) +=, append (), the push_back ()//in the tail added character
D) insert ()//insert characters
E) erase ()//delete characters
F) the clear ()//delete all characters
G) the replace ()//replace characters
H) +//series string
I)==,!=, & lt; ,<=, & gt; ,> Compare strings=, compare ()//
J) the size (), length ()//return characters
K) max_size ()//return the possible maximum number of characters
L) the empty//to determine whether a string is empty ()
M) capacity ()//before returning to redistribute the character size
N) reserve ()//keep a certain amount of memory to hold a certain number of characters
O) [], the at ()//access to a single character
P) & gt;> And getline ()//a value read from the stream
Q) & lt; R) copy ()//a value assignment for a C_string
S) c_str ()//will be content to C_string return
T) data ()//returns will be content to a character array
U) substr ()//return a string
V) lookup function
W) begin () end ()//offer similar STL iterator support
X) rbegin () rend ()//reverse iterator
Y) get_allocator ()//return configurator
Detailed below:

2. 1 C + + string and conversion of the C string
C + + provided by C + + string to get the corresponding C_string method is to use the data (), c_str () and copy (), among them, the data () returns a string in the form of a character array, but it doesn't add '\ 0', c_str () returns a character '\ 0' at the end of the array, and the copy (), copy the contents of the string or into the existing C_string or character array, C + + string does not end with '\ 0', my suggestion is that can use C + + string is used in the program, you have not choose C_string, because simply introduces, in detail, who wants to learn more about the matters needing attention in the use can give me a message (to my inbox), I explain in detail,

2. 2 size and capacity function
A character string of c + + there are three kinds of size: a) the number of characters, existing function is the size () and length (), they are equivalent to the Empty () is used to check whether a string is Empty, and b) max_size () the size refers to the current c + + string can contain the number of characters, most likely and the limitation of the machine itself or the position of the string for the size of the memory, we usually don't have to care about him, size should be enough that we use, but not enough use, throws length_error abnormal c) capacity () to allocate memory before the string can contain the maximum number of characters, another to note here is that the reserve () function, the function to a string to allocate memory, is determined by its parameters, the size of the redistribution is 0, the default parameters at this time will be optional cut the string,

There will be necessary to repeat the C + + string and C string conversion problem, many people will encounter such a problem, do your own program to call others function, class or something (such as database connection function Connect (char *, char *)), but the number of function arguments with others is a char * form, and we know that the c_str (), data () returns an array of characters from the string, so it is a kind of const char *, if you want to, as a function of the above mentioned parameters must also be copied to a char *, and our principle is to do not use C string is not used, so this time our approach is: if the function of parameters (i.e., char *) content don't change, we can Connect ((char *) UserID. C_str (), (char *) PassWD. C_str ()), but this time can be dangerous, because that the converted string is actually can modify (with interest can give it a try yourself), so I emphasize except when the function call wrong parameter modification, otherwise must be copied to a char *, of course, more safe way is to copy no matter what happens to a char *, at the same time we also praying to prayer is still the best programming using C string (say they master is a little too much, we may also wear in open-backed pants when they start programming, ha ha...). Functions are written specification, so we don't have to cast,
2. 3 element access

We can use the subscript operator [] and functions the at () on a visit to the element contains characters, but it should be noted that operators [] does not check whether the index is effective (effective index 0 ~ STR. The length ()), if the index fails, will cause undefined behavior, and the at () will check, if you use the at () when the index is invalid, throws out_of_range abnormalities,
There is an exception to have to say, const string a; Operators [] the index value is a. ength () is still valid, the return value is' \ 0 'and other situations, a. ength () index are invalid, for example:
Const string Cstr (" const string ");
String Str (" string ");

Str [3].//ok
Str. At (3);//ok

Str [100].//undefined behavior
Str. At (100);//throw out_of_range

Str [Str. Length ()]//undefined behavior
Cstr [Cstr. Length ()]//return '\ 0'
Str. Ats (Str. The length ());//throw out_of_range
Cstr. The at (Cstr) length ())////throw out_of_range

I don't agree with similar to the reference or pointer assignment below:
Char& R=s [2];
Char * p=& amp; S [3].
Because in the event of redistribution, r, p immediately fails, the way to avoid is not used,

2. 4 the comparison function
The comparison of C + + string support common operator (& gt; ,>=, & lt; ,<===,!=), and even support the string and the comparison of C - string (such as str<" Hello "), in the use of & gt; ,>=, & lt; ,<=these operators are based on "the current character features" will be one more character in dictionary order, dictionary sort of small characters, compare sequence is backward, not equal characters according to the position of two characters of the comparison results to determine the size of the two strings, at the same time, the string (" aaaa ") & lt; String (aaaaa),
Another powerful comparison function is a member function of the compare (), he supports multiple parameter processing, support with the index value and the length of the substring to compare, he returns an integer to represent the comparison results, the return value significance as follows: equal 0 -> 0 - greater than & lt; 0 - less than, for example:
String s (" abcd ");

S.com pare said (" abcd ");//return 0
S.com pare said (" dcba ");//return a value less than 0
S.com pare said (" ab ");//returns the value greater than 0

S.com pare said (s);//equal
S.com pare said (0, 2, s, 2, 2);//"ab" and "CD" compare less than zero
S.com pare said (1, 2, "BCX", 2);//compared with "BC" and "BC",
How's that? The function is complete! What? Can't satisfy your appetite? Well, wait, there's more behind personalized comparison algorithm, and give a hint, using the comparison of the STL algorithms, what? Don't know anything about the STL? Depend, you repair!

2. May change the content
Account for a large part of this in the operation of the string,

First assignment, the first assignment method is to use the operator=of course, the new value can be either a string (such as: s=ns), c_string (such as: s="gaint") or even a single characters (such as: s='j'), you can also use a member function of the assign (), the member function can make you more flexible and assign a string, or an example:
S.a ssign (STR);//don't say
S.a ssign (STR, 1, 3);//if the STR is "iamangel" is the "ama" is assigned to the string
S.a ssign (STR, 2, string: : npos);//the string STR from index value assignment of 2 start to end to s
S.a ssign (" gaint ");//don't say
S.a ssign (" nico ", 5);//the 'n' 'I', 'c' 'o' '\ 0' is assigned to the string
S.a ssign (5, 'x');//assign five x to string
There are three ways of to empty string: s=""; S.c Lear (); S.e rase (); (more and more, I think, for example, easier than talk to let others understand!) nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related