C++ string详解

一、构造函数

  string();
  string( size_type length, char ch );
  string( const char *str );
  string( const char *str, size_type length );
  string( string &str, size_type index, size_type length );
  string( input_iterator start, input_iterator end );

字符串的构造函数创建一个新字符串,包括:

  • 以length为长度的ch的拷贝(即length个ch)
  • 以str为初值 (长度任意),
  • 以index为索引开始的子串,长度为length, 或者
  • 以从start到end的元素为初值. 

例如:string s;此时s=""

          string s(3,'9');此时s="999"

          string s("Eknight and Kiwinnnn");此时s="Eknight Love Kiwinnnn"

          string s("Eknight and Kiwinnnn",7);此时s="Eknight"

          string s("Eknight and Kiwinnnn",12,8);此时s="Kiwinnnn"

二、操作符

1.==和!=

判断两个字符串是否相等

例如  "Eknight and Kiwinnnn"=="Eknight and Kiwinnnn"

         "Eknight and Kiwinnnn"!="Eknight or Kiwinnnn"

2.>(>=)和<(<=)

比较字符串大小

"Eknight"<"Kiwinnnn"

"KiWinnnn">"Ekinght"

3.+(+=)

字符串的连接

string s1="Eknight"

string s2="and"

string s3="Kiwinnnn"

s1+" "+s2+" "+s3="Eknight and Kiwinnnn"

4.[]

获取特定字符

string s="Eknight and Kiwinnnn"

s[0]='E',s[12]='K'

三、迭代器

1.begin()

返回一个指向字符串第一个元素的迭代器

2.end()

返回一个指向字符串的最后一个元素的下一个位置的迭代器

四、长度

1.length()/size()

输出当前字符串长度

五、删除

erase()

  iterator erase( iterator pos );
  iterator erase( iterator start, iterator end );
  basic_string &erase( size_type index = 0, size_type num = npos );

erase()函数可以:

  • 删除pos指向的字符, 返回指向下一个字符的迭代器,
  • 删除从start到end的所有字符, 返回一个迭代器,指向被删除的最后一个字符的下一个位置
  • 删除从index索引开始的num个字符, 返回*this. 

string s="Eknight123 and Kiwinnnn"

s.erase(s.begin()+7);

此时s="Eknight23 and Kiwinnnn"

s.erase(s.begin()+7,s.begin()+8);

此时s="Eknight3 and Kiwinnnn"

s.erase(7,1);

此时s="Eknight and Kiwinnnn"

六、查找

1.find()

size_type find( const basic_string &str, size_type index );
  size_type find( const char *str, size_type index );
  size_type find( const char *str, size_type index, size_type length );
  size_type find( char ch, size_type index );

find()函数:

  • 返回str在字符串中第一次出现的位置(从index开始查找)如果没找到则返回string::npos,
  • 返回str在字符串中第一次出现的位置(从index开始查找,长度为length)。如果没找到就返回string::npos,
  • 返回字符ch在字符串中第一次出现的位置(从index开始查找)。如果没找到就返回string::npos

例如,

string str1( "Alpha Beta Gamma Delta" );
    unsigned int loc = str1.find( "Omega", 0 );
    if( loc != string::npos )
      cout << "Found Omega at " << loc << endl;
    else
      cout << "Didn't find Omega" << endl;

七、插入

insert()

iterator insert( iterator i, const char &ch );
  basic_string &insert( size_type index, const basic_string &str );
  basic_string &insert( size_type index, const char *str );
  basic_string &insert( size_type index1, const basic_string &str, size_type index2, size_type num );
  basic_string &insert( size_type index, const char *str, size_type num );
  basic_string &insert( size_type index, size_type num, char ch );
  void insert( iterator i, size_type num, const char &ch );
  void insert( iterator i, iterator start, iterator end );

insert()函数的功能非常多:

  • 在迭代器i表示的位置前面插入一个字符ch,
  • 在字符串的位置index插入字符串str,
  • 在字符串的位置index插入字符串str的子串(从index2开始,长num个字符),
  • 在字符串的位置index插入字符串str的num个字符,
  • 在字符串的位置index插入num个字符ch的拷贝,
  • 在迭代器i表示的位置前面插入num个字符ch的拷贝,
  • 在迭代器i表示的位置前面插入一段字符,从start开始,以end结束. 
给我家的小傻子整理的string常用操作,溜了溜了

猜你喜欢

转载自blog.csdn.net/eknight123/article/details/80313609