stirng type
Summary:
Types of C ++ Standard Library: string
variable-length string of
simple operation
only comprises personal common function
head File
As with other types of string library types, corresponding to the need to include header files
#include<string>
using namespace std;
define and initialize the string type
Definition and initialization | Explanation |
---|---|
string s1 = "C++"; | Create a string s1, if omitted = "C ++" null string |
stirng s2(s1); | Create and initialize a string value s1 s2 value ( C ++ ) |
string s3("Love") | String s3 created and initialized to Love |
string s4(6,'I') | S4 and create a string initialized to 6 consecutive characters 'I', a string consisting of |
string type of function
Assignment of strings
string s1 = "I LOVE C++"; string s2; s2 = s1; cout<<s2;
Input and output:
I LOVE C++
String + operator + =
string s1 = "I "; string s2 = "LOVE "; string s3 = "C++"; s1 = s1 + s2; cout<<s1<<endl; s1 += s3; cout<<s1<<endl;
Input and output:
I LOVE
I LOVE C++String relational operators
string type can be used directly ==,! =,>, <,> =, <=, etc. relational operators to compare strings, and returns a Boolean type
//EG: string s1 = "123"; string s2 = "123"; cout<<(s1 == s2 ? "s1 = s2" : "s1 != s2");
Input and output:
s1 = s2
Read a string
cin way
Whitespace characters automatically ignored when reading the beginning of
the character when reading the event of a blank character, the end of the readingstring s1; cin>>s1; cout<<s1;
Input and output:
Hello World
Hellogetline way
Included in the string library
istream& getline (istream& is, string& str);
string str; getline(cin,str); cout<<str;
Input and output:
Hello World
abc
Hello WorldEach entry is a single line encounters '\ n' input end
istream& getline (istream& is, string& str, char delim);
string str; getline(cin,str,'#'); cout<<str;
Input and output:
abc def#abc
abc defWhen a '#' character to the end of the operation, '#' and '#' character is not read after
String length
size () / lenth () can return the string length (byte length)
string s1; cout<<s1.size()<<endl; cout<<s1.lenth()<<endl; s1 = "Hello World"; cout<<s1.size()<<endl; cout<<s1.lenth()<<endl; s1 = "你好"; cout<<s1.size()<<endl; cout<<s1.lenth()<<endl;
Input and output:
0
0
11
11
4
4Get the character string
str [n]: Returns the n-th str characters from 0 to size () - 1
string str = "I Love C++" cout<<str[0]<<endl; a[7] = 'A'; cout<<str;
Input and output:
I
I Love A++Sentenced empty string
empty () returns a Boolean type
string s1; if(s1.empty()) cout<<"s1字符串为空";
Input and output:
s1 string is empty
String search
string in the find () return value is the first character or string that appears in the index, if not found, it will return npos.
string s1 = "C++"; string s2 = "I LOVE C++"; cout<<s1.find(s2)<<endl; cout<<s1.find("Hello")<<endl;
Input and output:
. 7
4,294,967,295 (maximum value or minimum value)Ordering within the string
string str = "cba"; sort(str.begin(), str.end()); cout<<str;
Input and output:
abc