C++ string类

来源:互联网 发布:黑帽子数据 编辑:程序博客网 时间:2024/04/30 08:18

class std::String

String class

typedef basic_string string

     std::string是basic_string模板类的使用char作为类型的类实例,这种类处理字节与其所使用的编码无关:如果处理的是多字节可变长度的字符(比如UTF-8),该类中所有的成员(比如长度和大小),以及迭代器依然会按照字节来操作。

typedef basic_string string

成员类型 定义 value_type(迭代器所指对象的型别) char traits_type(特化类型) char_traits allocator_type (类型化内存分配以及对象的分配和撤销) allocator reference 容器中某元素属性的引用类型 char& const reference const char & pointer char* const_pointer const char* iterator 一个随机存取的字符迭代器(可以转化为常迭代器) const_iterator 一个随机存取的常字符迭代器 reverse_iterator 反向迭代器 reverse_iterator const_reverse_iterator 常反向迭代器 reverse_iterator difference_type 描述序列(容器)中两个元素地址之间差异的 有符号整数类型 ptrdiff_t size_type描述序列长度的无符号整数类型 size_t

成员函数

  • 构造函数
  • 析构函数
  • operator=
迭代器
 对应iterator:     begin     end 对应reverse iterator     rbegin     rend 对应const iterator     cbegin     cend 对应const_reverse_iterator     crbegin     crend
capacity
  • size
  • length
  • max_size
  • resize
  • capacity
  • reverse
  • clear
  • empty
  • shirink_to_fit
元素读取
  • operator[]
  • at
  • back
  • front
字符串编辑器
  • operator+=
  • append
  • push_back append character
  • assign assign content to string
  • insert
  • erase
  • replace
  • swap
  • pop_back delete last characters
字符串操作
  • c_str
  • data
  • get_allocator
  • copy
  • find
  • rfind 找到字符最后一次出现的位置
  • find_first_of
  • find_last_of
  • find_first_not_of
  • find_last_not_of
  • substr
  • compare

成员常量

npos size_t所能取到的最大值
static const size_t npos =- 1

函数重载
  • operator+=
  • relation operators 例如== <= >= !=等等
  • swap 交换string对象的值
  • operator>>字符串提取符
  • operator<<字符串插入符
  • getline 从输入流中提取一行形成string对象

进一步探讨string类成员函数

data()和c_str

const char* data( ) const;const char* c_str() const;

string类中间可能带’\0’;

string::c_str带’\0’,而string::data不带’\0’;

不要试图通过c_str()和data()返回指针来改变string内容;

assign()替换当前string 内容

string& assign ( const string& str );string& assign ( const string&, size_t pos, size_t n );string& assign ( const char* s, size_t n );string& assign ( const char* s );string& assign ( size_t n, char c );template <class InputIterator>   string& assign ( InputIterator first, InputIterator last );

erase() erases a part of the string contents

 string& erase ( size_t pos = 0, size_t n = npos );iterator erase ( iterator position );iterator erase ( iterator first, iterator last );

push_back() 添加单独一个字符到string的内容中

// string::push_back#include <iostream>#include <fstream>#include <string>using namespace std;int main (){  string str;  ifstream file ("test.txt",ios::in);  while (!file.eof())  {    str.push_back(file.get());  }  cout << str;  return 0;}

capacity()容量/reverse()预留空间/resize()

size_t capacity ( ) const;//Returns the size of the allocated storage space in the string object.void reserve ( size_t res_arg=0 );//Request a change in capacity 重新分配容量给string对象void resize ( size_t n, char c );//c是填补用的字符void resize ( size_t n );//Resizes the string content to n characte.如果n>std::max_size 会抛出异常

copy()/replace()/insert()

size_t copy ( char* s, size_t n, size_t pos = 0) const;string& replace ( size_t pos1, size_t n1,   const string& str );string& replace ( iterator i1, iterator i2, const string& str );string& replace ( size_t pos1, size_t n1, const string& str, size_t pos2, size_t n2 );string& replace ( size_t pos1, size_t n1,   const char* s, size_t n2 );string& replace ( iterator i1, iterator i2, const char* s, size_t n2 );string& replace ( size_t pos1, size_t n1,   const char* s );string& replace ( iterator i1, iterator i2, const char* s );string& replace ( size_t pos1, size_t n1,   size_t n2, char c );string& replace ( iterator i1, iterator i2, size_t n2, char c );template<class InputIterator>   string& replace ( iterator i1, iterator i2, InputIterator j1, InputIterator j2 ); string& insert ( size_t pos1, const string& str ); string& insert ( size_t pos1, const string& str, size_t pos2, size_t n ); string& insert ( size_t pos1, const char* s, size_t n); string& insert ( size_t pos1, const char* s ); string& insert ( size_t pos1, size_t n, char c );iterator insert ( iterator p, char c );    void insert ( iterator p, size_t n, char c );template<class InputIterator>    void insert ( iterator p, InputIterator first, InputIterator last );

find()/rfind()/find_first_of()/find_last_of()/find_first_not_of()/find_last_not_of()

size_t find ( const string& str, size_t pos = 0 ) const;size_t find ( const char* s, size_t pos, size_t n ) const;size_t find ( const char* s, size_t pos = 0 ) const;size_t find ( char c, size_t pos = 0 ) const; //rfind()是找到字符串最后一次出现的位置// string::find#include <iostream>#include <string>using namespace std;int main (){  string str ("There are two needles in this haystack with needles.");  string str2 ("needle");  size_t found;  // different member versions of find in the same order as above:  found=str.find(str2);  if (found!=string::npos)    cout << "first 'needle' found at: " << int(found) << endl;  found=str.find("needles are small",found+1,6);  if (found!=string::npos)    cout << "second 'needle' found at: " << int(found) << endl;  found=str.find("haystack");  if (found!=string::npos)    cout << "'haystack' also found at: " << int(found) << endl;  found=str.find('.');  if (found!=string::npos)    cout << "Period found at: " << int(found) << endl;  // let's replace the first needle:  str.replace(str.find(str2),str2.length(),"preposition");  cout << str << endl;  return 0;}----------size_t find_last_of ( const string& str, size_t pos = npos ) const;size_t find_last_of ( const char* s, size_t pos, size_t n ) const;size_t find_last_of ( const char* s, size_t pos = npos ) const;size_t find_last_of ( char c, size_t pos = npos ) const;/* *  从string对象内容的结尾开始找出匹配属于c,str或者string的任意其中 *一部分最后一次出现的位置 */ // string::find_last_of#include <iostream>#include <string>using namespace std;void SplitFilename (const string& str){  size_t found;  cout << "Splitting: " << str << endl;  found=str.find_last_of("/\\");  cout << " folder: " << str.substr(0,found) << endl;  cout << " file: " << str.substr(found+1) << endl;}int main (){  string str1 ("/usr/bin/man");  string str2 ("c:\\windows\\winhelp.exe");  SplitFilename (str1);  SplitFilename (str2);  return 0;}//find_first_of()和find_first_not_of()/find_last_not_of()与find_last_of()类似
0 0
原创粉丝点击