Common Use of STD string

Source: Internet
Author: User
Use string to replace the char * array, use the sort Sorting Algorithm to sort, and use the unique function to return and return duplicates.
1. Define
String S1 = "hello ";
String S2 = "world ";
String S3 = S1 + "," + S2 + "! \ N ";
2. append
S1 + = ", Shanshan \ n ";
3. Compare
If (S1 = S2)
.....
Else if (S1 = "hello ")
.....
4. String overload many operators, including +, + =, <, =, , [], <,>, And so on. These operators are convenient for string operations.

#include <string>
#include <iostream>
using namespace std;
int main(){
string strinfo="Please input your name:";
cout << strinfo ;
cin >> strinfo;
if( strinfo == "winter" )
cout << "you are winter!"<<endl;
else if( strinfo != "wende" )
cout << "you are not wende!"<<endl;
else if( strinfo < "winter")
cout << "your name should be ahead of winter"<<endl;
else
cout << "your name should be after of winter"<<endl;
strinfo += " , Welcome to China!";
cout << strinfo<<endl;
cout <<"Your name is :"<<endl;
string strtmp = "How are you? " + strinfo;
for(int i = 0 ; i < strtmp.size(); i ++)
cout<<strtmp[i];
return 0;
}

5. Find function
As search is one of the most frequently used functions, string provides a wide range of search functions. The list is as follows:

Function Name Description
Find Search
Rfind Reverse Lookup
Find_first_of Returns the first position if any character in the substring is found.
Find_first_not_of Returns the first position if any character in the substring is not included.
Find_last_of Searches for any character in the substring and returns the last position
Find_last_not_of Returns the last position if any character in the substring is not included.

The above functions are reloaded four times. The following uses the find_first_of function as an example to describe their parameters. Other functions are the same as their parameters, that is, there are 24 Functions in total:

Size_type find_first_of (const basic_string & S, size_type Pos = 0)
Size_type find_first_of (const chart * s, size_type POs, size_type N)
Size_type find_first_of (const chart * s, size_type Pos = 0)
Size_type find_first_of (Chart C, size_type Pos = 0)

All the lookup functions return a size_type type. The return value is generally the position of the string to be found. If not, the return value is string: NPOs.
In fact, string: NPOs indicates-1. -1 is returned if it is not found. Example:
# Include <string>
# Include <iostream>
Using namespace STD;
Int main (){
String strinfo = "// * --- Hello word !...... ------";
String strset = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz ";
Int first = strinfo. find_first_of (strset );
If (first = string: NPOs ){
Cout <"not find any characters" <Endl;
Return-1;
}
Int last = strinfo. find_last_of (strset );
If (last = string: NPOs ){
Cout <"not find any characters" <Endl;
Return-1;
}
Cout <strinfo. substr (first, last-first + 1) <Endl; // string. substr is a substring.
Return 0;
}
6. insert, replace, and erase Functions

String only provides the replace function based on the position and interval, rather than replacing another string in the specified string with a string.
Example:
# Include <string>
# Include <iostream>
Using namespace STD;
Int main (){
String strinfo = "this is winter, winter is a programmer. Do you know Winter? ";
Cout <"Orign string is: \ n" <strinfo <Endl;
String_replace (strinfo, "Winter", "Wende ");
Cout <"after replace winter with Wende, the string is: \ n" <strinfo <Endl;
Return 0;
}

String. Erase (Pos, srclen); // srclen indicates the deleted length.
String. insert (Pos, strdst); // POS is the position, and strdst is the inserted function.
Void string_replace (string & strbig, const string & strsrc, const string & strdst ){
String: size_type Pos = 0;
String: size_type srclen = strsrc. Size ();
String: size_type dstlen = strdst. Size ();
While (Pos = strbig. Find (strsrc, POS ))! = String: NPOs ){
Strbig. Erase (Pos, srclen );
Strbig. insert (Pos, strdst );
Pos + = dstlen;
}
}

Link: http://www.stlchina.org/twiki/bin/view.pl/Main/STLDetailString
 
7. Cut the string
# Include <sstream>
# Include <string>
# Include <iostream>
Using namespace STD;
Int main ()
{
String text = "Big | dog | China | sonic | free ";
Stringstream SS (text );
String sub_str;
While (Getline (SS, sub_str, '|') // use | to separate the content of test.
Cout <sub_str <Endl;

Return 0;
}
The output is as follows:
Big
Dog
China
Sonic
Free

8. constructor and destructor
String s generates an empty string s
String S (STR) copy constructor to generate a copy of the string Str
String S (STR, stridx) refers to the part of the string STR that begins with stridx and is treated as the initial value of string S.
String S (STR, stridx, strlen) refers to the part of STR that begins with stridx and has a length of strlen, and is treated as the initial value of string S.

String S (CSTR) uses C-string CSTR as the initial value of S
String S (Num, c) generates a string containing num (c)
String S (beg, end) uses the characters in the interval [beg, end] as the initial values of S.
S .~ String () destroys all characters and releases the memory.
Note:
STD: String S ('x'); // Error
STD: String S (1, 'x'); // OK, create a string that has one charactor 'X'

9. substr (string. substr method)

(1) Parameters Start: Number -- an integer that indicates the position of the first character used to create a substring in my_str. If start is a negative number, the start position is determined from the end of the string, where-1 indicates the last character. Length: Number -- number of characters in the substring to be created. If length is not specified, the substring contains all characters starting from the string and ending with the string. (2) return string -- the substring of the specified string. (3) In the following example, create a new string my_str and use substr () to return the second word in the string. First, use the positive start parameter and then use the negative start parameter: vaR my_str: String = new string ("Hello World ");
VaR mysubstring: String = new string ();
Mysubstring = my_str.substr (6, 5 );
Trace (mysubstring); // output: World

Mysubstring = my_str.substr (-5, 5 );
Trace (mysubstring); // output: World

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.