C++ String

成员函数(Member functions)

(constructor) 构建字符串对象 (构造函数成员)
operator= 字符串赋值 (公有成员函数)
以下全为公有成员函数


迭代器(Iterator)

begin 返回指向字符串开始处的迭代器
end 返回指向字符串结束处之后的迭代器
rbegin 返回指向反向开始处的反向迭代器,即:指向结束处的迭代器
rend 返回指向反向结束处的反向迭代器,即:指向开始处之前的迭代器


容量(Capacity)

size 返回字符串长度
length 返回字符串长度
max_size 返回字符串的最大长度
resize 改变字符串大小,即:多去少补
capacity 返回分配存储空间大小
reserve 请求改变存储空间大小
clear 清空字符串
empty 测试字符串是否为空


访问元素(Eelment access)

operator[] 得到字符串中的字符
at 得到字符串中的字符


修改(Modifiers)

operator+= 追加
append 追加
push_back 追加字符到字符串
assign 赋值
insert 插入
erase 从字符串中擦除一些字符
replace 替换部分或全部字符
swap 与另一字符串交换内容


字符串操作(String operation)

c_str 得到等效的字符数组
data 得到等效的字符串数据
get_allocator 得到分配器
copy 从字符串中复制字符序列
find 查找字符
rfind 从后向前查找字符
find_first_of 查找某个字符第一次出现的位置
find_last_of 查找某个字符最后一次出现的位置
find_first_not_of Find absence of character in string 注:英文原意比较准确
find_last_not_of Find absence of character in string from the end
substr 生成子字符串
compare 比较


string s="abcdefghijklmn";

for(string::const_iterator i = str.begin();i!=str.end();i++) 

{

  cout << *i << endl;

}

cout << s[3]<< endl;

cout << s.at(3)<< endl;

原文地址:https://www.cnblogs.com/rooney/p/2596937.html