C/C++学习之basic_string

来源:互联网 发布:云计算开发语言 编辑:程序博客网 时间:2024/06/03 21:39
#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_typeTraitsvalue_typeTraits::char_typeallocator_typeAllocatorsize_typeAllocator::size_typedifference_typeAllocator::difference_typereferenceAllocator::referenceconst_referenceAllocator::const_referencepointerAllocator::pointerconst_pointerAllocator::const_pointeriterator随机访问迭代器const_iterator常量随机访问迭代器reverse_iteratorstd::reverse_iterator<iterator>const_reverse_iteratorstd::reverse_iterator<const_iterator>

成员函数

(构造函数)根据字符数组或其它字符串创建字符串操作符字符串的拼接,赋值,I/O 和比较assign把字符赋给字符串

 

元素访问
at访问指定字符,带有边界检查[[operator_atoperator[] ]]访问指定字符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一个特殊值,用于指示 "未找到" 或 "所有剩下的字符"


原文网址:http://zh.cppreference.com/w/cpp/string/basic_string

原创粉丝点击