std::string简介及其使用

注:std::string C++11标准。

 

string概述

typedef basic_string<char> string;

  字符串是表示字符序列的对象。
  标准string类使用类似于字节标准容器的接口提供对此类对象的支持,但是添加了专门用于操作单字节字符(single-byte characters)的字符串的特性。
  string类是basic_string类模板的实例化,该模板使用char作为其字符类型,并具有默认的char_traits和allocator类型。
  需要注意的是,这个类独立于所使用的编码来处理字节(即与编码无关):如果用于处理多字节或可变长度字符(如UTF-8)的序列,那么这个类的所有成员(如长度或大小)及其迭代器仍将以字节(而不是实际编码的字符)进行操作。

 

成员类型

member typedefinition
value_type char
traits_type char_traits<char>
allocator_type allocator<char>
reference char&
const_reference const char&
pointer char*
const_pointer const char*
iterator a random access iterator to char (convertible to const_iterator)
const_iterator a random access iterator to const char
reverse_iterator reverse_iterator<iterator>
const_reverse_iterator reverse_iterator<const_iterator>
difference_type ptrdiff_t
size_type size_t

 

成员函数

  • (constructor) 构造函数
default (1)
string();
copy (2)
string (const string& str);
substring (3)
string (const string& str, size_t pos, size_t len = npos);
from c-string (4)
string (const char* s);
from buffer (5)
string (const char* s, size_t n);
fill (6)
string (size_t n, char c);
range (7)
template <class InputIterator>
string (InputIterator first, InputIterator last);
initializer list (8)
string (initializer_list<char> il);
move (9)
string (string&& str) noexcept;

  构造函数示例:

// string constructor
#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;
}

Output:
s1: 
s2: Initial string
s3: str
s4: A character sequence
s5: Another char
s6a: xxxxxxxxxx
s6b: **********
s7: Initial
构造函数示例
  • (destructor) 析构函数
~string();
  • operator= 赋值
string (1)
string& operator= (const string& str);
c-string (2)
string& operator= (const char* s);
character (3)
string& operator= (char c);
initializer list (4)
string& operator= (initializer_list<char> il);
move (5)
string& operator= (string&& str) noexcept;
  • 迭代器相关
函数 函数原型 功能描述
begin iterator begin() noexcept; Return iterator to   beginning 
const_iterator   begin() const noexcept;
end iterator end() noexcept; Return iterator to   end 
const_iterator   end() const noexcept;
rbegin reverse_iterator rbegin()   noexcept; Return reverse   iterator to reverse beginning 
const_reverse_iterator   rbegin() const noexcept;
rend reverse_iterator rend()   noexcept; Return reverse   iterator to reverse end 
const_reverse_iterator   rend() const noexcept;
cbegin  const_iterator cbegin() const   noexcept; Return const_iterator to   beginning 
cend  const_iterator cend() const noexcept; Return const_iterator to   end 
crbegin  const_reverse_iterator crbegin() const noexcept; Return const_reverse_iterator to   reverse beginning 
crend  const_reverse_iterator crend()   const noexcept; Return const_reverse_iterator to   reverse end 

   迭代器示例:

// string::begin/end
#include <iostream>
#include <string>

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

  return 0;
}

Output:
Test string
迭代器示例
  • 容器大小或容量相关
函数 函数原型 功能描述
size size_t size() const noexcept; Return length of string
length size_t length() const noexcept; Return length of string
max_size size_t max_size() const   noexcept; Return maximum size of string
resize void resize (size_t n); Resize string 
void resize (size_t n, const value_type& val);
capacity size_t capacity() const noexcept; Return size of allocated storage
reserve void reserve (size_t n = 0); Request a change in   capacity 
clear void clear() noexcept; Clear string
empty bool empty() const noexcept; Test if string is empty
shrink_to_fit  void shrink_to_fit(); Shrink to fit 

  示例:

// resizing string
#include <iostream>
#include <string>

int main ()
{
  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';
  return 0;
}

Output:
I like to code in C
I like to code in C++
I like to code
----------------------------------------------------------
// comparing size, length, capacity and max_size
#include <iostream>
#include <string>

int main ()
{
  std::string str ("Test string");
  std::cout << "size: " << str.size() << "\n";
  std::cout << "length: " << str.length() << "\n";
  std::cout << "capacity: " << str.capacity() << "\n";
  std::cout << "max_size: " << str.max_size() << "\n";
  return 0;
}

A possible output for this program could be:
size: 11
length: 11
capacity: 15
max_size: 429496729
示例代码
  • 成员访问相关
函数 函数原型 功能描述
operator[] char& operator[] (size_t   pos); Get character of   string
const   char& operator[] (size_t pos) const;
at char& at (size_t pos); Get character of   string
const   char& at (size_t pos) const;
front reference front(); Access last character
const_reference   front() const;
back char& back(); Access first character
const   char& back() const;

  成员访问示例:

// string::operator[]
#include <iostream>
#include <string>

int main ()
{
  std::string str ("Test string");
  for (int i=0; i<str.length(); ++i)
  {
    std::cout << str[i];
  }
  return 0;
}
------------------------------------------------------
// string::at
#include <iostream>
#include <string>

int main ()
{
  std::string str ("Test string");
  for (unsigned i=0; i<str.length(); ++i)
  {
    std::cout << str.at(i);
  }
  return 0;
}
------------------------------------------------------
// string::front
#include <iostream>
#include <string>

int main ()
{
  std::string str ("test string");
  str.front() = 'T';
  std::cout << str << '\n';
  return 0;
}

Output:
Test string
------------------------------------------------------
// string::back
#include <iostream>
#include <string>

int main ()
{
  std::string str ("hello world.");
  str.back() = '!';
  std::cout << str << '\n';
  return 0;
}

Output:
hello world!
成员访问示例
  • 添加、删除等修改相关操作
函数 函数原型 功能描述
operator+= string& operator+= (const string& str); Append to string
string& operator+= (const char* s);
string& operator+= (char c);
string& operator+=   (initializer_list<char> il);
append string& append (const string& str); Append to string
string& append (const string& str, size_t subpos, size_t sublen);
string& append (const char* s);
string& append (const char* s, size_t n);
string& append (size_t n, char c);
template <class InputIterator>
string& append (InputIterator first, InputIterator last);
string& append (initializer_list<char> il);
push_back void push_back (char c); Append character to string
assign string& assign (const   string& str); Assign content to string
string& assign (const string& str, size_t subpos, size_t sublen);
string& assign (const char* s);
string& assign (const char* s, size_t n);
string& assign (size_t n, char c);
template <class InputIterator>
string& assign (InputIterator first, InputIterator last);
string& assign (initializer_list<char> il);
string& assign (string&& str) noexcept;
insert string& insert (size_t pos, const string& str); Insert into string
string& insert (size_t pos, const   string& str, size_t subpos, size_t sublen);
string& insert (size_t pos, const   char* s);
string& insert (size_t pos, const   char* s, size_t n);
string& insert (size_t pos, size_t n, char c);
iterator insert (const_iterator p, size_t n, char c);
iterator insert (const_iterator p, char c);
template <class InputIterator>
iterator insert (iterator p, InputIterator first, InputIterator last);
string& insert (const_iterator p, initializer_list<char> il);
erase string& erase (size_t pos = 0, size_t len = npos); Erase characters from   string 
iterator erase (const_iterator p);
iterator erase (const_iterator first,   const_iterator last);
replace string& replace (size_t pos,   size_t len, const string& str);
string& replace (const_iterator i1, const_iterator i2, const   string& str);
Replace portion of   string
string& replace (size_t pos, size_t len, const string& str,
                        size_t subpos,   size_t sublen);
string& replace (size_t pos, size_t len, const char* s);
string& replace (const_iterator i1, const_iterator i2, const char* s);
string& replace (size_t pos, size_t len, const char* s, size_t n);
string& replace (const_iterator i1, const_iterator i2, const char* s, size_t n);
string& replace (size_t pos, size_t len, size_t n, char c);
string& replace (const_iterator i1, const_iterator i2, size_t n, char c);
template <class InputIterator>
string& replace (const_iterator i1, const_iterator i2,
                        InputIterator first, InputIterator last);
string& replace (const_iterator i1, const_iterator i2, initializer_list<char> il);
swap void swap (string& str); Swap string values
pop_back  void pop_back(); Delete last character

  示例:

// string::operator+=
#include <iostream>
#include <string>

int main ()
{
  std::string name ("John");
  std::string family ("Smith");
  name += " K. ";         // c-string
  name += family;         // string
  name += '\n';           // character

  std::cout << name;
  return 0;
}

Output:
John K. Smith
------------------------------------------------------------
// appending to string
#include <iostream>
#include <string>

int main ()
{
  std::string str;
  std::string str2="Writing ";
  std::string str3="print 10 and then 5 more";

  // used in the same order as described above:
  str.append(str2);                       // "Writing "
  str.append(str3,6,3);                   // "10 "
  str.append("dots are cool",5);          // "dots "
  str.append("here: ");                   // "here: "
  str.append(10u,'.');                    // ".........."
  str.append(str3.begin()+8,str3.end());  // " and then 5 more"
  str.append<int>(5,0x2E);                // "....."

  std::cout << str << '\n';
  return 0;
}

Output:
Writing 10 dots here: .......... and then 5 more.....
------------------------------------------------------------
// string::push_back
#include <iostream>
#include <fstream>
#include <string>

int main ()
{
  std::string str;
  std::ifstream file ("test.txt",std::ios::in);
  if (file) {
    while (!file.eof()) str.push_back(file.get());
  }
  std::cout << str << '\n';
  return 0;
}
------------------------------------------------------------
// string::assign
#include <iostream>
#include <string>

int main ()
{
  std::string str;
  std::string base="The quick brown fox jumps over a lazy dog.";

  // used in the same order as described above:

  str.assign(base);
  std::cout << str << '\n';

  str.assign(base,10,9);
  std::cout << str << '\n';         // "brown fox"

  str.assign("pangrams are cool",7);
  std::cout << str << '\n';         // "pangram"

  str.assign("c-string");
  std::cout << str << '\n';         // "c-string"

  str.assign(10,'*');
  std::cout << str << '\n';         // "**********"

  str.assign<int>(10,0x2D);
  std::cout << str << '\n';         // "----------"

  str.assign(base.begin()+16,base.end()-12);
  std::cout << str << '\n';         // "fox jumps over"

  return 0;
}

Output:
The quick brown fox jumps over a lazy dog.
brown fox
pangram
c-string
**********
----------
fox jumps over
------------------------------------------------------------
// inserting into a string
#include <iostream>
#include <string>

int main ()
{
  std::string str="to be question";
  std::string str2="the ";
  std::string str3="or not to be";
  std::string::iterator it;

  // used in the same order as described above:
  str.insert(6,str2);                 // to be (the )question
  str.insert(6,str3,3,4);             // to be (not )the question
  str.insert(10,"that is cool",8);    // to be not (that is )the question
  str.insert(10,"to be ");            // to be not (to be )that is the question
  str.insert(15,1,':');               // to be not to be(:) that is the question
  it = str.insert(str.begin()+5,','); // to be(,) not to be: that is the question
  str.insert (str.end(),3,'.');       // to be, not to be: that is the question(...)
  str.insert (it+2,str3.begin(),str3.begin()+3); // (or )

  std::cout << str << '\n';
  return 0;
}

Output:
to be, or not to be: that is the question...
------------------------------------------------------------
// string::erase
#include <iostream>
#include <string>

int main ()
{
  std::string str ("This is an example sentence.");
  std::cout << str << '\n';
                                           // "This is an example sentence."
  str.erase (10,8);                        //            ^^^^^^^^
  std::cout << str << '\n';
                                           // "This is an sentence."
  str.erase (str.begin()+9);               //           ^
  std::cout << str << '\n';
                                           // "This is a sentence."
  str.erase (str.begin()+5, str.end()-9);  //       ^^^^^
  std::cout << str << '\n';
                                           // "This sentence."
  return 0;
}

Output:
This is an example sentence.
This is an sentence.
This is a sentence.
This sentence.
------------------------------------------------------------
// replacing in a string
#include <iostream>
#include <string>

int main ()
{
  std::string base="this is a test string.";
  std::string str2="n example";
  std::string str3="sample phrase";
  std::string str4="useful.";

  // replace signatures used in the same order as described above:

  // Using positions:                 0123456789*123456789*12345
  std::string str=base;           // "this is a test string."
  str.replace(9,5,str2);          // "this is an example string." (1)
  str.replace(19,6,str3,7,6);     // "this is an example phrase." (2)
  str.replace(8,10,"just a");     // "this is just a phrase."     (3)
  str.replace(8,6,"a shorty",7);  // "this is a short phrase."    (4)
  str.replace(22,1,3,'!');        // "this is a short phrase!!!"  (5)

  // Using iterators:                                               0123456789*123456789*
  str.replace(str.begin(),str.end()-3,str3);                    // "sample phrase!!!"      (1)
  str.replace(str.begin(),str.begin()+6,"replace");             // "replace phrase!!!"     (3)
  str.replace(str.begin()+8,str.begin()+14,"is coolness",7);    // "replace is cool!!!"    (4)
  str.replace(str.begin()+12,str.end()-4,4,'o');                // "replace is cooool!!!"  (5)
  str.replace(str.begin()+11,str.end(),str4.begin(),str4.end());// "replace is useful."    (6)
  std::cout << str << '\n';
  return 0;
}

Output:
replace is useful.
------------------------------------------------------------
// swap strings
#include <iostream>
#include <string>

main ()
{
  std::string buyer ("money");
  std::string seller ("goods");

  std::cout << "Before the swap, buyer has " << buyer;
  std::cout << " and seller has " << seller << '\n';

  seller.swap (buyer);

  std::cout << " After the swap, buyer has " << buyer;
  std::cout << " and seller has " << seller << '\n';

  return 0;
}

Output:
Before the swap, buyer has money and seller has goods
 After the swap, buyer has goods and seller has money
------------------------------------------------------------
// string::pop_back
#include <iostream>
#include <string>

int main ()
{
  std::string str ("hello world!");
  str.pop_back();
  std::cout << str << '\n';
  return 0;
}

Output:
hello world
示例代码
  • 字符串操作
函数  函数原型 功能描述
c_str const char* c_str() const noexcept; Get C string equivalent
data const char* data() const noexcept; Get string data
get_allocator allocator_type get_allocator() const noexcept; Get allocator
copy size_t copy (char* s, size_t len, size_t pos = 0) const; Copy sequence of characters from string
find size_t find (const string& str, size_t pos = 0) const noexcept; Find content in string
size_t find (const char* s, size_t pos = 0) const;
size_t find (const char* s, size_t pos, size_type n) const;
size_t find (char c, size_t pos = 0) const noexcept;
rfind size_t rfind (const string&   str, size_t pos = npos) const noexcept; Find last occurrence of content in string
size_t rfind (const char* s, size_t pos = npos) const;
size_t rfind (const char* s, size_t pos, size_t n) const;
size_t rfind (char c, size_t pos = npos) const noexcept;
find_first_of size_t find_first_of (const string& str, size_t pos = 0) const noexcept; Find character in string. Searches the string for the first character that matches any of the characters specified in its arguments.
size_t find_first_of (const char* s, size_t pos = 0) const;
size_t find_first_of (const char* s, size_t pos, size_t n) const;
size_t find_first_of (char c, size_t pos = 0) const noexcept;
find_last_of size_t find_last_of (const   string& str, size_t pos = npos) const noexcept; Find character in string from the end. Searches the string for the last character that matches any of the characters specified in its arguments.
size_t find_last_of (const char* s, 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 (char c,   size_t pos = npos) const noexcept;
find_first_not_of size_t find_first_not_of (const   string& str, size_t pos = 0) const noexcept; Find absence of character in string. Searches the string for the first character that does not match any of the characters specified in its arguments.
size_t find_first_not_of (const char* s, size_t pos = 0) const;
size_t find_first_not_of (const char* s, size_t pos, size_t n) const;
size_t find_first_not_of (char c, size_t pos = 0) const noexcept;
find_last_not_of size_t find_last_not_of (const   string& str, size_t pos = npos) const noexcept; Find non-matching character in string from the end. Searches the string for the last character that does not match any of the characters specified in its arguments.
size_t find_last_not_of (const char* s, size_t pos = npos) const;
size_t find_last_not_of (const char* s, size_t pos, size_t n) const;
size_t find_last_not_of (char c, size_t pos = npos) const noexcept;
substr string substr (size_t pos = 0, size_t len = npos) const; Generate substring
compare int compare (const string&   str) const noexcept; Compare strings
int compare (size_t pos, size_t len, const string& str) const;
int compare (size_t pos, size_t len, const string& str,
                   size_t subpos, size_t   sublen) const;
int compare (size_t pos, size_t len, const string& str) const;
int compare (size_t pos, size_t len, const string& str,
                   size_t subpos, size_t   sublen) const;
int compare (size_t pos, size_t len, const char* s, size_t n) const;

  示例:

// strings and c-strings
#include <iostream>
#include <cstring>
#include <string>

int main ()
{
  std::string str ("Please split this sentence into tokens");

  char * cstr = new char [str.length()+1];
  std::strcpy (cstr, str.c_str());

  // cstr now contains a c-string copy of str

  char * p = std::strtok (cstr," ");
  while (p!=0)
  {
    std::cout << p << '\n';
    p = std::strtok(NULL," ");
  }

  delete[] cstr;
  return 0;
}

Output:
Please
split
this
sentence
into
tokens
------------------------------------------------------------------
// string::data
#include <iostream>
#include <string>
#include <cstring>

int main ()
{
  int length;

  std::string str = "Test string";
  char* cstr = "Test string";

  if ( str.length() == std::strlen(cstr) )
  {
    std::cout << "str and cstr have the same length.\n";

    if ( memcmp (cstr, str.data(), str.length() ) == 0 )
      std::cout << "str and cstr have the same content.\n";
  }
  return 0;
}

Output:
str and cstr have the same length.
str and cstr have the same content.
------------------------------------------------------------------
// string::copy
#include <iostream>
#include <string>

int main ()
{
  char buffer[20];
  std::string str ("Test string...");
  std::size_t length = str.copy(buffer,6,5);
  buffer[length]='\0';
  std::cout << "buffer contains: " << buffer << '\n';
  return 0;
}

Output:
buffer contains: string
------------------------------------------------------------------
// string::find
#include <iostream>       // std::cout
#include <string>         // std::string

int main ()
{
  std::string str ("There are two needles in this haystack with needles.");
  std::string str2 ("needle");

  // different member versions of find in the same order as above:
  std::size_t found = str.find(str2);
  if (found!=std::string::npos)
    std::cout << "first 'needle' found at: " << found << '\n';

  found=str.find("needles are small",found+1,6);
  if (found!=std::string::npos)
    std::cout << "second 'needle' found at: " << found << '\n';

  found=str.find("haystack");
  if (found!=std::string::npos)
    std::cout << "'haystack' also found at: " << found << '\n';

  found=str.find('.');
  if (found!=std::string::npos)
    std::cout << "Period found at: " << found << '\n';

  // let's replace the first needle:
  str.replace(str.find(str2),str2.length(),"preposition");
  std::cout << str << '\n';

  return 0;
}

Output:
first 'needle' found at: 14
second 'needle' found at: 44
'haystack' also found at: 30
Period found at: 51
There are two prepositions in this haystack with needles.
------------------------------------------------------------------
// string::find_first_of
#include <iostream>       // std::cout
#include <string>         // std::string
#include <cstddef>        // std::size_t

int main ()
{
  std::string str ("Please, replace the vowels in this sentence by asterisks.");
  std::size_t found = str.find_first_of("aeiou");
  while (found!=std::string::npos)
  {
    str[found]='*';
    found=str.find_first_of("aeiou",found+1);
  }

  std::cout << str << '\n';

  return 0;
}

Output:
Pl**s*, r*pl*c* th* v*w*ls *n th*s s*nt*nc* by *st*r*sks.
------------------------------------------------------------------
// string::find_last_of
#include <iostream>       // std::cout
#include <string>         // std::string
#include <cstddef>         // std::size_t

void SplitFilename (const std::string& str)
{
  std::cout << "Splitting: " << str << '\n';
  std::size_t found = str.find_last_of("/\\");
  std::cout << " path: " << str.substr(0,found) << '\n';
  std::cout << " file: " << str.substr(found+1) << '\n';
}

int main ()
{
  std::string str1 ("/usr/bin/man");
  std::string str2 ("c:\\windows\\winhelp.exe");

  SplitFilename (str1);
  SplitFilename (str2);

  return 0;
}

Output:
Splitting: /usr/bin/man
 path: /usr/bin
 file: man
Splitting: c:\windows\winhelp.exe
 path: c:\windows
 file: winhelp.exe
------------------------------------------------------------------
// string::find_first_not_of
#include <iostream>       // std::cout
#include <string>         // std::string
#include <cstddef>        // std::size_t

int main ()
{
  std::string str ("look for non-alphabetic characters...");

  std::size_t found = str.find_first_not_of("abcdefghijklmnopqrstuvwxyz ");

  if (found!=std::string::npos)
  {
    std::cout << "The first non-alphabetic character is " << str[found];
    std::cout << " at position " << found << '\n';
  }

  return 0;
}

Output:
The first non-alphabetic character is - at position 12
------------------------------------------------------------------
// string::find_last_not_of
#include <iostream>       // std::cout
#include <string>         // std::string
#include <cstddef>        // std::size_t

int main ()
{
  std::string str ("Please, erase trailing white-spaces   \n");
  std::string whitespaces (" \t\f\v\n\r");

  std::size_t found = str.find_last_not_of(whitespaces);
  if (found!=std::string::npos)
    str.erase(found+1);
  else
    str.clear();            // str is all whitespace

  std::cout << '[' << str << "]\n";

  return 0;
}

Output:
[Please, erase trailing white-spaces]
------------------------------------------------------------------
// string::substr
#include <iostream>
#include <string>

int main ()
{
  std::string str="We think in generalities, but we live in details.";
                                           // (quoting Alfred N. Whitehead)

  std::string str2 = str.substr (3,5);     // "think"

  std::size_t pos = str.find("live");      // position of "live" in str

  std::string str3 = str.substr(pos);      // get from "live" to the end

  std::cout << str2 << ' ' << str3 << '\n';

  return 0;
}

Output:
think live in details.
------------------------------------------------------------------
// comparing apples with apples
#include <iostream>
#include <string>

int main ()
{
  std::string str1 ("green apple");
  std::string str2 ("red apple");

  if (str1.compare(str2) != 0)
    std::cout << str1 << " is not " << str2 << '\n';

  if (str1.compare(6,5,"apple") == 0)
    std::cout << "still, " << str1 << " is an apple\n";

  if (str2.compare(str2.size()-5,5,"apple") == 0)
    std::cout << "and " << str2 << " is also an apple\n";

  if (str1.compare(6,5,str2,4,5) == 0)
    std::cout << "therefore, both are apples\n";

  return 0;
}

Output:
green apple is not red apple
still, green apple is an apple
and red apple is also an apple
therefore, both are apples
示例代码

 

成员常量

npos static const size_t npos = -1; Maximum value for size_t 

  当用作string成员函数中len(或sublen)参数的值时,其表示“直到字符串结束”。而作为返回值时,它通常用于表示不匹配。

 

重载的非成员函数

函数 函数原型 功能描述
operator+ string operator+ (const string& lhs, const string& rhs); Concatenate strings
string operator+ (string&& lhs, string&& rhs);
string operator+ (string&& lhs, const string& rhs);
string operator+ (const string& lhs, string&& rhs);
string operator+ (const string& lhs, const char* rhs);
string operator+ (string&& lhs, const char* rhs);
string operator+ (const char* lhs, const string& rhs);
string operator+ (const char* lhs, string&& rhs);
string operator+ (const string& lhs, char rhs);
string operator+ (string&& lhs, char rhs);
string operator+ (char lhs, const string& rhs);
string operator+ (char lhs, string&& rhs);
relational   operators bool operator== (const string& lhs, const string& rhs); Relational operators for string
bool operator== (const char* lhs, const string& rhs);
bool operator== (const string& lhs, const char* rhs);
bool operator!= (const string& lhs, const string& rhs);
bool operator!= (const char* lhs, const string& rhs);
bool operator!= (const string& lhs, const char* rhs);
bool operator<  (const string& lhs, const string& rhs);
bool operator<  (const char* lhs, const string& rhs);
bool operator<  (const string& lhs, const char* rhs);
bool operator<= (const string& lhs, const string& rhs);
bool operator<= (const char* lhs, const string& rhs);
bool operator<= (const string& lhs, const char* rhs);
bool operator>  (const string& lhs, const string& rhs);
bool operator>  (const char* lhs, const string& rhs);
bool operator>  (const string& lhs, const char* rhs);
bool operator>= (const string& lhs, const string& rhs);
bool operator>= (const char* lhs, const string& rhs);
bool operator>= (const string& lhs, const char* rhs);
swap void swap (string& x, string& y); Exchanges the values of two strings 
operator>>
istream& operator>> (istream& is, string& str);
Extract string from stream 
operator<< ostream& operator<< (ostream& os, const string& str); Insert string into stream
getline istream& getline (istream&  is, string& str, char delim); Get line from stream into string
istream& getline (istream&& is, string& str, char delim);
istream& getline (istream&  is, string& str);
istream& getline (istream&& is, string& str);

 

注:std::string功能还不是很完善,有些常用方法(比如:去除字符串首尾空字符等功能)还是没有,使用起来不是很方便,这个时候可以选择使用boost中的相应函数。

 

翻译、参考:

  http://www.cplusplus.com/reference/string/string/?kw=string

posted on 2019-01-15 10:59  泣血  阅读(21408)  评论(0编辑  收藏  举报

导航