头文件 #include<string>
支持长度可变的字符串。
1、string对象的定义和初始化
string s1; //默认构造函数,s1为空串
string s2(s1); //将s2初始化为s1的一个副本
string s3("value"); //将s3初始化为一个字符串字面值副本
string s4(n, 'c'); //将s4初始化为字符'c'的n个副本
2、string对象的读写
int main()
{
string s;
cin >> s;
cout << s << endl;
return 0;
}
⚠️:string类型的输入操作符:
读取并忽略开头所有的空白字符(如空格、换行符、制表符);
读取字符直至再次遇到空白字符,读取终止。
用get line读取整行文本:
int main() {
string s;
while( get line(cin, s) ) {
cout << s << endl;
}
}
换行符将不会存储在string对象中
3、string iterators
-
begin
- Return iterator to beginning (public member function )
-
end
- Return iterator to end (public member function )
-
rbegin
- Return reverse iterator to reverse beginning (public member function )
-
rend
- Return reverse iterator to reverse end (public member function )
-
cbegin
- Return const_iterator to beginning (public member function )
-
cend
- Return const_iterator to end (public member function )
-
crbegin
- Return const_reverse_iterator to reverse beginning (public member function )
-
crend
- Return const_reverse_iterator to reverse end (public member function )
4、string Capacity:
-
size
- Return length of string (public member function )
-
length
- Return length of string (public member function )
-
max_size
- Return maximum size of string (public member function )
-
resize
- Resize string (public member function )
-
capacity
- Return size of allocated storage (public member function )
-
reserve
- Request a change in capacity (public member function )
-
clear
- Clear string (public member function )
-
empty
- Test if string is empty (public member function )
-
shrink_to_fit
- Shrink to fit (public member function )
5、Element access:
-
operator[]
- Get character of string (public member function )
-
at
- Get character in string (public member function )
-
back
- Access last character (public member function )
-
front
- Access first character (public member function )
6、Modifiers:
-
operator+=
- Append to string (public member function )
-
append
- Append to string (public member function )
-
push_back
- Append character to string (public member function )
-
assign
- Assign content to string (public member function )
-
insert
- Insert into string (public member function )
-
erase
- Erase characters from string (public member function )
-
replace
- Replace portion of string (public member function )
-
swap
- Swap string values (public member function )
-
pop_back
- Delete last character (public member function )
-
c_str
- Get C string equivalent (public member function )
-
data
- Get string data (public member function )
-
get_allocator
- Get allocator (public member function )
-
copy
- Copy sequence of characters from string (public member function )
-
find
- Find content in string (public member function )
-
rfind
- Find last occurrence of content in string (public member function )
-
find_first_of
- Find character in string (public member function )
-
find_last_of
- Find character in string from the end (public member function )
-
find_first_not_of
- Find absence of character in string (public member function )
-
find_last_not_of
- Find non-matching character in string from the end (public member function )
-
substr
- Generate substring (public member function )
-
compare
- Compare strings (public member function )
8、Non-member function overloads
-
operator+
- Concatenate strings (function )
-
relational operators
- Relational operators for string (function )
-
swap
- Exchanges the values of two strings (function )
-
operator>>
- Extract string from stream (function )
-
operator<<
- Insert string into stream (function )
-
getline
- Get line from stream into string (function )
9、Member constants
-
npos
- Maximum value for size_t (public static member constant )
10、对单个字符的处理
isalnum(c); //如果c为字母或者数字,就返回true
isalpha(c); //如果c为字母,则返回true
isdigit(c); //数字
islower(c); //小写字母
isupper(c); //大写字母
isspace(c); //空白字符
tolower(c); //如果是大写字母,返回其小写形式,否则直接返回c
toupper(c); //如果是小写字母,返回其大写字母形式,否则直接返回c
isxdigit(c); //十六进制