原文网址:http://zh.cppreference.com/w/cpp/string/basic_string
#include <string> template< typename CharT, typename Traits = std::char_traits< CharT >, typename Allocator = std::allocator< CharT > > class basic_string;
类模板 basic_string 提供了字符序列如何操作和存储的泛型。下面是针对常见字符类型的特化:
typedef basic_string<char> string; typedef basic_string<wchar_t> wstring; typedef basic_string<char16_t> u16string; //C++0x 特征 typedef basic_string<char32_t> u32string; //C++0x 特征
类型成员
类型成员 | 定义 |
---|---|
traits_type | Traits |
value_type | Traits::char_type |
allocator_type | Allocator |
size_type | Allocator::size_type |
difference_type | Allocator::difference_type |
reference | Allocator::reference |
const_reference | Allocator::const_reference |
pointer | Allocator::pointer |
const_pointer | Allocator::const_pointer |
iterator | 随机访问迭代器 |
const_iterator | 常量随机访问迭代器 |
reverse_iterator | std::reverse_iterator<iterator> |
const_reverse_iterator | std::reverse_iterator<const_iterator> |
成员函数
(构造函数) | 根据字符数组或其它字符串创建字符串 |
操作符 | 字符串的拼接,赋值,I/O 和比较 |
assign | 把字符赋给字符串 |
元素访问
at | 访问指定字符,带有边界检查 | |
[[operator_at | operator[] ]] | 访问指定字符 |
data | 返回一个指针,指向字符串第一个字符 | |
c_str | 返回此字符串对应的标准C字符数组,不可修改 |
迭代器
begin,cbegin | 返回迭代器,指向字符串开头 |
end,cend | 返回迭代器,指向字符串最后一个元素的下一位置 |
rbegin,crbegin | 返回一个反向迭代器,指向字符串结尾 |
rend,crend | 返回一个反向迭代器,指向字符串开头的前一位置 |
容量
capacity | 返回字符串可以容纳的元素个数 |
empty | 若字符串中没有字符,返回'真' |
max_size | 返回字符串可以容纳的最大元素个数 |
reserve | 设置字符串的最小容量 |
size | 返回字符串中项的个数 |
length | 返回字符串长度 |
resize | 改变字符串大小 |
操作
clear | 清空内容 |
erase | 从字符串中移除字符 |
append | 向字符串中追加字符或字符串 |
compare | 比较两个字符串 |
replace | 替换字符串中字符 |
pop_back | 移除字符串最后一个字符 |
push_back | 在字符串末尾追加一个字符 |
substr | 返回特定子串 |
copy | 把字符串中字符拷贝到数组中 |
insert | 向字符串中插入字符 |
swap | 将此字符串内容与另一字符串交换 |
查找
find | 在字符串中查找字符 |
find_first_not_of | 查找非某字符第一次出现 |
find_first_of | 查找字符第一次出现 |
find_last_not_of | 查找最后一个非某字符 |
find_last_of | 查找字符最后一次出现 |
rfind | 查找子串最后一次出现 |
Allocator
get_allocator| /todo |
常量
npos | 一个特殊值,用于指示 "未找到" 或 "所有剩下的字符" |