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种方法,从网上提供的性能测试来看,性能是从前往后越来越好的,但是从使用方便的角度来讲,是从前往后,越来越差,根据具体情况,进行选择。

拆分

substr

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

pos
第一个字符的位置被复制为子串。
如果这是等于字符串的长度,该函数返回一个空字符串。
如果这是大于字符串的长度,它会抛出out_of_range。
注意:第一个字符表示为值0(不是1)。
len
字符数在子包括(如果字符串是短,尽可能多的字符可以在需要使用)。
字符串::非营利值表示的所有字符,直到字符串的结尾。

  // 取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;

转换

数字转 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;

字符数组转string

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

string bar = foo;
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 【转载】原文地址:std::string详解作者:kieven2008 之所以抛弃char*的字符串而选用C++标...
    VAYY阅读 656评论 0 2
  • strings.h #ifndef _STRINGS_H#define _STRINGS_H#include #...
    老练子丶2017阅读 402评论 0 0
  • 开淘宝的人一直以一个直线上涨模式 。开网店的人越来越多 因为都知道挣钱 但是真正挣到钱的不怎么多 。网店市场生存空...
    枉自i阅读 549评论 0 1
  • 想要做一个成功的团队管理者,第一位的是人品,这是能做成事的原则,做人,要仁爱,诚信,有一颗善心,多...
    时强阅读 1,601评论 0 3
  • 我们步入了一个新的时代。 在中国,逐渐流行起来的信息传送成为了人们热议的话题,始终有许多的人在呼唤人们回归原始,不...
    sinken阅读 679评论 0 2