string

来源:互联网 发布:无锡淘宝影视 张飞飞 编辑:程序博客网 时间:2024/06/06 08:44

string

  • string 都是以std::basic_string为基础
  • 其中有以下四种
    • std::string 是由std::basic_string定义
    • std::wstring 是由sted::basic_string定义
      其中它的模板为
template<    class CharT,    class Traits=std::char_traits<ChatT>,    class Allocator=std::allocator<CharT>>class basic_string;

其中 traits 中文为特质

成员类型

  • traits_type
  • value_type
  • allocator_type
  • size_type 一般是 std::size_t
  • difference_type 有符号 std::ptrdiff_t
  • reference c++11前 Allocator::reference c++11后 value_type&
  • const_reference c++11前 Allocator::const_reference c++11后 const value_type&
  • iterator 随机访问迭代器
  • const_iterator 常量随机访问迭代器
  • reverse_iterator std::reverse_iterator
  • const_reverse_iterator std::const_reverse_iterator

成员函数

  • 构造函数

      整个有9个构造函数:  1.basic_string() //空长度和为初始化字符串 常数  2.basic_string( size_type count,           CharT ch,           const Allocator& alloc = Allocator() );//以 count长度个ch 初始化 count应该小于npos 线性 count  3.basic_string( const basic_string& other,           size_type pos,           size_type count = std::basic_string::npos,          const Allocator& alloc = Allocator() );//主要是 通过另外一个字符串的子字符串复制到这里[pos,pos+count) 线性 count 4.basic_string( basic_string&& other );//move 主要是复制另外一个字符串 常数 5.basic_string( const basic_string& other );//主要是复制另外一个字符串 6.template< class InputIt >             basic_string( InputIt first, InputIt last,           const Allocator& alloc = Allocator() );//主要是通过迭代器复制另外一个字符串 7.basic_string( const CharT* s,          size_type count,           const Allocator& alloc = Allocator() );//主要是通过指针和长度来构建,可以有null字符. 8.basic_string( const CharT* s,          const Allocator& alloc = Allocator() );//主要是通过指针来构建,不能null字符.
  • operator=

  • assign
  • get_allocator

元素访问

  • at
  • operator[]
  • front
  • back
  • data
  • c_str

迭代器

  • begin cbegin
  • end cend
  • rbegin crbegin
  • rend crend

容量

  • empty
  • size/length
  • maxsize
  • reserve
  • capacity
  • ??

操作

  • clear
  • insert
  • push_back
  • pop_back
  • append
  • operator +=
  • compare
  • replace
  • substr
  • copy
  • resize
  • swap

查找

  • find
  • rfind
  • find_first_of
  • find_first_not_of
  • find_last_of
  • find_last_not_of

常量

  • npos ????

非成员函数

  • operator +
  • operator ==
  • operator !=
  • operator <
  • operator >
  • operator <=
  • operator >=
  • std::swap

输入输出

  • operator <<
  • operator >>
  • getline

转换函数

  • stoi
  • stol
  • stoll
  • stoul
  • stoull
  • stof
  • stod
  • stodl
  • to_string
  • to_wstring

literals 字面值

  • operator “”s
    • 如使用 string h=”abc\0\0cde”s;

帮助函数

主要是hash相关的

  • std::hash\
  • 其他字符形式
    • 主要是用来得到hash值
#include <iostream>#include <string>#include <functional>int main(){    std::string s="Stand back! I've got jimmies!";    hash<std::string> hash_fn;    size_t hash=hash_fn(s);    std::cout<<hash<<endl;    return 0;}
0 0
原创粉丝点击