stl 字符串类

来源:互联网 发布:ios数据持久化方式 编辑:程序博客网 时间:2024/06/03 12:54
1. 两个基本字符串类


对于ANSI类型字符串是使用:
  
typedef basic_string<char, char_traits<char>, allocator<char> >
string;


2.而对于使用UNICODE则是使用


typedef basic_string<wchar_t, char_traits<wchar_t>,
allocator<wchar_t> > wstring;


由此可见字符串的基类是basic_string,对于每中类型的字符串都有其特别的特性,比如求长度,比较,字符串的拷贝,这些具体的字符串有具体的操作方法,因此为了统一这些方法,就使用字符串特性类来提供统一的操作方法,这个特性类就是char_traits.这个类提供了字符复制,比较,转化,移动等通用的方法. 在这里需要注意的是因为字符形也可以用整型描述,因此有一个特定的类型定义: int_type 表示是对应的整形,这个类型在char_traits中定义.




allocator_type   分配器类型
const_iterator   常量迭代器
const_pointer    常量字符串指针
const_reference  常量字符引用
const_reverse_iterator 
difference_type   两迭代器之差的类型
iterator  
npos              静态数据成员,为-1通常用在查找没有找到时的返回
pointer           字符串指针
reference         字符引用
reverse_iterator  
size_type         位置,长度的类型
traits_type       特性类的类型
value_type        字符类型




2.basic_string类中的一些成员函数


const value_type *c_str( ) const   //转化为一个常量字符串指针.
const value_type *data( ) const;   //可以直接通过这个函数来操作数据块(只读)


size_type capacity( ) const;      //返回当前分配的缓冲的大小




字符串的查找


size_type find(
   const value_type* _Ptr, 
   size_type _Off = 0,
   size_type _Count    //表示只查找_Ptr中的指定数量的字符个数
) const;




size_type find_first_not_of(
   value_type _Ch, 
   size_type _Off = 0     //这里是开始索引
) const;
size_type find_first_not_of(
   const value_type* _Ptr,   //查找不是_Ptr中任意字符的开始位置.
   size_type _Off = 0
) const;
size_type find_first_not_of(
   const value_type* _Ptr, 
   size_type _Off,
   size_type _Count   //这个参数同find
) const;
size_type find_first_not_of(
   const basic_string& _Str,
   size_type _Off = 0
) const;




find_first_of  //跟前面函数作用相反




//查找不满足字符_Ptr中任意一个字符的最后一个字符的位置


size_type find_last_not_of(
   value_type _Ch, 
   size_type _Off = npos   //这里不是对象的开始索引,而是只指定只查找到这个位置.
) const;
size_type find_last_not_of(
   const value_type* _Ptr,
   size_type _Off = npos
) const;
size_type find_last_not_of(
   const value_type* _Ptr, 
   size_type _Off,
   size_type _Count
) const;
size_type find_last_not_of(
   const basic_string& _Str,
   size_type _Off = npos
) const;






find_last_of   //查找满足条件的最后一个字符的位置,注意这里是最后一个,也就是说"abcdef for"  "fo" 返回的是最后o的位置.




从后边找
size_type rfind(
   value_type _Ch, 
   size_type _Off = npos
) const;
size_type rfind(
   const value_type* _Ptr,
   size_type _Off = npos
) const;
size_type rfind(
   const value_type* _Ptr, 
   size_type _Off = npos,
   size_type _Count
) const;
size_type rfind(
   const basic_string& _Str,
   size_type _Off = npos
) const;






max_size: 字符串能够的最大容量




//取子串
substr 


swap: 交换内容




//从新设置字符串SIZE


void resize(
   size_type _Count,    //若_Count大于原来则用_Ch填充,否则就截掉尾部
   value_type _Ch = value_type( )    
);




void reserve(
   size_type _Count = 0
);   //设置最优化的缓冲容量,设置的结果并不一定是参数指定的值






另外<string>中还提供了 +, +=, -, -= 等运算符





0 0
原创粉丝点击