原文地址:http://www.cplusplus.com/reference/string/string/
string
字符串是表示字符序列的对象。
标准字符串类使用类似于标准容器字节的接口来提供对这些对象的支持,但是添加专门用于以单字节字符串操作的功能。
使用string类之前必须加上<string>头文件:#include<string>;
构造函数
default (1) | string(); |
---|---|
copy (2) | string (const string& str); |
substring (3) | string (const string& str, size_t pos, size_t len = npos); |
from c-string (4) | string (const char* s); |
from buffer (5) | string (const char* s, size_t n); |
fill (6) | string (size_t n, char c); |
range (7) | template <class InputIterator> string (InputIterator first, InputIterator last); |
initializer list (8) | string (initializer_list<char> il); |
move (9) | string (string&& str) noexcept; |
成员函数
Member functions
-
(constructor)
- Construct string object (public member function )构造函数
-
(destructor)
- String destructor (public member function )析构函数
-
operator=
- String assignment (public member function )重载=
Iterators :
-
begin
- Return iterator to beginning (public member function )获得begin迭代器
-
end
- Return iterator to end (public member function )获得end迭代器
-
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 )获得const begin迭代器
-
cend
- Return const_iterator to end (public member function )获得const end迭代器
-
crbegin
- Return const_reverse_iterator to reverse beginning (public member function )
-
crend
- Return const_reverse_iterator to reverse end (public member function )
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 )清空String类
-
empty
- Test if string is empty (public member function )判断是否为空
-
shrink_to_fit
- Shrink to fit (public member function )
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 )返回第一个字符的左值
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 )交换string的值
-
pop_back
- Delete last character (public member function )删除最后一个字符
String operations :
-
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 )比较
数值和String的相互转换(非string成员函数)
-
to_string(val)
- 返回数值val的string表示
-
stoi(s,p,b)
- 返回s的字串的int数值,b表示转换基数,默认为10,p用来保存s中第一个非数值字符的下标,默认为0
-
stol(s,p,b)
-
返回s的字串的long数值
-
stoul(s,p,b)
-
返回s的字串的unsigned long数值
-
stoll(s,p,b)
-
stof(s,p)
- 返回s的字串的float数值
-
stod(s,p)
-
返回s的字串的double数值
-
stold(s,p)
- 返回s的字串的long double数值