C + + Primer reading notes standard library string type learning __c++

Source: Internet
Author: User
standard library String type learning initialization of String type string S1
Default constructor, S1 is an empty string string s2 (S1)
Initialize S2 to a copy of S1 string S3 ("value")
Initializes S3 to a copy of a string literal string S4 (n, c)
Initialize S4 to n copies of the character ' C ' string s (CP, N)
Creates a string object that is initialized to a copy of the first n elements of the array that the CP points to string s (S2, Pos2)
Creates a string object that is initialized to a copy of the character in the existing string object S2 that starts with the subscript Pos2 string s (S2, Pos2, Len2)
Creates a string object that is initialized to an existing string object s2 a copy of the len2 character that starts with the small sign Pos2 Reading and writing of a string object (CIN, Getline)We can read and write strings directly using the standard input and output operators, that is, you can read and write strings directly with CIN >> S and cout << s ( read consecutive strings, no spaces, carriage returns, and other white space characters
Use CIN >> s to read the string and write to S, which, at the time of writing, reads and ignores the first white-space characters at the beginning, and then read a character until it encounters a blank break again, that is, if we read a "Hello World" string, we can only read "Hello", because O is followed by a space, unable to continue reading Getline (CIN, s), reading a line of text and storing it in S
The Getline function is read from the next line of the input stream, and the contents of the read are saved to S. This line does not include line breaks。 Getline does not ignore line breaks at the beginning of a row. Stops reading and returns as long as it encounters a newline character. If the beginning is a newline character, an empty string will be returned operation of a String objectS.empty ()
—————————————————————————————————— –s.size ()
Returns the number of characters in S (where the characters include those whitespace characters), The returned type is not an int, but a string::size_type type, so do not assign the return value of size to a value of type int
—————————————————————————————————— –s[n]
The string can be accessed directly with the subscript, and the position is counted from 0, but the subscript operator needs to be a value of a size_type type.。 By subscript we can not only access the value of the corresponding position in the string, the corresponding position can also be assigned, that is, s[n] can be either the right-hand operand of the assignment symbol = or its left operand, that is, a = S[n] and s[n] = ' A ' are all established
—————————————————————————————————— –s1 + s2
—————————————————————————————————— –s.append (args)
String The args back in S, and return the reference S.replace (POS, Len, args)
Deletes the Len character from the subscript Pos in S, replaces it with the character specified by args, and returns the reference to S S.replace (b, E, args)
Delete all elements in s that are marked by iterator B to E, then replace with args, and return the reference to S
—————————————————————————————————— – The args parameter here can be as follows:
S2, String s2, Pos2, len2 string S2 starting with subscript Pos2 the LEN2 character CP Pointer CP points to the null-terminated array CP, the Len2 CP points to a null-terminated array of characters N, c-character C-N copies B2, E2 iterator B2 and E2 tag range for all character end
—————————————————————————————————— –s1 = s2
Replace the contents of the S1 with the contents of S2, where we can replace them directly.
—————————————————————————————————— –S1 = = S2
Compare the contents of S1 and S2, equality returns TRUE, otherwise returns false, where the comparison is content, not storage location
—————————————————————————————————— –!=, <=, >=
——————————————————————————————————— a method that modifies a string object in common with the container (see the Vector section in detail)
S.insert (P, t) S.insert (p, N, t) S.insert (p, B, E) s.assign (b, E) s.assign (n, t) s.erase (p) s.erase (b, e) string-specific modification method
S.insert (POS, n, c)
Insert n characters before the element of the subscript pos C S.insert (POS, S2)
Inserts the copy of the string object S2 before the element of the Subscript pos S.insert (pos, S2, Pos2, Len)
Inserts the Len character S.insert (POS, CP, Len) in S2 from the subscript pos2 before the element in the subscript pos.
Inserts the first Len character of an array of CP-induced S.insert (POS, CP) before the element of the subscript pos
Inserts a null-terminated string copy of the CP point before the element of the subscript pos s.assign (S2)
Replace S s.assign with a copy of S2 (S2,pos2, Len)
Replaces S s.assign (CP, Len) with a copy of Len characters starting with subscript pos2 in S2
Replaces S s.assign (CP) with the first Len character copy of the array that the CP points to
Replaces S s.erase with a null-terminated string copy pointed to by CP (POS, Len)
Delete Len character end starting from subscript POS
—————————————————————————————————— – Substring operation
S.SUBSTR (POS, N)
Returns a String containing the N-character s.substr (POS) from the subscript POS in S
Returns a string that contains all the elements in S from the subscript Pos to the end of S (POS, S.length ()), where s.length () refers to the next position of the last element, so if the pos = = S.length (), The return will be an empty string with a length of 0 and no error, but if pos > s.length (), an error is made to access the unknown area s.substr ()
Returns the copy end of S
—————————————————————————————————— – Find operations
S.find (args)
Find the first occurrence of args in S s.rfind (args)
Find the last occurrence of args in S s.find_first_of (args)
The first occurrence of any character looking for args in S s.find_lase_of (args)
The last occurrence of any character looking for args in S s.find_first_not_of (args)
Find the first character in args that does not belong to args s.find_last_not_of (args)
Find the last character end of args that is not part of args in S
—————————————————————————————————— – The args parameter here can be as follows:
S2, POS in S, starts with the position of the subscript Pos to find the string Object s2. The POS value defaults to 0 C, POS is in S, starting from the position of the subscript POS tag, look for the character C CP, POS in S, start with the position of subscript pos tag, find the string cp, POS, N of the end of the null string pointed to by the pointer CP, and start with the position of the subscript pos tag in S. Find the first n character end of the array pointed to by the pointer CP
Comparison of —————————————————————————————————— –string
S.compare (S2)
Compare s and S2 s.compare (POS1, N1, S2)
Let s compare N1 elements starting with POS1 to S2 s.compare (POS1, N1, S2, Pos2, N2)
Compare N1 elements starting with POS1 in S to N2 elements from Pos2 starting with S2 s.compare (CP)
Compare the null-terminated string S.compare (POS1, N1, CP) pointed to by S and CP
Compare the N1 elements from POS1 in S and the null-terminated string S.compare (POS1, N1, CP, N2) that the CP points to.
Compare the N1 elements from pos1 beginning with the first N2 characters of the null-terminated strings pointed to by the CP for S1.compare (args), positive, S1 > args, negative, s1 < ARGS,0,S1 = args String::size_type TypeThe size () member function is supposed to return an int value, but actually the return is String::size_type type String::size_type type is a string type of matching type, not machine-independent, the type has the same meaning as the unsigned type, so the string it can store is twice times the length of the type intWe can approximate the string::size_type as an unsigned integral type string-type assignment OperationsThe efficiency of a string-type assignment is relatively low because it first frees the memory occupied by the S1 and then assigns the S1 enough memory space to S2 and then copies all S2 characters into that memory space two strings addedWe should note that because the string type and literal string "ABC" are two different types, we can do s1+s2 such operations, but not do "abc" + "BCD" (this is because the literal string is a const char* type, is a pointer, is unable to complete the string concatenation operation), so + operator The left and right operands have at least one required string type processing of string object characters (both defined in the Cctype header file)Isalnum (c) True if C is a letter or number Isalpha (c) True if C is a letter Iscntrl (c) True if C is a control character isdigit (c) If C is a number, true Isgraph (c) True if C is not a space but printable, Islower (c) True if C is a lowercase letter isprint (c) True ispunct if C is a printable character isspace (c) If C is a punctuation mark True Isupper if C is a white-space character (c) If C is a capital letter isxdigit (c) If C is a hexadecimal number, true ToLower (c) If C is a capital letter, returns its lowercase form, otherwise it returns directly to C ToUpper ( c if c is a lowercase letter, return its uppercase form, or return directly to C Note:A string like "abc" We Become string literal value, in C + +, the string literal is not the same type as the standard library string, and the string literal is the const char* type with the end of the, and the string type ends with No. S+ "ABC" returned is still a string type in a string, any integer value can be indexed, but the actual data type of the index is indeed unsigned type String::size_type for C + +, We can convert an array to a string array using the stream in the library, assuming we have an integer array of length n, we can use the code to convert to the corresponding string array
String *strnum = new String[n];
for (int i = 0; i < n; ++i) {
    stringstream stream;
    Stream << Num[i];
    Stream >> Strnum[i]
}
Related Article

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.