STL備忘錄——string

前言

string是非常常用的類,了解它的功能和特性有助于我們對字符串處理方面的提升。C++語言范式繁多,所以我們需要仔細記住各種重載。所以在此寫了備忘錄,方便日后查閱:

初始化

#include <iostream>
#include <string>

int main ()
{
  std::string s0 ("Initial string");

  // constructors used in the same order as described above:
  std::string s1;
  std::string s2 (s0);
  std::string s3 (s0, 8, 3);
  std::string s4 ("A character sequence");
  std::string s5 ("Another character sequence", 12);
  std::string s6a (10, 'x');
  std::string s6b (10, 42);      // 42 is the ASCII code for '*'
  std::string s7 (s0.begin(), s0.begin()+7);

  std::cout << "s1: " << s1 << "\ns2: " << s2 << "\ns3: " << s3;
  std::cout << "\ns4: " << s4 << "\ns5: " << s5 << "\ns6a: " << s6a;
  std::cout << "\ns6b: " << s6b << "\ns7: " << s7 << '\n';
  return 0;
}

http://www.cplusplus.com/reference/string/string/string/

長度

length()

// output : 5
string s = "abcde";
cout<<s.length()<<endl;
cout<<s.size()<<endl;

begin

  std::string str ("Test string");
  for ( std::string::iterator it=str.begin(); it!=str.end(); ++it)
    std::cout << *it;
  std::cout << '\n';

除了begin之外,string還提供了cbegin、rbegin和crbegin。
其中,cbegin相較于begin的不同點在于,它會返回const_iterator。
而rbegin,則是一個逆序的迭代器,它指向字符串的最后一個。
最后的crbegin則是cbegin與rbegin特性的組合。

end與begin類似,不再贅述。

拼接

+

// 字符串拼接 +
string s = "abc";
s = s+"def";

append

// 字符串拼接 append
string s = "abc";
s = s.append("defg");

stringstream

//字符串拼接 stringstream
#include <sstream>

  string s = "abc";
  stringstream ss ;
  ss<<s<<"def";
  cout<<ss.str()<<endl;

sprintf

  // sprintf拼接
  char* s = new char[100];
  char* r = s;
  char* s1 = "abc";
  char* s2 = "def2";
  sprintf(s,"%s",s1 );
  s+=strlen(s1);
  sprintf(s,"%s",s2 );
  cout<<r<<endl;

上面提供了4種方法,從網(wǎng)上提供的性能測試來看,性能是從前往后越來越好的,但是從使用方便的角度來講,是從前往后,越來越差,根據(jù)具體情況,進行選擇。

拆分

substr

string substr (size_t pos = 0, size_t len = npos) const;

pos
第一個字符的位置被復(fù)制為子串。
如果這是等于字符串的長度,該函數(shù)返回一個空字符串。
如果這是大于字符串的長度,它會拋出out_of_range。
注意:第一個字符表示為值0(不是1)。
len
字符數(shù)在子包括(如果字符串是短,盡可能多的字符可以在需要使用)。
字符串::非營利值表示的所有字符,直到字符串的結(jié)尾。

  // 取1-3位
  // output: bcd
  string s = "abc";
  s.append("def");
  s = s.substr(1,3);
  cout<<s<<endl;

resize


  std::string str ("I like to code in C");
  std::cout << str << '\n';

  unsigned sz = str.size();

  str.resize (sz+2,'+');
  std::cout << str << '\n';

  str.resize (14);
  std::cout << str << '\n';
/*output*/
/*
I like to code in C
I like to code in C++
I like to code
*/

查找

find

size_t find (const string& str, size_t pos = 0) const;

  string s = "abcdef";
  // 尋找"c" output:2
  int index1 = s.find("c");
  cout<<index1<<endl;
  // 從第 2 位開始,尋找"c" output:2
  int index2 = s.find("c" , 2);
  cout<<index2<<endl;
  // 從第 3 位開始,尋找"c" output :-1
  int index3 = s.find("c" , 3);
  cout<<index3<<endl;

轉(zhuǎn)換

數(shù)字轉(zhuǎn) string

string to_string (int val);
string to_string (long val);
string to_string (long long val);
string to_string (unsigned val);
string to_string (unsigned long val);
string to_string (unsigned long long val);
string to_string (float val);
string to_string (double val);
string to_string (long double val);
  // output : abcdef123
  string s = "abcdef";
  s+=to_string(123);
  cout<<s<<endl;

string to int

  // output : 9528
  stringstream ss;
  int num2;
  ss<<"9527";
  ss>>num2;
  cout<<num2+1<<endl;

字符數(shù)組轉(zhuǎn)string

// 字符串轉(zhuǎn)char*
std::string x = "hello world";
char *y = x.c_str();
const char* foo = "Whatever";

string bar = foo;
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 【轉(zhuǎn)載】原文地址:std::string詳解作者:kieven2008 之所以拋棄char*的字符串而選用C++標...
    VAYY閱讀 708評論 0 2
  • strings.h #ifndef _STRINGS_H#define _STRINGS_H#include #...
    老練子丶2017閱讀 446評論 0 0
  • 開淘寶的人一直以一個直線上漲模式 。開網(wǎng)店的人越來越多 因為都知道掙錢 但是真正掙到錢的不怎么多 。網(wǎng)店市場生存空...
    枉自i閱讀 595評論 0 1
  • 想要做一個成功的團隊管理者,第一位的是人品,這是能做成事的原則,做人,要仁愛,誠信,有一顆善心,多...
    時強閱讀 1,755評論 0 3
  • 我們步入了一個新的時代。 在中國,逐漸流行起來的信息傳送成為了人們熱議的話題,始終有許多的人在呼喚人們回歸原始,不...
    sinken閱讀 690評論 0 2

友情鏈接更多精彩內(nèi)容