添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

basic_string 类型的一个对象控制的序列是标准 C++ 字符串类且作为字符串被引用,但不应将它们与以 null 结尾的通用于 C++ 标准库的 C 样式字符串相混淆。 标准 C++ 字符串是一个容器,它可使字符串作为普通类型使用,例如,比较和连接操作、迭代器、C++ 标准库算法以及复制由类分配器管理的内存和使用它进行分配。 如需要将标准 C++ 字符串转换为以 null 结尾的 C 样式字符串,请使用 basic_string::c_str 成员。

template <class CharType, class Traits = char_traits<CharType>, class Allocator = allocator<CharType>>
class basic_string;

CharType
要存储在字符串中的单个字符的数据类型。 C++ 标准库使用类型为 char 的元素的类型定义 string、类型为 wchar_t 的元素的类型定义 wstring、类型为 char16_t 的元素的类型定义 u16string 以及类型为 char32_t 的元素的类型定义 u32string 提供此类模板的专用化。

Traits
basic_string 专用化中 CharType 元素的各种重要属性由类 Traits 描述。 默认值为 char_traits<CharType>。

Allocator
一种表示存储的分配器对象的类型,该分配器对象封装有关字符串的内存分配和解除分配的详细信息。 默认值为 allocator<CharType>

inline basic_string<char8_t> operator"" s(const char8_t* str, size_t len) 返回:basic_string<char8_t>(str, len) inline u16string operator"" s(const char16_t* str, size_t len) 返回:u16string(str, len) inline u32string operator"" s(const char32_t* str, size_t len) 返回:u32string(str, len)

如果要求函数生成的序列长于 max_size 元素,这个函数将通过引发 length_error 类型的对象来报告长度错误。

用于指定受控制序列元素的引用、指针和迭代器在调用了可更改受控制序列的函数后或第一次调用一个非 const 成员函数后可能失效。

标头:<string>

命名空间: std

basic_string::allocator_type

表示字符串对象的分配器类的类型。

typedef Allocator allocator_type;

该类型是模板参数 Allocator 的同义词。

// basic_string_allocator_type.cpp
// compile with: /EHsc
#include <string>
#include <iostream>
int main( )
   using namespace std;
   // The following lines declare objects
   // that use the default allocator.
   string s1;
   basic_string <char>::allocator_type xchar = s1.get_allocator( );
   // You can now call functions on the allocator class xchar used by s1

basic_string::append

向字符串的末尾添加字符。

basic_string<CharType, Traits, Allocator>& append(
    const value_type* ptr);
basic_string<CharType, Traits, Allocator>& append(
    const value_type* ptr,
    size_type count);
basic_string<CharType, Traits, Allocator>& append(
    const basic_string<CharType, Traits, Allocator>& str,
    size_type offset,
    size_type count);
basic_string<CharType, Traits, Allocator>& append(
    const basic_string<CharType, Traits, Allocator>& str);
basic_string<CharType, Traits, Allocator>& append(
    size_type count,
    value_type char_value);
template <class InputIterator>
basic_string<CharType, Traits, Allocator>& append(
    InputIterator first,
    InputIterator last);
basic_string<CharType, Traits, Allocator>& append(
    const_pointer first,
    const_pointer last);
basic_string<CharType, Traits, Allocator>& append(
    const_iterator first,
    const_iterator last);
要追加的 C 字符串。

要追加字符的字符串。

offset
提供要追加的字符的源字符串部分的索引。

count
要从源字符串追加的字符的最大数目。

char_value
要追加的字符值。

first
一种输入迭代器。用于寻址要追加的范围中的第一个元素。

一种输入迭代器(const_pointerconst_iterator),用于寻址要追加的范围中超出最后一个元素的元素位置。

使用由成员函数传递的字符追加的字符串对象的引用。

可以使用 operator+= 或成员函数 appendpush_back 将字符追加到字符串。 operator+= 追加单一参数值,而多参数 append 成员函数允许指定字符串的特定部分用于添加。

// basic_string_append.cpp
// compile with: /EHsc
#include <string>
#include <iostream>
int main( )
   using namespace std;
   // The first member function
   // appending a C-string to a string
   string str1a ( "Hello " );
   cout << "The original string str1 is: " << str1a << endl;
   const char *cstr1a = "Out There ";
   cout << "The C-string cstr1a is: " << cstr1a << endl;
   str1a.append ( cstr1a );
   cout << "Appending the C-string cstr1a to string str1 gives: "
        << str1a << "." << endl << endl;
   // The second member function
   // appending part of a C-string to a string
   string str1b ( "Hello " );
   cout << "The string str1b is: " << str1b << endl;
   const char *cstr1b = "Out There ";
   cout << "The C-string cstr1b is: " << cstr1b << endl;
   str1b.append ( cstr1b , 3 );
   cout << "Appending the 1st part of the C-string cstr1b "
        << "to string str1 gives: " << str1b << "."
        << endl << endl;
   // The third member function
   // appending part of one string to another
   string str1c ( "Hello " ), str2c ( "Wide World " );
   cout << "The string str2c is: " << str2c << endl;
   str1c.append ( str2c , 5 , 5 );
   cout << "The appended string str1 is: "
        << str1c << "." << endl << endl;
   // The fourth member function
   // appending one string to another in two ways,
   // comparing append and operator [ ]
   string str1d ( "Hello " ), str2d ( "Wide " ), str3d ( "World " );
   cout << "The  string str2d is: " << str2d << endl;
   str1d.append ( str2d );
   cout << "The appended string str1d is: "
        << str1d << "." << endl;
   str1d += str3d;
   cout << "The doubly appended strig str1 is: "
        << str1d << "." << endl << endl;
   // The fifth member function
   // appending characters to a string
   string str1e ( "Hello " );
   str1e.append ( 4 , '!' );
   cout << "The string str1 appended with exclamations is: "
        << str1e << endl << endl;
   // The sixth member function
   // appending a range of one string to another
   string str1f ( "Hello " ), str2f ( "Wide World " );
   cout << "The string str2f is: " << str2f << endl;
   str1f.append ( str2f.begin ( ) + 5 , str2f.end ( ) - 1 );
   cout << "The appended string str1 is: "
        << str1f << "." << endl << endl;
The original string str1 is: Hello
The C-string cstr1a is: Out There
Appending the C-string cstr1a to string str1 gives: Hello Out There .
The string str1b is: Hello
The C-string cstr1b is: Out There
Appending the 1st part of the C-string cstr1b to string str1 gives: Hello Out.
The string str2c is: Wide World
The appended string str1 is: Hello World.
The  string str2d is: Wide
The appended string str1d is: Hello Wide .
The doubly appended strig str1 is: Hello Wide World .
The string str1 appended with exclamations is: Hello !!!!
The string str2f is: Wide World
The appended string str1 is: Hello World.

basic_string::assign

对字符串的内容赋新的字符值。

basic_string<CharType, Traits, Allocator>& assign(
    const value_type* ptr);
basic_string<CharType, Traits, Allocator>& assign(
    const value_type* ptr,
    size_type count);
basic_string<CharType, Traits, Allocator>& assign(
    const basic_string<CharType, Traits, Allocator>& str,
    size_type off,
    size_type count);
basic_string<CharType, Traits, Allocator>& assign(
    const basic_string<CharType, Traits, Allocator>& str);
basic_string<CharType, Traits, Allocator>& assign(
    size_type count,
    value_type char_value);
template <class InIt>
basic_string<CharType, Traits, Allocator>& assign(
    InputIterator first,
    InputIterator last);
basic_string<CharType, Traits, Allocator>& assign(
    const_pointer first,
    const_pointer last);
basic_string<CharType, Traits, Allocator>& assign(
    const_iterator first,
    const_iterator last);
指向要分配给目标字符串的 C 字符串字符的指针。

count
要从源字符串分配的字符的数目。

要分配给目标字符串的字符的源字符串。

char_value
要分配的字符值。

first
一种输入迭代器(const_pointer 或 const_iterator),用于寻址要分配给目标范围的源字符串范围中的第一个字符。

一种输入迭代器(const_pointer 或 const_iterator),用于寻址要分配给目标范围的源字符串范围中超出最后一个字符的字符。

开始分配新字符的位置。

由成员函数分配新字符的字符串对象的引用。

可为这些字符串分配新字符值。 新值可以是字符串和 C 字符串或单个字符。 如果可由单一参数描述新值,则可使用 operator=,否则使用成员函数 assign,该函数具有多个参数,可用于指定向目标字符串分配字符串的哪个部分。

// basic_string_assign.cpp
// compile with: /EHsc
#include <string>
#include <iostream>
int main( )
   using namespace std;
   // The first member function assigning the
   // characters of a C-string to a string
   string str1a;
   const char *cstr1a = "Out There";
   cout << "The C-string cstr1a is: " << cstr1a <<  "." << endl;
   str1a.assign ( cstr1a );
   cout << "Assigning the C-string cstr1a to string str1 gives: "
        << str1a << "." << endl << endl;
   // The second member function assigning a specific
   // number of the of characters a C-string to a string
   string  str1b;
   const char *cstr1b = "Out There";
   cout << "The C-string cstr1b is: " << cstr1b << endl;
   str1b.assign ( cstr1b , 3 );
   cout << "Assigning the 1st part of the C-string cstr1b "
        << "to string str1 gives: " << str1b << "."
        << endl << endl;
   // The third member function assigning a specific number
   // of the characters from one string to another string
   string str1c ( "Hello " ), str2c ( "Wide World " );
   cout << "The string str2c is: " << str2c << endl;
   str1c.assign ( str2c , 5 , 5 );
   cout << "The newly assigned string str1 is: "
        << str1c << "." << endl << endl;
   // The fourth member function assigning the characters
   // from one string to another string in two equivalent
   // ways, comparing the assign and operator =
   string str1d ( "Hello" ), str2d ( "Wide" ), str3d ( "World" );
   cout << "The original string str1 is: " << str1d << "." << endl;
   cout << "The string str2d is: " << str2d << endl;
   str1d.assign ( str2d );
   cout << "The string str1 newly assigned with string str2d is: "
        << str1d << "." << endl;
   cout << "The string str3d is: " << str3d << "." << endl;
   str1d = str3d;
   cout << "The string str1 reassigned with string str3d is: "
        << str1d << "." << endl << endl;
   // The fifth member function assigning a specific
   // number of characters of a certain value to a string
   string str1e ( "Hello " );
   str1e.assign ( 4 , '!' );
   cout << "The string str1 assigned with eclamations is: "
        << str1e << endl << endl;
   // The sixth member function assigning the value from
   // the range of one string to another string
   string str1f ( "Hello " ), str2f ( "Wide World " );
   cout << "The string str2f is: " << str2f << endl;
   str1f.assign ( str2f.begin ( ) + 5 , str2f.end ( ) - 1 );
   cout << "The string str1 assigned a range of string str2f is: "
        << str1f << "." << endl << endl;
The C-string cstr1a is: Out There.
Assigning the C-string cstr1a to string str1 gives: Out There.
The C-string cstr1b is: Out There
Assigning the 1st part of the C-string cstr1b to string str1 gives: Out.
The string str2c is: Wide World
The newly assigned string str1 is: World.
The original string str1 is: Hello.
The string str2d is: Wide
The string str1 newly assigned with string str2d is: Wide.
The string str3d is: World.
The string str1 reassigned with string str3d is: World.
The string str1 assigned with eclamations is: !!!!
The string str2f is: Wide World
The string str1 assigned a range of string str2f is: World.

basic_string::at

使用字符串中的指定索引提供对字符的引用。

const_reference at(size_type offset) const;
reference at(size_type offset);

offset
要引用的元素的位置索引。

由参数索引指定的位置的字符串字符的引用。

字符串的第一个元素的索引为零,其后续元素以正整数进行连续地索引,因此,长度为 n 的字符串具有由数字 n - 1 索引的第 n个元素。

在提供对字符串元素的读取和写入访问权限时,成员 operator[] 比成员函数 at 更快。

成员 operator[] 并不检查以参数进行传递的索引是否有效,但成员函数 at 进行此项检查,因此应在不确定其有效性时使用它。 传递到成员函数 at 的无效索引(小于零或大于或等于字符串大小的索引)引发 out_of_range异常。 传递给 operator[] 的无效索引导致未定义行为,但是等于字符串长度的索引对于常量字符串而言是有效索引,且运算符在传递此索引时返回空字符。

重新分配字符串或修改非 const 字符串可能使返回的引用无效。

// basic_string_at.cpp
// compile with: /EHsc
#include <string>
#include <iostream>
int main( )
   using namespace std;
   string str1 ( "Hello world" ), str2 ( "Goodbye world" );
   const string  cstr1 ( "Hello there" ), cstr2 ( "Goodbye now" );
   cout << "The original string str1 is: " << str1 << endl;
   cout << "The original string str2 is: " << str2 << endl;
   // Element access to the non const strings
   basic_string <char>::reference refStr1 = str1 [6];
   basic_string <char>::reference refStr2 = str2.at ( 3 );
   cout << "The character with an index of 6 in string str1 is: "
        << refStr1 << "." << endl;
   cout << "The character with an index of 3 in string str2 is: "
        << refStr2 << "." << endl;
   // Element access to the const strings
   basic_string <char>::const_reference crefStr1 = cstr1 [ cstr1.length ( ) ];
   basic_string <char>::const_reference crefStr2 = cstr2.at ( 8 );
   if ( crefStr1 == '\0' )
      cout << "The null character is returned as a valid reference."
           << endl;
      cout << "The null character is not returned." << endl;
   cout << "The character with index 8 in the const string cstr2 is: "
        << crefStr2 << "." << endl;

basic_string::back

返回对字符串中最后一个元素的引用。

const_reference back() const;
reference back();

对字符串中最后一个元素的引用,必须为非空值。

basic_string::basic_string

构造一个字符串,它为空、由特定字符初始化,或者是另一个字符串对象或 C 样式(以 null 终止)字符串的全部或部分的副本。

basic_string();
explicit basic_string(
    const allocator_type& alloc_type);
basic_string(
    const basic_string& right);
basic_string(
    basic_string&& right);
basic_string(
    const basic_string& right,
    size_type right_offset,
    size_type count = npos);
basic_string(
    const basic_string& right,
    size_type right_offset,
    size_type count,
    const allocator_type& alloc_type);
basic_string(
    const value_type* ptr,
    size_type count);
basic_string(
    const value_type* ptr,
    size_type count,
    const allocator_type& alloc_type);
basic_string(
    const value_type* ptr);
basic_string(
    const value_type* ptr,
    const allocator_type& alloc_type);
basic_string(
    size_type count,
    value_type char_value);
basic_string(
    size_type count,
    value_type char_value,
    const allocator_type& alloc_type);
template <class InputIterator>
basic_string(
    InputIterator first,
    InputIterator last);
template <class InputIterator>
basic_string(
    InputIterator first,
    InputIterator last,
    const allocator_type& alloc_type);
basic_string(
    const_pointer first,
    const_pointer last);
basic_string(
    const_iterator first,
    const_iterator last);
C 字符串,其字符将用于初始化正在构造的 string。 除非 count 为零,否则此值不能为空指针。

alloc_type
正在构造的字符串对象的存储分配器类。

count
要初始化的字符数。

right
用于初始化正在构造的字符串的字符串。

right_offset
字符串中字符的索引,该字符串会最先用于初始化正在构造的字符串的字符值。

char_value
要复制到正在构造的字符串中的字符值。

first
输入迭代器(const_pointer 或 const_iterator),用于寻址要插入的源范围中的第一个元素。

输入迭代器(const_pointer 或 const_iterator),用于寻址要插入的源范围中超出最后一个元素的元素的位置。

对构造函数正在构造的字符串对象的引用。

所有构造函数都存储 basic_string::allocator_type 并初始化受控序列。 分配器对象是参数 al(如果存在)。 对于复制构造函数,则为 right.get_allocator(),其调用 basic_string::get_allocator。 否则,分配器为 Alloc()

受控序列初始化为剩余操作数指定的操作数序列的副本。 没有操作数序列的构造函数指定空的初始受控序列。 如果 InputIterator 是模板构造函数中的整数类型,则操作数序列 first, last 的行为与 (size_type) first, (value_type) last 相同。

// basic_string_ctor.cpp
// compile with: /EHsc
#include <string>
#include <iostream>
int main( )
   using namespace std;
   // The first member function initializing with a C-string
   const char *cstr1a = "Hello Out There.";
   basic_string <char> str1a ( cstr1a , 5);
   cout << "The string initialized by C-string cstr1a is: "
        << str1a << "." << endl;
   // The second member function initializing with a string
   string  str2a ( "How Do You Do" );
   basic_string <char> str2b ( str2a , 7 , 7 );
   cout << "The string initialized by part of the string cstr2a is: "
        << str2b << "." << endl;
   // The third member function initializing a string
   // with a number of characters of a specific value
   basic_string <char> str3a ( 5, '9' );
   cout << "The string initialized by five number 9s is: "
        << str3a << endl;
   // The fourth member function creates an empty string
   // and string with a specified allocator
   basic_string <char> str4a;
   string str4b;
   basic_string <char> str4c ( str4b.get_allocator( ) );
   if (str4c.empty ( ) )
      cout << "The string str4c is empty." << endl;
      cout << "The string str4c is not empty." << endl;
   // The fifth member function initializes a string from
   // another range of characters
   string str5a ( "Hello World" );
   basic_string <char> str5b ( str5a.begin ( ) + 5 , str5a.end ( ) );
   cout << "The string initialized by another range is: "
        << str5b << "." << endl;

basic_string::begin

返回发现字符串中第一个元素的位置的迭代器。

const_iterator begin() const;
iterator begin();

一种随机访问迭代器,用于寻址序列的第一个元素或刚超出空序列末尾的位置。

// basic_string_begin.cpp
// compile with: /EHsc
#include <string>
#include <iostream>
int main( ) {
   using namespace std;
   string str1 ( "No way out." ), str2;
   basic_string <char>::iterator strp_Iter, str1_Iter, str2_Iter;
   basic_string <char>::const_iterator str1_cIter;
   str1_Iter = str1.begin ( );
   cout << "The first character of the string str1 is: "
        << *str1_Iter << endl;
   cout << "The full original string str1 is: " << str1 << endl;
   // The dereferenced iterator can be used to modify a character
*str1_Iter = 'G';
   cout << "The first character of the modified str1 is now: "
        << *str1_Iter << endl;
   cout << "The full modified string str1 is now: " << str1 << endl;
   // The following line would be an error because iterator is const
   // *str1_cIter = 'g';
   // For an empty string, begin is equivalent to end
   if (  str2.begin ( ) == str2.end ( ) )
      cout << "The string str2 is empty." << endl;
      cout << "The string str2 is not empty." << endl;

basic_string::c_str

将字符串的内容转换为以 null 结尾的 C 样式字符串。

const value_type *c_str() const;

指向调用字符串的 C 样式版本的指针。 指针值在调用非 const 函数(包括该对象上的 basic_string 类中的析构函数)后无效。

属于 basic_string<char> 类模板的字符串类型对象并不一定是以 null 结尾的。 空字符 '\0' 用作 C 字符串中的特殊字符,以标记字符串的末尾,但在类型字符串对象中并无特殊含义,且可能像其他字符一样是字符串的一部分。 存在从 const char * 到字符串的自动转换,但是字符串类并不提供从 C 样式字符串到 basic_string<char> 类型对象的自动转换。

不应修改返回的 C 样式字符串,这可能使指向字符串的指针无效;也不应将其删除,因为该字符串具有有限的生存期且归属于类字符串。

// basic_string_c_str.cpp
// compile with: /EHsc
#include <string>
#include <iostream>
int main( )
   using namespace std;
   string  str1 ( "Hello world" );
   cout << "The original string object str1 is: "
        << str1 << endl;
   cout << "The length of the string object str1 = "
        << str1.length ( ) << endl << endl;
   // Converting a string to an array of characters
   const char *ptr1 = 0;
   ptr1= str1.data ( );
   cout << "The modified string object ptr1 is: " << ptr1
        << endl;
   cout << "The length of character array str1 = "
        << strlen ( ptr1) << endl << endl;
   // Converting a string to a C-style string
   const char *c_str1 = str1.c_str ( );
   cout << "The C-style string c_str1 is: " << c_str1
        << endl;
   cout << "The length of C-style string str1 = "
        << strlen ( c_str1) << endl << endl;
The original string object str1 is: Hello world
The length of the string object str1 = 11
The modified string object ptr1 is: Hello world
The length of character array str1 = 11
The C-style string c_str1 is: Hello world
The length of C-style string str1 = 11

basic_string::capacity

返回在不增加字符串内存分配的情况下可存储在字符串中的元素的最大数目。

size_type capacity() const;

当前在内存中分配的用于存放字符串的存储空间大小。

成员函数返回当前分配的用于存放受控序列的存储空间,该值的大小至少为 size

// basic_string_capacity.cpp
// compile with: /EHsc
#include <string>
#include <iostream>
int main( )
   using namespace std;
   string  str1 ("Hello world");
   cout << "The original string str1 is: " << str1 << endl;
   // The size and length member functions differ in name only
   basic_string <char>::size_type sizeStr1, lenStr1;
   sizeStr1 = str1.size ( );
   lenStr1 = str1.length ( );
   basic_string <char>::size_type capStr1, max_sizeStr1;
   capStr1 = str1.capacity ( );
   max_sizeStr1 = str1.max_size ( );
   // Compare size, length, capacity & max_size of a string
   cout << "The current size of original string str1 is: "
        << sizeStr1 << "." << endl;
   cout << "The current length of original string str1 is: "
        << lenStr1 << "." << endl;
   cout << "The capacity of original string str1 is: "
        << capStr1 << "." << endl;
   cout << "The max_size of original string str1 is: "
        << max_sizeStr1 << "." << endl << endl;
   str1.erase ( 6, 5 );
   cout << "The modified string str1 is: " << str1 << endl;
   sizeStr1 = str1.size (  );
   lenStr1 = str1.length (  );
   capStr1 = str1.capacity (  );
   max_sizeStr1 = str1.max_size (  );
   // Compare size, length, capacity & max_size of a string
   // after erasing part of the original string
   cout << "The current size of modified string str1 is: "
        << sizeStr1 << "." << endl;
   cout << "The current length of modified string str1 is: "
        << lenStr1 << "." << endl;
   cout << "The capacity of modified string str1 is: "
        << capStr1 << "." << endl;
   cout << "The max_size of modified string str1 is: "
        << max_sizeStr1 << "." << endl;

basic_string::cbegin

返回确定范围中第一个元素地址的 const 迭代器。

const_iterator cbegin() const;

const 随机访问迭代器,指向范围的第一个元素,或刚超出空范围末尾的位置(对于空范围,cbegin() == cend())。

由于使用 cbegin 的返回值,因此不能修改范围中的元素。

可以使用此成员函数替代 begin() 成员函数,以保证返回值为 const_iterator。 它一般与 auto 类型推导关键字一起使用,如下例所示。 在此示例中,将 Container 视为支持 begin()cbegin() 的可修改的任何类型的(非 const)容器。

auto i1 = Container.begin();
// i1 is Container<T>::iterator
auto i2 = Container.cbegin();
// i2 is Container<T>::const_iterator

basic_string::cend

返回一个 const 迭代器,此迭代器用于发现刚超出范围中最后一个元素的位置。

const_iterator cend() const;

指向刚超出范围末尾的位置的 const 随机访问迭代器。

cend 用于测试迭代器是否超过了其范围的末尾。

可以使用此成员函数替代 end() 成员函数,以保证返回值为 const_iterator。 它一般与 auto 类型推导关键字一起使用,如下例所示。 在此示例中,将 Container 视为支持 end()cend() 的可修改的任何类型的(非 const)容器。

auto i1 = Container.end();
// i1 is Container<T>::iterator
auto i2 = Container.cend();
// i2 is Container<T>::const_iterator

不应对 cend 返回的值取消引用。

basic_string::clear

清除字符串中的全部元素。

void clear();

所调用的成员函数的字符串将为空。

// basic_string_clear.cpp
// compile with: /EHsc
#include <string>
#include <iostream>
int main( )
   using namespace std;
   string  str1 ("Hello world"), str2;
   basic_string <char>::iterator str_Iter;
   cout << "The original string str1 is: ";
   for ( str_Iter = str1.begin( ); str_Iter != str1.end( ); str_Iter++ )
      cout << *str_Iter;
   cout << endl;
   str1.clear ( );
   cout << "The modified string str1 is: ";
   for ( str_Iter = str1.begin( ); str_Iter != str1.end( ); str_Iter++ )
      cout << *str_Iter;
   cout << endl;
   //For an empty string, begin is equivalent to end
   if ( str1.begin ( ) == str1.end ( ) )
      cout << "Nothing printed above because "
           << "the string str1 is empty." << endl;
      cout << "The string str1 is not empty." << endl;
The original string str1 is: Hello world
The modified string str1 is:
Nothing printed above because the string str1 is empty.

basic_string::compare

与指定字符串进行区分大小写的比较,以确定两个字符串是否相等或按字典顺序一个字符串是否小于另一个。

int compare(
    const basic_string<CharType, Traits, Allocator>& str) const;
int compare(
    size_type position_1,
    size_type number_1,
    const basic_string<CharType, Traits, Allocator>& str) const;
int compare(
    size_type position_1,
    size_type number_1,
    const basic_string<CharType, Traits, Allocator>& str,
    size_type offset,
    size_type count) const;
int compare(
    const value_type* ptr) const;
int compare(
    size_type position_1,
    size_type number_1,
    const value_type* ptr) const;
int compare(
    size_type position_1,
    size_type number_1,
    const value_type* ptr
    size_type number_2) const;
要与操作数字符串比较的字符串。

position_1
开始进行比较的操作数字符串的索引。

number_1
要比较的操作数字符串的最大字符数。

number_2
要比较的参数字符串的最大字符数。

offset
开始进行比较的参数字符串的索引。

count
要比较的参数字符串的最大字符数。

要与操作数字符串比较的 C 字符串。

如果操作数字符串小于参数字符串,则为负值;如果两个字符串相等,则为 0;或者如果操作数字符串大于参数字符串,则为正值。

compare 成员函数比较所有或部分参数和操作数字符串,具体取决于所使用的部分。

比较是区分大小写的。

// basic_string_compare.cpp
// compile with: /EHsc
#include <string>
#include <iostream>
int main( )
   using namespace std;
   // The first member function compares
   // an operand string to a parameter string
   int comp1;
   string s1o ( "CAB" );
   string s1p ( "CAB" );
   cout << "The operand string is: " << s1o << endl;
   cout << "The parameter string is: " << s1p << endl;
   comp1 = s1o.compare ( s1p );
   if ( comp1 < 0 )
      cout << "The operand string is less than "
           << "the parameter string." << endl;
   else if ( comp1 == 0 )
      cout << "The operand string is equal to "
           << "the parameter string." << endl;
      cout << "The operand string is greater than "
           << "the parameter string." << endl;
   cout << endl;
   // The second member function compares part of
   // an operand string to a parameter string
   int comp2a, comp2b;
   string s2o ( "AACAB" );
   string s2p ( "CAB" );
   cout << "The operand string is: " << s2o << endl;
   cout << "The parameter string is: " << s2p << endl;
   comp2a = s2o.compare (  2 , 3 , s2p );
   if ( comp2a < 0 )
      cout << "The last three characters of "
           << "the operand string\n are less than "
           << "the parameter string." << endl;
   else if ( comp2a == 0 )
      cout << "The last three characters of "
           << "the operand string\n are equal to "
           << "the parameter string." << endl;
      cout << "The last three characters of "
           << "the operand string\n is greater than "
           << "the parameter string." << endl;
   comp2b = s2o.compare (  0 , 3 , s2p );
   if ( comp2b < 0 )
      cout << "The first three characters of "
           << "the operand string\n are less than "
           << "the parameter string." << endl;
   else if ( comp2b == 0 )
      cout << "The first three characters of "
           << "the operand string\n are equal to "
           << "the parameter string." << endl;
      cout << "The first three characters of "
           << "the operand string\n is greater than "
           << "the parameter string." << endl;
   cout << endl;
   // The third member function compares part of
   // an operand string to part of a parameter string
   int comp3a;
   string s3o ( "AACAB" );
   string s3p ( "DCABD" );
   cout << "The operand string is: " << s3o << endl;
   cout << "The parameter string is: " << s3p << endl;
   comp3a = s3o.compare (  2 , 3 , s3p , 1 , 3 );
   if ( comp3a < 0 )
      cout << "The three characters from position 2 of "
           << "the operand string are less than\n "
           << "the 3 characters parameter string "
           << "from position 1." << endl;
   else if ( comp3a == 0 )
      cout << "The three characters from position 2 of "
           << "the operand string are equal to\n "
           << "the 3 characters parameter string "
           << "from position 1." << endl;
      cout << "The three characters from position 2 of "
           << "the operand string is greater than\n "
           << "the 3 characters parameter string "
           << "from position 1." << endl;
   cout << endl;
   // The fourth member function compares
   // an operand string to a parameter C-string
   int comp4a;
   string s4o ( "ABC" );
   const char* cs4p = "DEF";
   cout << "The operand string is: " << s4o << endl;
   cout << "The parameter C-string is: " << cs4p << endl;
   comp4a = s4o.compare ( cs4p );
   if ( comp4a < 0 )
      cout << "The operand string is less than "
           << "the parameter C-string." << endl;
   else if ( comp4a == 0 )
      cout << "The operand string is equal to "
           << "the parameter C-string." << endl;
      cout << "The operand string is greater than "
           << "the parameter C-string." << endl;
   cout << endl;
   // The fifth member function compares part of
   // an operand string to a parameter C-string
   int comp5a;
   string s5o ( "AACAB" );
   const char* cs5p = "CAB";
   cout << "The operand string is: " << s5o << endl;
   cout << "The parameter string is: " << cs5p << endl;
   comp5a = s5o.compare (  2 , 3 , s2p );
   if ( comp5a < 0 )
      cout << "The last three characters of "
           << "the operand string\n are less than "
           << "the parameter C-string." << endl;
   else if ( comp5a == 0 )
      cout << "The last three characters of "
           << "the operand string\n are equal to "
           << "the parameter C-string." << endl;
      cout << "The last three characters of "
           << "the operand string\n is greater than "
           << "the parameter C-string." << endl;
   cout << endl;
   // The sixth member function compares part of
   // an operand string to part of an equal length of
   // a parameter C-string
   int comp6a;
   string s6o ( "AACAB" );
   const char* cs6p = "ACAB";
   cout << "The operand string is: " << s6o << endl;
   cout << "The parameter C-string is: " << cs6p << endl;
   comp6a = s6o.compare (  1 , 3 , cs6p , 3 );
   if ( comp6a < 0 )
      cout << "The 3 characters from position 1 of "
           << "the operand string are less than\n "
           << "the first 3 characters of the parameter C-string."
           << endl;
   else if ( comp6a == 0 )
      cout << "The 3 characters from position 2 of "
           << "the operand string are equal to\n "
           << "the first 3 characters of the parameter C-string."
           <<  endl;
      cout << "The 3 characters from position 2 of "
           << "the operand string is greater than\n "
           << "the first 3 characters of the parameter C-string."
           << endl;
   cout << endl;
The operand string is: CAB
The parameter string is: CAB
The operand string is equal to the parameter string.
The operand string is: AACAB
The parameter string is: CAB
The last three characters of the operand string
are equal to the parameter string.
The first three characters of the operand string
are less than the parameter string.
The operand string is: AACAB
The parameter string is: DCABD
The three characters from position 2 of the operand string are equal to
the 3 characters parameter string from position 1.
The operand string is: ABC
The parameter C-string is: DEF
The operand string is less than the parameter C-string.
The operand string is: AACAB
The parameter string is: CAB
The last three characters of the operand string
are equal to the parameter C-string.
The operand string is: AACAB
The parameter C-string is: ACAB
The 3 characters from position 2 of the operand string are equal to
the first 3 characters of the parameter C-string.

basic_string::const_iterator

提供可访问和读取字符串中 const 元素的随机访问迭代器的类型。

typedef implementation-defined const_iterator;

类型 const_iterator 不能用于修改字符值并用于正向循环访问字符串。

有关如何声明和使用 const_iterator 的示例,请参阅 begin 的示例。

basic_string::const_pointer

提供指向字符串中 const 元素的指针的类型。

typedef typename allocator_type::const_pointer const_pointer;

类型是 allocator_type::const_pointer 的同义词。

对于类型 string,它等效于 char*

对声明为常量的指针声明时,必须对其初始化。 Const 指针始终指向同一内存位置,且可能指向常量或非常量数据。

// basic_string_const_ptr.cpp
// compile with: /EHsc
#include <string>
#include <iostream>
int main( )
   using namespace std;
   basic_string<char>::const_pointer pstr1a = "In Here";
   const char *cstr1c = "Out There";
   cout << "The string pstr1a is: " << pstr1a <<  "." << endl;
   cout << "The C-string cstr1c is: " << cstr1c << "." << endl;
The string pstr1a is: In Here.
The C-string cstr1c is: Out There.

basic_string::const_reference

提供对存储于字符串中供读取和执行 const 操作的 const 元素的引用的类型。

typedef typename allocator_type::const_reference const_reference;

const_reference 类型不能用于修改元素的值。

类型是 allocator_type::const_reference 的同义词。 对于类型 string,它等效于常量 char&

有关如何声明和使用 const_reference 的示例,请参阅 at 的示例。

basic_string::const_reverse_iterator

提供可访问字符串中任何 const 元素的随机访问迭代器的类型。

typedef std::reverse_iterator<const_iterator> const_reverse_iterator;

类型 const_reverse_iterator 不能修改字符值,它用于反向循环访问字符串。

有关如何声明和使用 const_reverse_iterator 的示例,请参阅 rbegin 的示例。

basic_string::copy

将指定数目的字符从源字符串中的索引位置复制到目标字符组。

此方法可能并不安全,因为它依赖于调用方检查所传递的值是否正确。 请考虑改用 basic_string::_Copy_s

size_type copy(
    value_type* ptr,
    size_type count,
    size_type offset = 0) const;
要复制的元素的目标字符数组。

count 要从源字符串复制的字符的最大数目。

offset
要进行复制的源字符串中的开始位置。

复制的字符数。

空字符不追加到副本的末尾。

// basic_string_copy.cpp
// compile with: /EHsc /W3
#include <string>
#include <iostream>
int main( )
   using namespace std;
   string str1 ( "Hello World" );
   basic_string <char>::iterator str_Iter;
   char array1 [ 20 ] = { 0 };
   char array2 [ 10 ] = { 0 };
   basic_string <char>:: pointer array1Ptr = array1;
   basic_string <char>:: value_type *array2Ptr = array2;
   cout << "The original string str1 is: ";
   for ( str_Iter = str1.begin( ); str_Iter != str1.end( ); str_Iter++ )
      cout << *str_Iter;
   cout << endl;
   basic_string <char>:: size_type nArray1;
   // Note: string::copy is potentially unsafe, consider
   // using string::_Copy_s instead.
   nArray1 = str1.copy ( array1Ptr , 12 );  // C4996
   cout << "The number of copied characters in array1 is: "
        << nArray1 << endl;
   cout << "The copied characters array1 is: " << array1 << endl;
   basic_string <char>:: size_type nArray2;
   // Note: string::copy is potentially unsafe, consider
   // using string::_Copy_s instead.
   nArray2 = str1.copy ( array2Ptr , 5 , 6  );  // C4996
   cout << "The number of copied characters in array2 is: "
           << nArray2 << endl;
   cout << "The copied characters array2 is: " << array2Ptr << endl;
The original string str1 is: Hello World
The number of copied characters in array1 is: 11
The copied characters array1 is: Hello World
The number of copied characters in array2 is: 5
The copied characters array2 is: World

basic_string::crbegin

返回发现反向字符串中第一个元素的位置的常量迭代器。

const_reverse_iterator crbegin() const;

指向刚超出字符串末尾的位置的反向迭代器。 该位置指定反向序列的开头。

basic_string::crend

返回 const 迭代器,用于寻址反向字符串中最后一个元素之后的位置。

const_reverse_iterator crend() const;

用于发现反向字符串中最后一个元素之后的位置(非反向字符串中第一个元素之前的位置)的 const 反向迭代器。

basic_string::_Copy_s

将指定数目的字符从源字符串中的索引位置复制到目标字符组。

size_type _Copy_s(
    value_type* dest,
    size_type dest_size,
    size_type count,
    size_type offset = 0) const;
要复制的元素的目标字符数组。

dest_size
dest 的大小。

count 要从源字符串复制的字符的最大数目。

offset
要进行复制的源字符串中的开始位置。

复制的字符数。

空字符不追加到副本的末尾。 此函数特定于 Microsoft。

// basic_string__Copy_s.cpp
// compile with: /EHsc
#include <string>
#include <iostream>
int main( )
    using namespace std;
    string str1("Hello World");
    basic_string<char>::iterator str_Iter;
    const int array1_size = 20;
    char array1[array1_size] = { 0 };
    const int array2_size = 10;
    char array2[array2_size] = { 0 };
    basic_string<char>:: pointer array1Ptr = array1;
    basic_string<char>:: value_type *array2Ptr = array2;
    cout << "The original string str1 is: ";
    for (str_Iter = str1.begin(); str_Iter != str1.end(); str_Iter++)
        cout << *str_Iter;
    cout << endl;
    basic_string<char>::size_type nArray1;
    nArray1 = str1._Copy_s(array1Ptr, array1_size, 12);
    cout << "The number of copied characters in array1 is: "
         << nArray1 << endl;
    cout << "The copied characters array1 is: " << array1 << endl;
    basic_string<char>:: size_type nArray2;
    nArray2 = str1._Copy_s(array2Ptr, array2_size, 5, 6);
    cout << "The number of copied characters in array2 is: "
         << nArray2 << endl;
    cout << "The copied characters array2 is: " << array2Ptr << endl;
The original string str1 is: Hello World
The number of copied characters in array1 is: 11
The copied characters array1 is: Hello World
The number of copied characters in array2 is: 5
The copied characters array2 is: World

basic_string::data

将字符串的内容转换为以 null 结尾的字符数组。

const value_type *data() const noexcept;
value_type *data() noexcept;

指向包含该字符串内容的以 null 结尾的数组的第一个元素的指针。 对于空字符串,指针指向等于 value_type() 的单个 null 字符。

[data(), data() + size()] 有效范围内的 data 点返回的指针。 该范围中的每个元素都对应于该字符串中的当前数据。 即,在范围 data() + n == addressof(operator[](n)) 中每个有效偏移量为 n

如果修改 dataconst 重载返回的字符串内容,则不会定义行为。 如果将结尾 null 字符更改为任何其他值,则也不会定义行为。 如果将字符串的非 const 引用传递给标准库函数,则返回的指针可能会失效。 调用非 const 成员函数也会使它失效。 调用成员 atbackbeginendfrontrbeginrendoperator[] 不会使指针失效。

在 C++11 之前,data 不保证返回的字符串以 null 结尾。 从 C++11 开始,datac_str 均返回一个以 null 结尾的字符串,并且它们实际上相同。

const 重载是 C++17 中的新增功能。 若要使用它,请指定 /std:c++17 或更高版本编译器选项。

// basic_string_data.cpp
// compile with: /EHsc
#include <string>
#include <iostream>
int main( )
   using namespace std;
   string str1 ( "Hello world" );
   cout << "The original string object str1 is: "
        << str1 << endl;
   cout << "The length of the string object str1 = "
        << str1.length ( ) << endl << endl;
   // Converting a string to an array of characters
   const char *ptr1 = 0;
   ptr1= str1.data ( );
   cout << "The modified string object ptr1 is: " << ptr1
        << endl;
   cout << "The length of character array str1 = "
        << strlen ( ptr1) << endl << endl;
   // Converting a string to a C-style string
   const char *c_str1 = str1.c_str ( );
   cout << "The C-style string c_str1 is: " << c_str1
        << endl;
   cout << "The length of C-style string str1 = "
        << strlen ( c_str1) << endl << endl;
The original string object str1 is: Hello world
The length of the string object str1 = 11
The modified string object ptr1 is: Hello world
The length of character array str1 = 11
The C-style string c_str1 is: Hello world
The length of C-style string str1 = 11

basic_string::difference_type

提供引用同一字符串中的元素的两个迭代器之间的差异的类型。

typedef typename allocator_type::difference_type difference_type;

带符号的整数类型描述一个可表示受控序列中任意两个元素的地址之间的差异的对象。

对于类型 string,它等效于 ptrdiff_t

// basic_string_diff_type.cpp
// compile with: /EHsc
#include <string>
#include <iostream>
int main( )
   using namespace std;
   string str1 ( "quintillion" );
   cout << "The original string str1 is: " << str1 << endl;
   basic_string <char>::size_type indexChFi, indexChLi;
   indexChFi = str1.find_first_of ( "i" );
   indexChLi = str1.find_last_of ( "i" );
   basic_string<char>::difference_type diffi = indexChLi - indexChFi;
   cout << "The first character i is at position: "
        << indexChFi << "." << endl;
   cout << "The last character i is at position: "
        << indexChLi << "." << endl;
   cout << "The difference is: " << diffi << "." << endl;
The original string str1 is: quintillion
The first character i is at position: 2.
The last character i is at position: 8.
The difference is: 6.

basic_string::empty

测试字符串是否包含字符。

bool empty() const;

如果字符串对象不包含字符,则为 true;如果它至少包含一个字符,则为 false

成员函数等效于 size == 0。

// basic_string_empty.cpp
// compile with: /EHsc
#include <string>
#include <iostream>
int main() {
   using namespace std;
   bool b1, b2;
   string str1 ("Hello world");
   cout << "The original string object str1 is: " << str1 << endl;
   b1 = str1.empty();
   if (b1)
      cout << "The string object str1 is empty." << endl;
      cout << "The string object str1 is not empty." << endl;
   cout << endl;
   // An example of an empty string object
   string str2;
   b2 = str2.empty();
   if (b2)
      cout << "The string object str2 is empty." << endl;
      cout << "The string object str2 is not empty." << endl;

basic_string::end

返回发现字符串中最后一个元素之后的位置的迭代器。

const_iterator end() const;
iterator end();

返回一种随机访问迭代器,用于寻址字符串中最后一个元素之后的位置。

end 通常用于测试迭代器是否已到达其字符串的末尾。 不应对 end 返回的值取消引用。

如果将 end 的返回值分配给 const_iterator,则无法修改字符串对象。 如果将 end 的返回值分配给 iterator,则可修改字符串对象。

// basic_string_end.cpp
// compile with: /EHsc
#include <string>
#include <iostream>
int main( )
   using namespace std;
   string str1 ( "No way out." ), str2;
   basic_string <char>::iterator str_Iter, str1_Iter, str2_Iter;
   basic_string <char>::const_iterator str1_cIter;
   str1_Iter = str1.end ( );
   str1_Iter--;
   str1_Iter--;
   cout << "The last character-letter of the string str1 is: " << *str1_Iter << endl;
   cout << "The full original string str1 is: " << str1 << endl;
   // end used to test when an iterator has reached the end of its string
   cout << "The string is now: ";
   for ( str_Iter = str1.begin( ); str_Iter != str1.end( ); str_Iter++ )
      cout << *str_Iter;
   cout << endl;
   // The dereferenced iterator can be used to modify a character
   *str1_Iter = 'T';
   cout << "The last character-letter of the modified str1 is now: "
        << *str1_Iter << endl;
   cout << "The modified string str1 is now: " << str1 << endl;
   // The following line would be an error because iterator is const
   // *str1_cIter = 'T';
   // For an empty string, end is equivalent to begin
   if ( str2.begin( ) == str2.end ( ) )
      cout << "The string str2 is empty." << endl;
      cout << "The stringstr2  is not empty." << endl;
The last character-letter of the string str1 is: t
The full original string str1 is: No way out.
The string is now: No way out.
The last character-letter of the modified str1 is now: T
The modified string str1 is now: No way ouT.
The string str2 is empty.

basic_string::ends_with

检查字符串是否以指定的后缀结尾。

bool ends_with(const CharType c) const noexcept;
bool ends_with(const CharType* const x) const noexcept;
bool ends_with(const basic_string_view sv) const noexcept;
要查找的单个字符后缀。

包含要查找的后缀的字符串视图。
可以传递 std::basic_string,其将转换为字符串视图。

包含要查找的后缀的以 null 结尾的字符字符串。

如果字符串以指定的后缀结尾,则为 true;否则为 false

ends_with() 是 C++20 中的新增功能。 若要使用它,请指定 /std:c++20 或更高版本编译器选项。

查看 starts_with 以检查字符串是否以指定的前缀开头。

// Requires /std:c++20 or /std:c++latest
#include <string>
#include <iostream>
int main()
    std::basic_string<char> str = "abcdefg";
    std::cout << std::boolalpha; // so booleans show as 'true'/'false'
    std::cout << str.ends_with('g') << '\n';
    std::cout << str.ends_with("eFg") << '\n';
    std::basic_string<char> str2 = "efg";
    std::cout << str.ends_with(str2);
    return 0;
false

basic_string::erase

从字符串中的指定位置删除一个或一系列元素。

iterator erase(
    iterator first,
    iterator last);
iterator erase(
    iterator iter);
basic_string<CharType, Traits, Allocator>& erase(
    size_type offset = 0,
    size_type count = npos);

first
一种迭代器,用于寻址要清除范围中的第一个元素的位置。

一种迭代器,用于寻址要清除范围中最后一个元素之后下一个元素的位置。

一种迭代器,用于寻址要清除字符串中的元素位置。

offset
要删除的字符串中的第一个字符的索引。

count
如果在字符串范围中有同样数量的以 offset 开头的元素,将删除该元素数目。

对于前两个成员函数,是用于寻址由成员函数删除的最后一个字符后的第一个字符的迭代器。 对于第三个成员函数,是对已清除元素的字符串对象的引用。

第三个成员函数返回 *this

// basic_string_erase.cpp
// compile with: /EHsc
#include <string>
#include <iostream>
int main( )
   using namespace std;
   // The 1st member function using a range demarcated
   // by iterators
   string str1 ( "Hello world" );
   basic_string <char>::iterator str1_Iter;
   cout << "The original string object str1 is: "
        << str1 << "." << endl;
   str1_Iter = str1.erase ( str1.begin ( ) + 3 , str1.end ( ) - 1 );
   cout << "The first element after those removed is: "
        << *str1_Iter << "." << endl;
   cout << "The modified string object str1 is: " << str1
           << "." << endl << endl;
   // The 2nd member function erasing a char pointed to
   // by an iterator
   string str2 ( "Hello World" );
   basic_string <char>::iterator str2_Iter;
   cout << "The original string object str2 is: " << str2
        << "." << endl;
   str2_Iter = str2.erase ( str2.begin ( ) + 5 );
   cout << "The first element after those removed is: "
        << *str2_Iter << "." << endl;
   cout << "The modified string object str2 is: " << str2
        << "." << endl << endl;
   // The 3rd member function erasing a number of chars
   // after a char
   string str3 ( "Hello computer" ), str3m;
   basic_string <char>::iterator str3_Iter;
   cout << "The original string object str3 is: "
        << str3 << "." << endl;
   str3m = str3.erase ( 6 , 8 );
   cout << "The modified string object str3m is: "
        << str3m << "." << endl;
The original string object str1 is: Hello world.
The first element after those removed is: d.
The modified string object str1 is: Held.
The original string object str2 is: Hello World.
The first element after those removed is: W.
The modified string object str2 is: HelloWorld.
The original string object str3 is: Hello computer.
The modified string object str3m is: Hello .

basic_string::find

向前搜索字符串,搜索与指定字符序列匹配的第一个子字符串。

size_type find(
    value_type char_value,
    size_type offset = 0) const;
size_type find(
    const value_type* ptr,
    size_type offset = 0) const;
size_type find(
    const value_type* ptr,
    size_type offset,
    size_type count) const;
size_type find(
    const basic_string<CharType, Traits, Allocator>& str,
    size_type offset = 0) const;

char_value
成员函数要搜索的字符值。

offset
搜索开始处的索引。

成员函数要搜索的 C 字符串。

count
在成员函数要搜索的 C 字符串中从第一个字符开始计数的字符数。

成员函数要搜索的字符串。

搜索成功时,则为搜索的子字符串的首个字符的索引;否则为 npos

// basic_string_find.cpp
// compile with: /EHsc
#include <string>
#include <iostream>
int main( )
   using namespace std;
   // The first member function
   // searches for a single character in a string
   string str1 ( "Hello Everyone" );
   cout << "The original string str1 is: " << str1 << endl;
   basic_string <char>::size_type indexCh1a, indexCh1b;
   indexCh1a = str1.find ( "e" , 3 );
   if (indexCh1a != string::npos )
      cout << "The index of the 1st 'e' found after the 3rd"
           << " position in str1 is: " << indexCh1a << endl;
      cout << "The character 'e' was not found in str1 ." << endl;
   indexCh1b = str1.find ( "x" );
   if (indexCh1b != string::npos )
      cout << "The index of the 'x' found in str1 is: "
           << indexCh1b << endl << endl;
      cout << "The Character 'x' was not found in str1."
           << endl << endl;
   // The second member function searches a string
   // for a substring as specified by a C-string
   string str2 ( "Let me make this perfectly clear." );
   cout << "The original string str2 is: " << str2 << endl;
   basic_string <char>::size_type indexCh2a, indexCh2b;
   const char *cstr2 = "perfect";
   indexCh2a = str2.find ( cstr2 , 5 );
   if ( indexCh2a != string::npos )
      cout << "The index of the 1st element of 'perfect' "
           << "after\n the 5th position in str2 is: "
           << indexCh2a << endl;
      cout << "The substring 'perfect' was not found in str2 ."
           << endl;
   const char *cstr2b = "imperfectly";
   indexCh2b = str2.find ( cstr2b , 0 );
   if (indexCh2b != string::npos )
      cout << "The index of the 1st element of 'imperfect' "
           << "after\n the 5th position in str3 is: "
           << indexCh2b << endl;
      cout << "The substring 'imperfect' was not found in str2 ."
           << endl << endl;
   // The third member function searches a string
   // for a substring as specified by a C-string
   string str3 ( "This is a sample string for this program" );
   cout << "The original string str3 is: " << str3 << endl;
   basic_string <char>::size_type indexCh3a, indexCh3b;
   const char *cstr3a = "sample";
   indexCh3a = str3.find ( cstr3a );
   if ( indexCh3a != string::npos )
      cout << "The index of the 1st element of sample "
           << "in str3 is: " << indexCh3a << endl;
      cout << "The substring 'sample' was not found in str3 ."
           << endl;
   const char *cstr3b = "for";
   indexCh3b = str3.find ( cstr3b , indexCh3a + 1 , 2 );
   if (indexCh3b != string::npos )
      cout << "The index of the next occurrence of 'for' is in "
           << "str3 begins at: " << indexCh3b << endl << endl;
      cout << "There is no next occurrence of 'for' in str3 ."
           << endl << endl;
   // The fourth member function searches a string
   // for a substring as specified by a string
   string str4 ( "clearly this perfectly unclear." );
   cout << "The original string str4 is: " << str4 << endl;
   basic_string <char>::size_type indexCh4a, indexCh4b;
   string str4a ( "clear" );
   indexCh4a = str4.find ( str4a , 5 );
   if ( indexCh4a != string::npos )
      cout << "The index of the 1st element of 'clear' "
           << "after\n the 5th position in str4 is: "
           << indexCh4a << endl;
      cout << "The substring 'clear' was not found in str4 ."
           << endl;
   string str4b ( "clear" );
   indexCh4b = str4.find ( str4b );
   if (indexCh4b != string::npos )
      cout << "The index of the 1st element of 'clear' "
           << "in str4 is: "
           << indexCh4b << endl;
      cout << "The substring 'clear' was not found in str4 ."
           << endl << endl;
The original string str1 is: Hello Everyone
The index of the 1st 'e' found after the 3rd position in str1 is: 8
The Character 'x' was not found in str1.
The original string str2 is: Let me make this perfectly clear.
The index of the 1st element of 'perfect' after
the 5th position in str2 is: 17
The substring 'imperfect' was not found in str2 .
The original string str3 is: This is a sample string for this program
The index of the 1st element of sample in str3 is: 10
The index of the next occurrence of 'for' is in str3 begins at: 24
The original string str4 is: clearly this perfectly unclear.
The index of the 1st element of 'clear' after
the 5th position in str4 is: 25
The index of the 1st element of 'clear' in str4 is: 0

basic_string::find_first_not_of

在字符串中搜索不属于指定字符串中元素的第一个字符。

size_type find_first_not_of(
    value_type char_value,
    size_type offset = 0) const;
size_type find_first_not_of(
    const value_type* ptr,
    size_type offset = 0) const;
size_type find_first_not_of(
    const value_type* ptr,
    size_type offset,
    size_type count) const;
size_type find_first_not_of(
    const basic_string<CharType, Traits, Allocator>& str,
    size_type offset = 0) const;

char_value
成员函数要搜索的字符值。

offset
搜索开始处的索引。

成员函数要搜索的 C 字符串。

count
在成员函数要搜索的 C 字符串中从第一个字符开始计数的字符数。

成员函数要搜索的字符串。

搜索成功时,则为搜索的子字符串的首个字符的索引;否则为 npos

// basic_string_find_first_not_of.cpp
// compile with: /EHsc
#include <string>
#include <iostream>
int main( )
   using namespace std;
   // The first member function
   // searches for a single character in a string
   string str1 ( "xddd-1234-abcd" );
   cout << "The original string str1 is: " << str1 << endl;
   basic_string <char>::size_type indexCh1a, indexCh1b;
   static const basic_string <char>::size_type npos = -1;
   indexCh1a = str1.find_first_not_of ( "d" , 2 );
   if ( indexCh1a != npos )
      cout << "The index of the 1st 'd' found after the 3rd"
           << " position in str1 is: " << indexCh1a << endl;
      cout << "The character 'd' was not found in str1 ." << endl;
   indexCh1b = str1.find_first_not_of  ( "x" );
   if (indexCh1b != npos )
      cout << "The index of the 'non x' found in str1 is: "
           << indexCh1b << endl << endl;
      cout << "The character 'non x' was not found in str1."
           << endl << endl;
   // The second member function searches a string
   // for a substring as specified by a C-string
   string str2 ( "BBB-1111" );
   cout << "The original string str2 is: " << str2 << endl;
   basic_string <char>::size_type indexCh2a, indexCh2b;
   const char *cstr2 = "B1";
   indexCh2a = str2.find_first_not_of ( cstr2 , 6 );
   if ( indexCh2a != npos )
      cout << "The index of the 1st occurrence of an "
           << "element of 'B1' in str2 after\n the 6th "
           << "position is: " << indexCh2a << endl;
      cout << "Elements of the substring 'B1' were not"
           << "\n found in str2 after the 6th position."
           << endl;
   const char *cstr2b = "B2";
   indexCh2b = str2.find_first_not_of ( cstr2b );
   if ( indexCh2b != npos )
      cout << "The index of the 1st element of 'B2' "
           << "after\n the 0th position in str2 is: "
           << indexCh2b << endl << endl;
      cout << "The substring 'B2' was not found in str2 ."
           << endl << endl << endl;
   // The third member function searches a string
   // for a substring as specified by a C-string
   string str3 ( "444-555-GGG" );
   cout << "The original string str3 is: " << str3 << endl;
   basic_string <char>::size_type indexCh3a, indexCh3b;
   const char *cstr3a = "45G";
   indexCh3a = str3.find_first_not_of ( cstr3a );
   if ( indexCh3a != npos )
      cout << "The index of the 1st occurrence of an "
           << "element in str3\n other than one of the "
           << "characters in '45G' is: " << indexCh3a
           << endl;
      cout << "Elements in str3 contain only characters "
           << " in the string '45G'. "
           << endl;
   const char *cstr3b = "45G";
   indexCh3b = str3.find_first_not_of ( cstr3b , indexCh3a + 1 , 2 );
   if ( indexCh3b != npos )
      cout << "The index of the second occurrence of an "
           << "element of '45G' in str3\n after the 0th "
           << "position is: " << indexCh3b << endl << endl;
      cout << "Elements in str3 contain only characters "
           << " in the string  '45G'. "
           << endl  << endl;
   // The fourth member function searches a string
   // for a substring as specified by a string
   string str4 ( "12-ab-12-ab" );
   cout << "The original string str4 is: " << str4 << endl;
   basic_string <char>::size_type indexCh4a, indexCh4b;
   string str4a ( "ba3" );
   indexCh4a = str4.find_first_not_of ( str4a , 5 );
   if (indexCh4a != npos )
      cout << "The index of the 1st non occurrence of an "
           << "element of 'ba3' in str4 after\n the 5th "
           << "position is: " << indexCh4a << endl;
      cout << "Elements other than those in the substring"
           << " 'ba3' were not found in the string str4."
           << endl;
   string str4b ( "12" );
   indexCh4b = str4.find_first_not_of ( str4b  );
   if (indexCh4b != npos )
      cout << "The index of the 1st non occurrence of an "
           << "element of '12' in str4 after\n the 0th "
           << "position is: " << indexCh4b << endl;
      cout << "Elements other than those in the substring"
           << " '12' were not found in the string str4."
           << endl;
The original string str1 is: xddd-1234-abcd
The index of the 1st 'd' found after the 3rd position in str1 is: 4
The index of the 'non x' found in str1 is: 1
The original string str2 is: BBB-1111
Elements of the substring 'B1' were not
found in str2 after the 6th position.
The index of the 1st element of 'B2' after
the 0th position in str2 is: 3
The original string str3 is: 444-555-GGG
The index of the 1st occurrence of an element in str3
other than one of the characters in '45G' is: 3
The index of the second occurrence of an element of '45G' in str3
after the 0th position is: 7
The original string str4 is: 12-ab-12-ab
The index of the 1st non occurrence of an element of 'ba3' in str4 after
the 5th position is: 5
The index of the 1st non occurrence of an element of '12' in str4 after
the 0th position is: 2

basic_string::find_first_of

在字符串中搜索与指定字符串中任何元素匹配的第一个字符。

size_type find_first_of(
    value_type char_value,
    size_type offset = 0) const;
size_type find_first_of(
    const value_type* ptr,
    size_type offset = 0) const;
size_type find_first_of(
    const value_type* ptr,
    size_type offset,
    size_type count) const;
size_type find_first_of(
    const basic_string<CharType, Traits, Allocator>& str,
    size_type offset = 0) const;

char_value
成员函数要搜索的字符值。

offset
搜索开始处的索引。

成员函数要搜索的 C 字符串。

count
在成员函数要搜索的 C 字符串中从第一个字符开始计数的字符数。

成员函数要搜索的字符串。

搜索成功时,则为搜索的子字符串的首个字符的索引;否则为 npos

// basic_string_find_first_of.cpp
// compile with: /EHsc
#include <string>
#include <iostream>
int main( )
   using namespace std;
   // The first member function
   // searches for a single character in a string
   string str1 ( "abcd-1234-abcd-1234" );
   cout << "The original string str1 is: " << str1 << endl;
   basic_string <char>::size_type indexCh1a, indexCh1b;
   static const basic_string <char>::size_type npos = -1;
   indexCh1a = str1.find_first_of ( "d" , 5 );
   if ( indexCh1a != npos )
      cout << "The index of the 1st 'd' found after the 5th"
           << " position in str1 is: " << indexCh1a << endl;
      cout << "The character 'd' was not found in str1 ." << endl;
   indexCh1b = str1.find_first_of ( "x" );
   if ( indexCh1b != npos )
      cout << "The index of the 'x' found in str1 is: "
           << indexCh1b << endl << endl;
      cout << "The character 'x' was not found in str1."
           << endl << endl;
   // The second member function searches a string
   // for any element of a substring as specified by a C-string
   string str2 ( "ABCD-1234-ABCD-1234" );
   cout << "The original string str2 is: " << str2 << endl;
   basic_string <char>::size_type indexCh2a, indexCh2b;
   const char *cstr2 = "B1";
   indexCh2a = str2.find_first_of ( cstr2 , 6 );
   if ( indexCh2a != npos )
      cout << "The index of the 1st occurrence of an "
           << "element of 'B1' in str2 after\n the 6th "
           << "position is: " << indexCh2a << endl;
      cout << "Elements of the substring 'B1' were not "
           << "found in str2 after the 10th position."
           << endl;
   const char *cstr2b = "D2";
   indexCh2b = str2.find_first_of ( cstr2b );
   if ( indexCh2b != npos )
      cout << "The index of the 1st element of 'D2' "
           << "after\n the 0th position in str2 is: "
           << indexCh2b << endl << endl;
      cout << "The substring 'D2' was not found in str2 ."
           << endl << endl << endl;
   // The third member function searches a string
   // for any element of a substring as specified by a C-string
   string str3 ( "123-abc-123-abc-456-EFG-456-EFG" );
   cout << "The original string str3 is: " << str3 << endl;
   basic_string <char>::size_type indexCh3a, indexCh3b;
   const char *cstr3a = "5G";
   indexCh3a = str3.find_first_of ( cstr3a );
   if ( indexCh3a != npos )
      cout << "The index of the 1st occurrence of an "
           << "element of '5G' in str3 after\n the 0th "
           << "position is: " << indexCh3a << endl;
      cout << "Elements of the substring '5G' were not "
           << "found in str3\n after the 0th position."
           << endl;
   const char *cstr3b = "5GF";
   indexCh3b = str3.find_first_of  ( cstr3b , indexCh3a + 1 , 2 );
   if (indexCh3b != npos )
      cout << "The index of the second occurrence of an "
           << "element of '5G' in str3\n after the 0th "
           << "position is: " << indexCh3b << endl << endl;
      cout << "Elements of the substring '5G' were not "
           << "found in str3\n after the first occurrrence."
           << endl << endl;
   // The fourth member function searches a string
   // for any element of a substring as specified by a string
   string str4 ( "12-ab-12-ab" );
   cout << "The original string str4 is: " << str4 << endl;
   basic_string <char>::size_type indexCh4a, indexCh4b;
   string str4a ( "ba3" );
   indexCh4a = str4.find_first_of ( str4a , 5 );
   if ( indexCh4a != npos )
      cout << "The index of the 1st occurrence of an "
           << "element of 'ba3' in str4 after\n the 5th "
           << "position is: " << indexCh4a << endl;
      cout << "Elements of the substring 'ba3' were not "
           << "found in str4\n after the 0th position."
           << endl;
   string str4b ( "a2" );
   indexCh4b = str4.find_first_of ( str4b );
   if ( indexCh4b != npos )
      cout << "The index of the 1st occurrence of an "
           << "element of 'a2' in str4 after\n the 0th "
           << "position is: " << indexCh4b << endl;
      cout << "Elements of the substring 'a2' were not "
           << "found in str4\n after the 0th position."
           << endl;
The original string str1 is: abcd-1234-abcd-1234
The index of the 1st 'd' found after the 5th position in str1 is: 13
The character 'x' was not found in str1.
The original string str2 is: ABCD-1234-ABCD-1234
The index of the 1st occurrence of an element of 'B1' in str2 after
the 6th position is: 11
The index of the 1st element of 'D2' after
the 0th position in str2 is: 3
The original string str3 is: 123-abc-123-abc-456-EFG-456-EFG
The index of the 1st occurrence of an element of '5G' in str3 after
the 0th position is: 17
The index of the second occurrence of an element of '5G' in str3
after the 0th position is: 22
The original string str4 is: 12-ab-12-ab
The index of the 1st occurrence of an element of 'ba3' in str4 after
the 5th position is: 9
The index of the 1st occurrence of an element of 'a2' in str4 after
the 0th position is: 1

basic_string::find_last_not_of

在字符串中搜索不属于指定字符串中任何元素的最后一个字符。

size_type find_last_not_of(
    value_type char_value,
    size_type offset = npos) const;
size_type find_last_not_of(
    const value_type* ptr,
    size_type offset = npos) const;
size_type find_last_not_of(
    const value_type* ptr,
    size_type offset,
    size_type count) const;
size_type find_last_not_of(
    const basic_string<CharType, Traits, Allocator>& str,
    size_type offset = npos) const;

char_value
成员函数要搜索的字符值。

offset
搜索结束位置的索引。

成员函数要搜索的 C 字符串。

count
在成员函数要搜索的 C 字符串中从第一个字符开始计数的字符数。

成员函数要搜索的字符串。

搜索成功时,则为搜索的子字符串的首个字符的索引;否则为 npos

// basic_string_find_last_not_of.cpp
// compile with: /EHsc
#include <string>
#include <iostream>
int main( )
   using namespace std;
   // The first member function
   // searches for a single character in a string
   string str1 ( "dddd-1dd4-abdd" );
   cout << "The original string str1 is: " << str1 << endl;
   basic_string <char>::size_type indexCh1a, indexCh1b;
   static const basic_string <char>::size_type npos = -1;
   indexCh1a = str1.find_last_not_of ( "d" , 7 );
   if ( indexCh1a != npos )
      cout << "The index of the last non 'd'\n found before the "
           << "7th position in str1 is: " << indexCh1a << endl;
      cout << "The non 'd' character was not found ." << endl;
   indexCh1b = str1.find_last_not_of  ( "d" );
   if ( indexCh1b != npos )
      cout << "The index of the non 'd' found in str1 is: "
           << indexCh1b << endl << endl;
      cout << "The Character 'non x' was not found in str1."
           << endl << endl;
   // The second member function searches a string
   // for a substring as specified by a C-string
   string str2 ( "BBB-1111" );
   cout << "The original string str2 is: " << str2 << endl;
   basic_string <char>::size_type indexCh2a, indexCh2b;
   const char *cstr2 = "B1";
   indexCh2a = str2.find_last_not_of  ( cstr2 , 6 );
   if ( indexCh2a != npos )
      cout << "The index of the last occurrence of a "
           << "element\n not of 'B1' in str2 before the 6th "
           << "position is: " << indexCh2a << endl;
      cout << "Elements not of the substring 'B1' were not "
           << "\n found in str2 before the 6th position."
           << endl;
   const char *cstr2b = "B-1";
   indexCh2b = str2.find_last_not_of  ( cstr2b );
   if ( indexCh2b != npos )
      cout << "The index of the last element not "
           << "in 'B-1'\n is: "
           << indexCh2b << endl << endl;
      cout << "The elements of the substring 'B-1' were "
           << "not found in str2 ."
           << endl << endl;
   // The third member function searches a string
   // for a substring as specified by a C-string
   string str3 ( "444-555-GGG" );
   cout << "The original string str3 is: " << str3 << endl;
   basic_string <char>::size_type indexCh3a, indexCh3b;
   const char *cstr3a = "45G";
   indexCh3a = str3.find_last_not_of ( cstr3a );
   if ( indexCh3a != npos )
      cout << "The index of the last occurrence of an "
           << "element in str3\n other than one of the "
           << "characters in '45G' is: " << indexCh3a
           << endl;
      cout << "Elements in str3 contain only characters "
           << " in the string  '45G'. "
           << endl;
   const char *cstr3b = "45G";
   indexCh3b = str3.find_last_not_of ( cstr3b , 6 , indexCh3a - 1 );
   if (indexCh3b != npos )
      cout << "The index of the penultimate occurrence of an "
           << "element\n not in '45G' in str3 is: "
           << indexCh3b << endl << endl;
      cout << "Elements in str3 contain only characters "
           << " in the string '45G'. "
           << endl  << endl;
   // The fourth member function searches a string
   // for a substring as specified by a string
   string str4 ( "12-ab-12-ab" );
   cout << "The original string str4 is: " << str4 << endl;
   basic_string <char>::size_type indexCh4a, indexCh4b;
   string str4a ( "b-a" );
   indexCh4a = str4.find_last_not_of  ( str4a , 5 );
   if ( indexCh4a != npos )
      cout << "The index of the last occurrence of an "
           << "element not\n in 'b-a' in str4 before the 5th "
           << "position is: " << indexCh4a << endl;
      cout << "Elements other than those in the substring"
           << " 'b-a' were not found in the string str4."
           << endl;
   string str4b ( "12" );
   indexCh4b = str4.find_last_not_of ( str4b  );
   if ( indexCh4b != npos )
      cout << "The index of the last occurrence of an "
           << "element not in '12'\n in str4 before the end "
           << "position is: " << indexCh4b << endl;
      cout << "Elements other than those in the substring"
           << " '12'\n were not found in the string str4."
           << endl;
The original string str1 is: dddd-1dd4-abdd
The index of the last non 'd'
found before the 7th position in str1 is: 5
The index of the non 'd' found in str1 is: 11
The original string str2 is: BBB-1111
The index of the last occurrence of a element
not of 'B1' in str2 before the 6th position is: 3
The elements of the substring 'B-1' were not found in str2 .
The original string str3 is: 444-555-GGG
The index of the last occurrence of an element in str3
other than one of the characters in '45G' is: 7
The index of the penultimate occurrence of an element
not in '45G' in str3 is: 3
The original string str4 is: 12-ab-12-ab
The index of the last occurrence of an element not
in 'b-a' in str4 before the 5th position is: 1
The index of the last occurrence of an element not in '12'
in str4 before the end position is: 10

basic_string::find_last_of

在字符串中搜索与指定字符串中任何元素匹配的最后一个字符。

size_type find_last_of(
    value_type char_value,
    size_type offset = npos) const;
size_type find_last_of(
    const value_type* ptr,
    size_type offset = npos) const;
size_type find_last_of(
    const value_type* ptr,
    size_type offset,
    size_type count) const;
size_type find_last_of(
    const basic_string<CharType, Traits, Allocator>& str,
    size_type offset = npos) const;

char_value
成员函数要搜索的字符值。

offset
搜索结束位置的索引。

成员函数要搜索的 C 字符串。

count
在成员函数要搜索的 C 字符串中从第一个字符开始计数的字符数。

成员函数要搜索的字符串。

搜索成功时,则为搜索的子字符串的最后一个字符的索引;否则为 npos

// basic_string_find_last_of.cpp
// compile with: /EHsc
#include <string>
#include <iostream>
int main( )
   using namespace std;
   // The first member function
   // searches for a single character in a string
   string str1 ( "abcd-1234-abcd-1234" );
   cout << "The original string str1 is: " << str1 << endl;
   basic_string <char>::size_type indexCh1a, indexCh1b;
   static const basic_string <char>::size_type npos = -1;
   indexCh1a = str1.find_last_of ( "d" , 14 );
   if ( indexCh1a != npos )
      cout << "The index of the last 'd' found before the 14th"
           << " position in str1 is: " << indexCh1a << endl;
      cout << "The character 'd' was not found in str1 ." << endl;
   indexCh1b = str1.find_first_of ( "x" );
   if ( indexCh1b != npos )
      cout << "The index of the 'x' found in str1 is: "
           << indexCh1b << endl << endl;
      cout << "The character 'x' was not found in str1."
           << endl << endl;
   // The second member function searches a string
   // for a substring as specified by a C-string
   string str2 ( "ABCD-1234-ABCD-1234" );
   cout << "The original string str2 is: " << str2 << endl;
   basic_string <char>::size_type indexCh2a, indexCh2b;
   const char *cstr2 = "B1";
   indexCh2a = str2.find_last_of  ( cstr2 , 12 );
   if (indexCh2a != npos )
      cout << "The index of the last occurrence of an "
           << "element of 'B1' in str2 before\n the 12th "
           << "position is: " << indexCh2a << endl;
      cout << "Elements of the substring 'B1' were not "
           << "found in str2 before the 12th position."
           << endl;
   const char *cstr2b = "D2";
   indexCh2b = str2.find_last_of  ( cstr2b );
   if ( indexCh2b != npos )
      cout << "The index of the last element of 'D2' "
           << "after\n the 0th position in str2 is: "
           << indexCh2b << endl << endl;
      cout << "The substring 'D2' was not found in str2 ."
           << endl << endl << endl;
   // The third member function searches a string
   // for a substring as specified by a C-string
   string str3 ( "456-EFG-456-EFG" );
   cout << "The original string str3 is: " << str3 << endl;
   basic_string <char>::size_type indexCh3a;
   const char *cstr3a = "5E";
   indexCh3a = str3.find_last_of ( cstr3a , 8 , 8 );
   if ( indexCh3a != npos )
      cout << "The index of the last occurrence of an "
           << "element of '5E' in str3 before\n the 8th "
           << "position is: " << indexCh3a << endl << endl;
      cout << "Elements of the substring '5G' were not "
           << "found in str3\n before the 8th position."
           << endl << endl;
   // The fourth member function searches a string
   // for a substring as specified by a string
   string str4 ( "12-ab-12-ab" );
   cout << "The original string str4 is: " << str4 << endl;
   basic_string <char>::size_type indexCh4a, indexCh4b;
   string str4a ( "ba3" );
   indexCh4a = str4.find_last_of  ( str4a , 8 );
   if ( indexCh4a != npos )
      cout << "The index of the last occurrence of an "
           << "element of 'ba3' in str4 before\n the 8th "
           << "position is: " << indexCh4a << endl;
      cout << "Elements of the substring 'ba3' were not "
           << "found in str4\n after the 0th position."
           << endl;
   string str4b ( "a2" );
   indexCh4b = str4.find_last_of ( str4b  );
   if ( indexCh4b != npos )
      cout << "The index of the last occurrence of an "
           << "element of 'a2' in str4 before\n the 0th "
           << "position is: " << indexCh4b << endl;
      cout << "Elements of the substring 'a2' were not "
           << "found in str4\n after the 0th position."
           << endl;
The original string str1 is: abcd-1234-abcd-1234
The index of the last 'd' found before the 14th position in str1 is: 13
The character 'x' was not found in str1.
The original string str2 is: ABCD-1234-ABCD-1234
The index of the last occurrence of an element of 'B1' in str2 before
the 12th position is: 11
The index of the last element of 'D2' after
the 0th position in str2 is: 16
The original string str3 is: 456-EFG-456-EFG
The index of the last occurrence of an element of '5E' in str3 before
the 8th position is: 4
The original string str4 is: 12-ab-12-ab
The index of the last occurrence of an element of 'ba3' in str4 before
the 8th position is: 4
The index of the last occurrence of an element of 'a2' in str4 before
the 0th position is: 9

basic_string::front

返回对字符串中第一个元素的引用。

const_reference front() const;
reference front();

对字符串的第一个元素的引用,必须为非空值。

basic_string::get_allocator

返回用于构造字符串的分配器对象的一个副本。

allocator_type get_allocator() const;

字符串使用的分配器。

该成员函数将返回存储的分配器对象。

字符串类的分配器指定类管理存储的方式。 容器类提供的默认分配器足以满足大多编程需求。 编写和使用你自己的分配器类是高级 C++ 功能。

// basic_string_get_allocator.cpp
// compile with: /EHsc
#include <string>
#include <iostream>
int main( )
   using namespace std;
   // The following lines declare objects
   // that use the default allocator.
   string s1;
   basic_string <char> s2;
   basic_string <char, char_traits< char >, allocator< char > > s3;
   // s4 will use the same allocator class as s1
   basic_string <char> s4( s1.get_allocator ( ) );
   basic_string <char>::allocator_type xchar = s1.get_allocator( );
   // You can now call functions on the allocator class xchar used by s1

basic_string::insert

将一个、多个或一系列元素插入到指定位置的字符串中。

basic_string<CharType, Traits, Allocator>& insert(
    size_type position,
    const value_type* ptr);
basic_string<CharType, Traits, Allocator>& insert(
    size_type position,
    const value_type* ptr,
    size_type count);
basic_string<CharType, Traits, Allocator>& insert(
    size_type position,
    const basic_string<CharType, Traits, Allocator>& str);
basic_string<CharType, Traits, Allocator>& insert(
    size_type position,
    const basic_string<CharType, Traits, Allocator>& str,
    size_type offset,
    size_type count);
basic_string<CharType, Traits, Allocator>& insert(
    size_type position,
    size_type count,
    value_type char_value);
iterator insert(
    iterator iter);
iterator insert(
    iterator iter,
    value_type char_value)l
template <class InputIterator>
void insert(
    iterator iter,
    InputIterator first,
    InputIterator last);
void insert(
    iterator iter,
    size_type count,
    value_type char_value);
void insert(
    iterator iter,
    const_pointer first,
    const_pointer last);
void insert(
    iterator iter,
    const_iterator first,
    const_iterator last);

position
新字符插入点之后的位置的索引。

将要完全或部分插入到字符串中的 C 字符串。

count
要插入的字符数。

将要完全或部分插入到目标字符串中的字符串。

offset
提供要追加的字符的源字符串部分的索引。

char_value
要插入的元素的字符值。

对要在其后插入一个字符的位置进行寻址的迭代器。

first
一种输入迭代器(const_pointerconst_iterator),用于寻址要插入的源范围中的第一个元素。

一种输入迭代器(const_pointerconst_iterator),用于寻址要插入的源范围中超出最后一个元素的元素位置。

返回值的函数将返回以下任一值:

  • 对包含原始字符串和新字符的 basic_string 的引用。
  • 位于已插入字符开头的迭代器。
  • // basic_string_insert.cpp
    // compile with: /EHsc
    #include <string>
    #include <iostream>
    int main( )
       using namespace std;
       // The first member function inserting a C-string
       // at a given position
       basic_string <char> str1a ( "way" );
       const char *cstr1a = "a";
       str1a.insert ( 0, cstr1a );
       cout << "The string with a C-string inserted at position 0 is: "
            << str1a << "." << endl;
       // The second member function inserting a C-string
       // at a given position for a specified number of elements
       basic_string <char> str2a ( "Good" );
       const char *cstr2a = "Bye Bye Baby";
       str2a.insert ( 4, cstr2a ,3 );
       cout << "The string with a C-string inserted at the end is: "
            << str2a << "." << endl;
       // The third member function inserting a string
       // at a given position
       basic_string <char> str3a ( "Bye" );
       string str3b ( "Good" );
       str3a.insert ( 0, str3b );
       cout << "The string with a string inserted at position 0 is: "
            << str3a << "." << endl;
       // The fourth member function inserting part of
       // a string at a given position
       basic_string <char> str4a ( "Good " );
       string str4b ( "Bye Bye Baby" );
       str4a.insert ( 5, str4b , 8 , 4 );
       cout << "The string with part of a string inserted at position 4 is: "
            << str4a << "." << endl;
       // The fifth member function inserts a number of characters
       // at a specified position in the string
       string str5 ( "The number is: ." );
       str5.insert ( 15 , 3 , '3' );
       cout << "The string with characters inserted is: "
            << str5 << endl;
       // The sixth member function inserts a character
       // at a specified position in the string
       string str6 ( "ABCDFG" );
       basic_string <char>::iterator str6_Iter = ( str6.begin ( ) + 4 );
       str6.insert ( str6_Iter , 'e' );
       cout << "The string with a character inserted is: "
            << str6 << endl;
       // The seventh member function inserts a range
       // at a specified position in the string
       string str7a ( "ABCDHIJ" );
       string str7b ( "abcdefgh" );
       basic_string <char>::iterator str7a_Iter = (str7a.begin ( ) + 4 );
       str7a.insert ( str7a_Iter , str7b.begin ( ) + 4 , str7b.end ( ) -1 );
       cout << "The string with a character inserted from a range is: "
            << str7a << endl;
       // The eighth member function inserts a number of
       // characters at a specified position in the string
       string str8 ( "ABCDHIJ" );
       basic_string <char>::iterator str8_Iter = ( str8.begin ( ) + 4 );
       str8.insert ( str8_Iter , 3 , 'e' );
       cout << "The string with a character inserted from a range is: "
            << str8 << endl;
    
    The string with a C-string inserted at position 0 is: away.
    The string with a C-string inserted at the end is: GoodBye.
    The string with a string inserted at position 0 is: GoodBye.
    The string with part of a string inserted at position 4 is: Good Baby.
    The string with characters inserted is: The number is: 333.
    The string with a character inserted is: ABCDeFG
    The string with a character inserted from a range is: ABCDefgHIJ
    The string with a character inserted from a range is: ABCDeeeHIJ
    

    basic_string::iterator

    提供可访问和读取字符串中 const 元素的随机访问迭代器的类型。

    typedef implementation-defined iterator;
    

    类型 iterator 可用于修改字符值并用于正向循环访问字符串。

    有关如何声明和使用 iterator 的示例,请参阅 begin 的示例。

    basic_string::length

    返回字符串中元素的当前数目。

    size_type length() const;
    

    成员函数与 size 相同。

    // basic_string_length.cpp
    // compile with: /EHsc
    #include <string>
    #include <iostream>
    int main( )
       using namespace std;
       string str1 ("Hello world");
       cout << "The original string str1 is: " << str1 << endl;
       // The size and length member functions differ in name only
       basic_string <char>::size_type sizeStr1, lenStr1;
       sizeStr1 = str1.size ( );
       lenStr1 = str1.length ( );
       basic_string <char>::size_type capStr1, max_sizeStr1;
       capStr1 = str1.capacity ( );
       max_sizeStr1 = str1.max_size ( );
       // Compare size, length, capacity & max_size of a string
       cout << "The current size of original string str1 is: "
            << sizeStr1 << "." << endl;
       cout << "The current length of original string str1 is: "
            << lenStr1 << "." << endl;
       cout << "The capacity of original string str1 is: "
            << capStr1 << "." << endl;
       cout << "The max_size of original string str1 is: "
            << max_sizeStr1 << "." << endl << endl;
       str1.erase ( 6, 5 );
       cout << "The modified string str1 is: " << str1 << endl;
       sizeStr1 = str1.size ( );
       lenStr1 = str1.length ( );
       capStr1 = str1.capacity ( );
       max_sizeStr1 = str1.max_size ( );
       // Compare size, length, capacity & max_size of a string
       // after erasing part of the original string
       cout << "The current size of modified string str1 is: "
            << sizeStr1 << "." << endl;
       cout << "The current length of modified string str1 is: "
            << lenStr1 << "." << endl;
       cout << "The capacity of modified string str1 is: "
            << capStr1 << "." << endl;
       cout << "The max_size of modified string str1 is: "
            << max_sizeStr1 << "." << endl;
    

    basic_string::max_size

    返回字符串可包含的字符的最大数目。

    size_type max_size() const;
    

    字符串可包含的字符的最大数目。

    当运算生成长度大于最大大小的字符串时,引发 length_error类型异常。

    // basic_string_max_size.cpp
    // compile with: /EHsc
    #include <string>
    #include <iostream>
    int main( )
       using namespace std;
       string str1 ("Hello world");
       cout << "The original string str1 is: " << str1 << endl;
       // The size and length member functions differ in name only
       basic_string <char>::size_type sizeStr1, lenStr1;
       sizeStr1 = str1.size ( );
       lenStr1 = str1.length ( );
       basic_string <char>::size_type capStr1, max_sizeStr1;
       capStr1 = str1.capacity ( );
       max_sizeStr1 = str1.max_size ( );
       // Compare size, length, capacity & max_size of a string
       cout << "The current size of original string str1 is: "
            << sizeStr1 << "." << endl;
       cout << "The current length of original string str1 is: "
            << lenStr1 << "." << endl;
       cout << "The capacity of original string str1 is: "
            << capStr1 << "." << endl;
       cout << "The max_size of original string str1 is: "
            << max_sizeStr1 << "." << endl << endl;
       str1.erase ( 6, 5 );
       cout << "The modified string str1 is: " << str1 << endl;
       sizeStr1 = str1.size ( );
       lenStr1 = str1.length ( );
       capStr1 = str1.capacity ( );
       max_sizeStr1 = str1.max_size ( );
       // Compare size, length, capacity & max_size of a string
       // after erasing part of the original string
       cout << "The current size of modified string str1 is: "
            << sizeStr1 << "." << endl;
       cout << "The current length of modified string str1 is: "
            << lenStr1 << "." << endl;
       cout << "The capacity of modified string str1 is: "
            << capStr1 << "." << endl;
       cout << "The max_size of modified string str1 is: "
            << max_sizeStr1 << "." << endl;
    

    basic_string::npos

    一个初始化为 -1 的无符号整数值,这个值在搜索功能失败时指示“找不到”或“所有其余字符”。

    static const size_type npos = -1;
    

    npos 值检查返回值时,只有在返回值是 size_type 类型,而不是 intunsigned 时才起作用。

    有关如何声明和使用 npos 的示例,请参阅 find 的示例。

    basic_string::operator+=

    向字符串追加字符。

    basic_string<CharType, Traits, Allocator>& operator+=(
        value_type char_value);
    basic_string<CharType, Traits, Allocator>& operator+=(
        const value_type* ptr);
    basic_string<CharType, Traits, Allocator>& operator+=(
        const basic_string<CharType, Traits, Allocator>& right);
    

    char_value
    要追加的字符。

    要追加的 C 字符串的字符。

    right
    要追加的字符串的字符。

    使用由成员函数传递的字符追加的字符串对象的引用。

    可以使用 operator+= 或成员函数 appendpush_back 将字符追加到字符串。 operator+= 追加单一参数值,而多参数 append 成员函数允许指定字符串的特定部分用于添加。

    // basic_string_op_app.cpp
    // compile with: /EHsc
    #include <string>
    #include <iostream>
    int main( )
       using namespace std;
       // The first member function
       // appending a single character to a string
       string str1a ( "Hello" );
       cout << "The original string str1 is: " << str1a << endl;
       str1a +=  '!' ;
       cout << "The string str1 appended with an exclamation is: "
            << str1a << endl << endl;
       // The second member function
       // appending a C-string to a string
       string  str1b ( "Hello " );
       const char *cstr1b = "Out There";
       cout << "The C-string cstr1b is: " << cstr1b << endl;
       str1b +=  cstr1b;
       cout << "Appending the C-string cstr1b to string str1 gives: "
            << str1b << "." << endl << endl;
       // The third member function
       // appending one string to another in two ways,
       // comparing append and operator [ ]
       string str1d ( "Hello " ), str2d ( "Wide " ), str3d ( "World" );
       cout << "The string str2d is: " << str2d << endl;
       str1d.append ( str2d );
       cout << "The appended string str1d is: "
            << str1d << "." << endl;
       str1d += str3d;
       cout << "The doubly appended strig str1 is: "
            << str1d << "." << endl << endl;
    
    The original string str1 is: Hello
    The string str1 appended with an exclamation is: Hello!
    The C-string cstr1b is: Out There
    Appending the C-string cstr1b to string str1 gives: Hello Out There.
    The string str2d is: Wide
    The appended string str1d is: Hello Wide .
    The doubly appended strig str1 is: Hello Wide World.
    

    basic_string::operator=

    对字符串的内容赋新的字符值。

    basic_string<CharType, Traits, Allocator>& operator=(
        value_type char_value);
    basic_string<CharType, Traits, Allocator>& operator=(
        const value_type* ptr);
    basic_string<CharType, Traits, Allocator>& operator=(
        const basic_string<CharType, Traits, Allocator>& right);
    basic_string<CharType, Traits, Allocator>& operator=(
        const basic_string<CharType, Traits, Allocator>&& right);
    

    char_value
    要分配的字符值。

    指向要分配给目标字符串的 C 字符串字符的指针。

    right
    要分配给目标字符串的字符的源字符串。

    由成员函数分配新字符的字符串对象的引用。

    可以为这些字符串分配了新字符值。 新值可以是字符串和 C 字符串或单一字符。 如果可由单一参数描述新值,则可使用 operator=,否则使用成员函数 assign,该函数具有多个参数,可用于指定向目标字符串分配字符串的哪个部分。

    // basic_string_op_assign.cpp
    // compile with: /EHsc
    #include <string>
    #include <iostream>
    int main( )
       using namespace std;
       // The first member function assigning a
       // character of a certain value to a string
       string str1a ( "Hello " );
       str1a = '0';
       cout << "The string str1 assigned with the zero character is: "
            << str1a << endl << endl;
       // The second member function assigning the
       // characters of a C-string to a string
       string  str1b;
       const char *cstr1b = "Out There";
       cout << "The C-string cstr1b is: " << cstr1b <<  "." << endl;
       str1b = cstr1b;
       cout << "Assigning the C-string cstr1a to string str1 gives: "
            << str1b << "." << endl << endl;
       // The third member function assigning the characters
       // from one string to another string in two equivalent
       // ways, comparing the assign and operator =
       string str1c ( "Hello" ), str2c ( "Wide" ), str3c ( "World" );
       cout << "The original string str1 is: " << str1c << "." << endl;
       cout << "The string str2c is: " << str2c << "." << endl;
       str1c.assign ( str2c );
       cout << "The string str1 newly assigned with string str2c is: "
            << str1c << "." << endl;
       cout << "The string str3c is: " << str3c << "." << endl;
       str1c = str3c;
       cout << "The string str1 reassigned with string str3c is: "
            << str1c << "." << endl << endl;
    
    The string str1 assigned with the zero character is: 0
    The C-string cstr1b is: Out There.
    Assigning the C-string cstr1a to string str1 gives: Out There.
    The original string str1 is: Hello.
    The string str2c is: Wide.
    The string str1 newly assigned with string str2c is: Wide.
    The string str3c is: World.
    The string str1 reassigned with string str3c is: World.
    

    basic_string::operator[]

    使用字符串中的指定索引提供对字符的引用。

    const_reference operator[](size_type offset) const;
    reference operator[](size_type offset);
    

    offset
    要引用的元素的位置索引。

    由参数索引指定的位置的字符串字符的引用。

    字符串的第一个元素的索引为零,其后续元素以正整数进行连续地索引。 这表明长度为 n 的字符串具有由数字 n - 1 索引的第 n个元素。

    在提供对字符串元素的读取和写入访问权限时,operator[] 比成员函数 at 更快。

    operator[] 并不检查以参数进行传递的索引是否有效,但成员函数 at 进行此项检查,因此应在不确定其有效性时使用它。 传递到成员函数 at 的无效索引(小于零或大于或等于字符串大小的索引)引发 out_of_range异常。 传递给 operator[] 的无效索引导致未定义行为,但是等于字符串长度的索引对于常量字符串而言是有效索引,且运算符在传递此索引时返回空字符。

    重新分配字符串或修改非 const 字符串可能使返回的引用无效。

    _ITERATOR_DEBUG_LEVEL 设置为 1 或 2 的情况下进行编译时,如果尝试访问字符串边界以外的元素,将发生运行时错误。 有关更多信息,请参阅经过检查的迭代器

    // basic_string_op_ref.cpp
    // compile with: /EHsc
    #include <string>
    #include <iostream>
    int main( )
       using namespace std;
       string str1 ( "Hello world" ), str2 ( "Goodbye world" );
       const string cstr1 ( "Hello there" ), cstr2 ( "Goodbye now" );
       cout << "The original string str1 is: " << str1 << endl;
       cout << "The original string str2 is: " << str2 << endl;
       // Element access to the non-const strings
       basic_string <char>::reference refStr1 = str1 [6];
       basic_string <char>::reference refStr2 = str2.at ( 3 );
       cout << "The character with an index of 6 in string str1 is: "
            << refStr1 << "." << endl;
       cout << "The character with an index of 3 in string str2 is: "
            << refStr2 << "." << endl;
       // Element access to the const strings
       basic_string <char>::const_reference crefStr1 = cstr1 [ cstr1.length ( ) ];
       basic_string <char>::const_reference crefStr2 = cstr2.at ( 8 );
       if ( crefStr1 == '\0' )
          cout << "The null character is returned as a valid reference."
               << endl;
          cout << "The null character is not returned." << endl;
       cout << "The character with index of 8 in the const string cstr2 is: "
            << crefStr2 << "." << endl;
    

    basic_string::pointer

    提供指向字符串中或字符数组中字符元素的指针的类型。

    typedef typename allocator_type::pointer pointer;
    

    类型是 allocator_type::pointer 的同义词。

    对于类型 string,它等效于 char *

    // basic_string_pointer.cpp
    // compile with: /EHsc
    #include <string>
    #include <iostream>
    int main( )
       using namespace std;
       basic_string<char>::pointer pstr1a = "In Here";
       char *cstr1b = "Out There";
       cout << "The string pstr1a is: " << pstr1a <<  "." << endl;
       cout << "The C-string cstr1b is: " << cstr1b << "." << endl;
    
    The string pstr1a is: In Here.
    The C-string cstr1b is: Out There.
    

    basic_string::pop_back

    删除字符串的最后一个元素。

    void pop_back();
    

    此成员函数可有效地调用 erase(size() - 1),以清除序列(必须为非空)的最后一个元素。

    basic_string::push_back

    在字符串的末尾处添加一个元素。

    void push_back(value_type char_value);
    

    char_value
    要添加到字符串末尾的字符。

    该成员函数有效调用 insert( end, char_value )。 有关详细信息,请参阅 insertend

    // basic_string_push_back.cpp
    // compile with: /EHsc
    #include <string>
    #include <iostream>
    int main( )
       using namespace std;
       string str1 ( "abc" );
       basic_string <char>::iterator str_Iter, str1_Iter;
       cout << "The original string str1 is: ";
       for ( str_Iter = str1.begin( ); str_Iter != str1.end( ); str_Iter++ )
          cout << *str_Iter;
       cout << endl;
       // str1.push_back ( 'd' );
       str1_Iter = str1.end ( );
       str1_Iter--;
       cout << "The last character-letter of the modified str1 is now: "
            << *str1_Iter << endl;
       cout << "The modified string str1 is: ";
       for ( str_Iter = str1.begin( ); str_Iter != str1.end( ); str_Iter++ )
          cout << *str_Iter;
       cout << endl;
    
    The original string str1 is: abc
    The last character-letter of the modified str1 is now: c
    The modified string str1 is: abc
    

    basic_string::rbegin

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

    const_reverse_iterator rbegin() const;
    reverse_iterator rbegin();
    

    返回反向字符串中第一个元素的随机访问迭代器,用于寻址相应非反向字符串中的最后一个元素。

    rbegin 与反向字符串配合使用,正如 begin 与字符串配合使用一样。

    如果将 rbegin 的返回值分配给 const_reverse_iterator,则无法修改字符串对象。 如果将 rbegin 的返回值分配给 reverse_iterator,则可修改字符串对象。

    rbegin 可用于向后初始化字符串迭代。

    // basic_string_rbegin.cpp
    // compile with: /EHsc
    #include <string>
    #include <iostream>
    int main( )
       using namespace std;
       string str1 ( "Able was I ere I saw Elba" ), str2;
       basic_string <char>::reverse_iterator str_rIter, str1_rIter, str2_rIter;
       basic_string <char>::const_reverse_iterator str1_rcIter;
       str1_rIter = str1.rbegin ( );
       // str1_rIter--;
       cout << "The first character-letter of the reversed string str1 is: "
            << *str1_rIter << endl;
       cout << "The full reversed string str1 is:\n ";
       for ( str_rIter = str1.rbegin( ); str_rIter != str1.rend( ); str_rIter++ )
          cout << *str_rIter;
       cout << endl;
       // The dereferenced iterator can be used to modify a character
       *str1_rIter = 'A';
       cout << "The first character-letter of the modified str1 is now: "
            << *str1_rIter << endl;
       cout << "The full modified reversed string str1 is now:\n ";
       for ( str_rIter = str1.rbegin( ); str_rIter != str1.rend( ); str_rIter++ )
          cout << *str_rIter;
       cout << endl;
       // The following line would be an error because iterator is const
       // *str1_rcIter = 'A';
       // For an empty string, begin is equivalent to end
       if ( str2.rbegin( ) == str2.rend ( ) )
          cout << "The string str2 is empty." << endl;
          cout << "The stringstr2  is not empty." << endl;
    
    The first character-letter of the reversed string str1 is: a
    The full reversed string str1 is:
    ablE was I ere I saw elbA
    The first character-letter of the modified str1 is now: A
    The full modified reversed string str1 is now:
    AblE was I ere I saw elbA
    The string str2 is empty.
    

    basic_string::reference

    提供对存储在字符串中的元素的引用的类型。

    typedef typename allocator_type::reference reference;
    

    reference 类型可用于修改元素的值。

    类型是 allocator_type::reference 的同义词。

    对于类型 string,它等效于 chr&

    有关如何声明和使用 reference 的示例,请参阅 at 的示例。

    basic_string::rend

    返回一种迭代器,用于寻址反向字符串中最后一个元素之后的位置。

    const_reverse_iterator rend() const;
    reverse_iterator rend();
    

    一种反向随机访问迭代器,用于寻址反向字符串中最后一个元素之后的位置。

    rend 与反向字符串配合使用,正如 end 与字符串配合使用一样。

    如果将 rend 的返回值分配给 const_reverse_iterator,则无法修改字符串对象。 如果将 rend 的返回值分配给 reverse_iterator,则可修改字符串对象。

    rend 可用于测试反向迭代器是否已到达其字符串末尾。

    不应对 rend 返回的值取消引用。

    // basic_string_rend.cpp
    // compile with: /EHsc
    #include <string>
    #include <iostream>
    int main( )
       using namespace std;
       string str1 ("Able was I ere I saw Elba"), str2;
       basic_string <char>::reverse_iterator str_rIter, str1_rIter, str2_rIter;
       basic_string <char>::const_reverse_iterator str1_rcIter;
       str1_rIter = str1.rend ( );
       str1_rIter--;
       cout << "The last character-letter of the reversed string str1 is: "
            << *str1_rIter << endl;
       cout << "The full reversed string str1 is:\n ";
       for ( str_rIter = str1.rbegin( ); str_rIter != str1.rend( ); str_rIter++ )
          cout << *str_rIter;
       cout << endl;
       // The dereferenced iterator can be used to modify a character
       *str1_rIter = 'o';
       cout << "The last character-letter of the modified str1 is now: "
            << *str1_rIter << endl;
       cout << "The full modified reversed string str1 is now:\n ";
       for ( str_rIter = str1.rbegin( ); str_rIter != str1.rend( ); str_rIter++ )
          cout << *str_rIter;
       cout << endl;
       // The following line would be an error because iterator is const
       // *str1_rcIter = 'T';
       // For an empty string, end is equivalent to begin
       if ( str2.rbegin( ) == str2.rend ( ) )
          cout << "The string str2 is empty." << endl;
          cout << "The stringstr2  is not empty." << endl;
    
    The last character-letter of the reversed string str1 is: A
    The full reversed string str1 is:
    ablE was I ere I saw elbA
    The last character-letter of the modified str1 is now: o
    The full modified reversed string str1 is now:
    ablE was I ere I saw elbo
    The string str2 is empty.
    

    basic_string::replace

    用指定字符或者从其他范围、字符串或 C 字符串复制的字符来替代字符串中指定位置的元素。

    basic_string<CharType, Traits, Allocator>& replace(
        size_type position_1,
        size_type number_1,
        const value_type* ptr);
    basic_string<CharType, Traits, Allocator>& replace(
        size_type position_1,
        size_type number_1,
        const basic_string<CharType, Traits, Allocator>& str);
    basic_string<CharType, Traits, Allocator>& replace(
        size_type position_1,
        size_type number_1,
        const value_type* ptr,
        size_type number_2);
    basic_string<CharType, Traits, Allocator>& replace(
        size_type position_1,
        size_type number_1,
        const basic_string<CharType, Traits, Allocator>& str,
        size_type position_2,
        size_type number_2);
    basic_string<CharType, Traits, Allocator>& replace(
        size_type position_1,
        size_type number_1,
        size_type count,
        value_type char_value);
    basic_string<CharType, Traits, Allocator>& replace(
        iterator first0,
        iterator last0,
        const value_type* ptr);
    basic_string<CharType, Traits, Allocator>& replace(
        iterator first0,
        iterator last0,
        const basic_string<CharType, Traits, Allocator>& str);
    basic_string<CharType, Traits, Allocator>& replace(
        iterator first0,
        iterator last0,
        const value_type* ptr,
        size_type number_2);
    basic_string<CharType, Traits, Allocator>& replace(
        iterator first0,
        iterator last0,
        size_type number_2,
        value_type char_value);
    template <class InputIterator>
    basic_string<CharType, Traits, Allocator>& replace(
        iterator first0,
        iterator last0,
        InputIterator first,
        InputIterator last);
    basic_string<CharType, Traits, Allocator>& replace(
        iterator first0,
        iterator last0,
        const_pointer first,
        const_pointer last);
    basic_string<CharType, Traits, Allocator>& replace(
        iterator first0,
        iterator last0,
        const_iterator first,
        const_iterator last);
    要作为操作数字符串字符的源的字符串。

    position_1
    开始进行替换的操作数字符串的索引。

    number_1
    操作数字符串中要替换的最大字符数。

    position_2
    开始进行复制的参数字符串的索引。

    number_2
    要从参数 C 字符串使用的最大字符数。

    要作为操作数字符串字符的源的 C 字符串。

    char_value
    要复制到操作数字符串的字符。

    first0
    一种迭代器,用于寻址操作数字符串中要删除的第一个字符。

    last0
    一种迭代器,用于寻址操作数字符串中要删除的最后一个字符。

    first
    一种迭代器(const_pointer 或 const_iterator),用于寻址参数字符串中进行复制的第一个字符。

    一种迭代器(const_pointer 或 const_iterator),用于寻址参数字符串中进行复制的最后一个字符。

    count
    char_value 复制到操作数字符串的次数。

    进行了替换的操作数字符串。

    // basic_string_replace.cpp
    // compile with: /EHsc
    #include <string>
    #include <iostream>
    int main( )
       using namespace std;
       // The first two member functions replace
       // part of the operand string with
       // characters from a parameter string or C-string
       string result1a, result1b;
       string s1o ( "AAAAAAAA" );
       string s1p ( "BBB" );
       const char* cs1p = "CCC";
       cout << "The operand string s1o is: " << s1o << endl;
       cout << "The parameter string s1p is: " << s1p << endl;
       cout << "The parameter C-string cs1p is: " << cs1p << endl;
       result1a = s1o.replace ( 1 , 3 , s1p );
       cout << "The result of s1o.replace ( 1 , 3 , s1p )\n is "
            << "the string: " << result1a << "." << endl;
       result1b = s1o.replace ( 5 , 3 , cs1p );
       cout << "The result of s1o.replace ( 5 , 3 , cs1p )\n is "
            << "the string: " << result1b << "." << endl;
       cout << endl;
       // The third & fourth member function replace
       // part of the operand string with characters
       // form part of a parameter string or C-string
       string result2a, result2b;
       string s2o ( "AAAAAAAA" );
       string s2p ( "BBB" );
       const char* cs2p = "CCC";
       cout << "The operand string s2o is: " << s2o << endl;
       cout << "The parameter string s1p is: " << s2p << endl;
       cout << "The parameter C-string cs2p is: " << cs2p << endl;
       result2a = s2o.replace ( 1 , 3 , s2p , 1 , 2 );
       cout << "The result of s2o.replace (1, 3, s2p, 1, 2)\n is "
            << "the string: " << result2a << "." << endl;
       result2b = s2o.replace ( 4 , 3 , cs2p , 1 );
       cout << "The result of s2o.replace (4 ,3 ,cs2p)\n is "
            << "the string: " << result2b << "." << endl;
       cout << endl;
       // The fifth member function replaces
       // part of the operand string with characters
       string result3a;
       string s3o ( "AAAAAAAA" );
       char ch3p = 'C';
       cout << "The operand string s3o is: " << s3o << endl;
       cout << "The parameter character c1p is: " << ch3p << endl;
       result3a = s3o.replace ( 1 , 3 , 4 , ch3p );
       cout << "The result of s3o.replace(1, 3, 4, ch3p)\n is "
            << "the string: " << result3a << "." << endl;
       cout << endl;
       // The sixth & seventh member functions replace
       // part of the operand string, delineated with iterators,
       // with a parameter string or C-string
       string s4o ( "AAAAAAAA" );
       string s4p ( "BBB" );
       const char* cs4p = "CCC";
       cout << "The operand string s4o is: " << s4o << endl;
       cout << "The parameter string s4p is: " << s4p << endl;
       cout << "The parameter C-string cs4p is: " << cs4p << endl;
       basic_string<char>::iterator IterF0, IterL0;
       IterF0 = s4o.begin ( );
       IterL0 = s4o.begin ( ) + 3;
       string result4a, result4b;
       result4a = s4o.replace ( IterF0 , IterL0 , s4p );
       cout << "The result of s1o.replace (IterF0, IterL0, s4p)\n is "
            << "the string: " << result4a << "." << endl;
       result4b = s4o.replace ( IterF0 , IterL0 , cs4p );
       cout << "The result of s4o.replace (IterF0, IterL0, cs4p)\n is "
            << "the string: " << result4b << "." << endl;
       cout << endl;
       // The 8th member function replaces
       // part of the operand string delineated with iterators
       // with a number of characters from a parameter C-string
       string s5o ( "AAAAAAAF" );
       const char* cs5p = "CCCBB";
       cout << "The operand string s5o is: " << s5o << endl;
       cout << "The parameter C-string cs5p is: " << cs5p << endl;
       basic_string<char>::iterator IterF1, IterL1;
       IterF1 = s5o.begin ( );
       IterL1 = s5o.begin ( ) + 4;
       string result5a;
       result5a = s5o.replace ( IterF1 , IterL1 , cs5p , 4 );
       cout << "The result of s5o.replace (IterF1, IterL1, cs4p ,4)\n is "
            << "the string: " << result5a << "." << endl;
       cout << endl;
       // The 9th member function replaces
       // part of the operand string delineated with iterators
       // with specified characters
       string s6o ( "AAAAAAAG" );
       char ch6p = 'q';
       cout << "The operand string s6o is: " << s6o << endl;
       cout << "The parameter character ch6p is: " << ch6p << endl;
       basic_string<char>::iterator IterF2, IterL2;
       IterF2 = s6o.begin ( );
       IterL2 = s6o.begin ( ) + 3;
       string result6a;
       result6a = s6o.replace ( IterF2 , IterL2 , 4 , ch6p );
       cout << "The result of s6o.replace (IterF1, IterL1, 4, ch6p)\n is "
            << "the string: " << result6a << "." << endl;
       cout << endl;
       // The 10th member function replaces
       // part of the operand string delineated with iterators
       // with part of a parameter string delineated with iterators
       string s7o ( "OOOOOOO" );
       string s7p ( "PPPP" );
       cout << "The operand string s7o is: " << s7o << endl;
       cout << "The parameter string s7p is: " << s7p << endl;
       basic_string<char>::iterator IterF3, IterL3, IterF4, IterL4;
       IterF3 = s7o.begin ( ) + 1;
       IterL3 = s7o.begin ( ) + 3;
       IterF4 = s7p.begin ( );
       IterL4 = s7p.begin ( ) + 2;
       string result7a;
       result7a = s7o.replace ( IterF3 , IterL3 , IterF4 , IterL4 );
       cout << "The result of s7o.replace (IterF3 ,IterL3 ,IterF4 ,IterL4)\n is "
            << "the string: " << result7a << "." << endl;
       cout << endl;
    
    The operand string s1o is: AAAAAAAA
    The parameter string s1p is: BBB
    The parameter C-string cs1p is: CCC
    The result of s1o.replace ( 1 , 3 , s1p )
    is the string: ABBBAAAA.
    The result of s1o.replace ( 5 , 3 , cs1p )
    is the string: ABBBACCC.
    The operand string s2o is: AAAAAAAA
    The parameter string s1p is: BBB
    The parameter C-string cs2p is: CCC
    The result of s2o.replace (1, 3, s2p, 1, 2)
    is the string: ABBAAAA.
    The result of s2o.replace (4 ,3 ,cs2p)
    is the string: ABBAC.
    The operand string s3o is: AAAAAAAA
    The parameter character c1p is: C
    The result of s3o.replace(1, 3, 4, ch3p)
    is the string: ACCCCAAAA.
    The operand string s4o is: AAAAAAAA
    The parameter string s4p is: BBB
    The parameter C-string cs4p is: CCC
    The result of s1o.replace (IterF0, IterL0, s4p)
    is the string: BBBAAAAA.
    The result of s4o.replace (IterF0, IterL0, cs4p)
    is the string: CCCAAAAA.
    The operand string s5o is: AAAAAAAF
    The parameter C-string cs5p is: CCCBB
    The result of s5o.replace (IterF1, IterL1, cs4p ,4)
    is the string: CCCBAAAF.
    The operand string s6o is: AAAAAAAG
    The parameter character ch6p is: q
    The result of s6o.replace (IterF1, IterL1, 4, ch6p)
    is the string: qqqqAAAAG.
    The operand string s7o is: OOOOOOO
    The parameter string s7p is: PPPP
    The result of s7o.replace (IterF3 ,IterL3 ,IterF4 ,IterL4)
    is the string: OPPOOOO.
    

    basic_string::reserve

    将字符串的容量设置为一个数目,这个数目至少应与指定数目一样大。

    void reserve(size_type count = 0);
    

    count
    具有保留内存的字符数。

    拥有足够的容量至关重要,因为重新分配十分耗时。 并且,它会使引用字符串中字符的所有引用、指针和迭代器失效。

    字符串对象类型的容量的概念与 vector 类型对象的容量的概念相同。 与 vector 不同的是,成员函数 reserve 可能被调用,以减小对象的容量。 这是非绑定请求,不一定会发生。 因为参数的默认值为 0,调用 reserve 是减小字符串容量的非绑定请求,用以适应字符串当前的字符数。 该容量永远不会减少到当前字符数以下。

    调用 reserve 是减小字符串容量的唯一可能方法。 但是,如上文所述,这是非绑定请求,可能不会发生。

    // basic_string_reserve.cpp
    // compile with: /EHsc
    #include <string>
    #include <iostream>
    int main( )
       using namespace std;
       string str1 ("Hello world");
       cout << "The original string str1 is: " << str1 << endl;
       basic_string <char>::size_type sizeStr1, sizerStr1;
       sizeStr1 = str1.size ( );
       basic_string <char>::size_type capStr1, caprStr1;
       capStr1 = str1.capacity ( );
       // Compare size & capacity of the original string
       cout << "The current size of original string str1 is: "
            << sizeStr1 << "." << endl;
       cout << "The capacity of original string str1 is: "
            << capStr1 << "." << endl << endl;
       // Compare size & capacity of the string
       // with added capacity
       str1.reserve ( 40 );
       sizerStr1 = str1.size ( );
       caprStr1 = str1.capacity ( );
       cout << "The string str1with augmented capacity is: "
            << str1 << endl;
       cout << "The current size of string str1 is: "
            << sizerStr1 << "." << endl;
       cout << "The new capacity of string str1 is: "
            << caprStr1 << "." << endl << endl;
       // Compare size & capacity of the string
       // with downsized capacity
       str1.reserve ( );
       basic_string <char>::size_type sizedStr1;
       basic_string <char>::size_type capdStr1;
       sizedStr1 = str1.size ( );
       capdStr1 = str1.capacity ( );
       cout << "The string str1 with downsized capacity is: "
            << str1 << endl;
       cout << "The current size of string str1 is: "
            << sizedStr1 << "." << endl;
       cout << "The reduced capacity of string str1 is: "
            << capdStr1 << "." << endl << endl;
    
    The original string str1 is: Hello world
    The current size of original string str1 is: 11.
    The capacity of original string str1 is: 15.
    The string str1with augmented capacity is: Hello world
    The current size of string str1 is: 11.
    The new capacity of string str1 is: 47.
    The string str1 with downsized capacity is: Hello world
    The current size of string str1 is: 11.
    The reduced capacity of string str1 is: 47.
    

    basic_string::resize

    根据要求追加或删除元素,为字符串指定新的大小。

    void resize(
        size_type count,);
    void resize(
        size_type count,
        value_type char_value);
    

    count
    字符串的新大小。

    char_value
    在需要更多元素的情况下,用于初始化追加字符的值。

    如果所生成的大小超过最大字符数,则窗体引发 length_error

    // basic_string_resize.cpp
    // compile with: /EHsc
    #include <string>
    #include <iostream>
    int main( )
       using namespace std;
       string  str1 ( "Hello world" );
       cout << "The original string str1 is: " << str1 << endl;
       basic_string <char>::size_type sizeStr1;
       sizeStr1 = str1.size ( );
       basic_string <char>::size_type capStr1;
       capStr1 = str1.capacity ( );
       // Compare size & capacity of the original string
       cout << "The current size of original string str1 is: "
            << sizeStr1 << "." << endl;
       cout << "The capacity of original string str1 is: "
            << capStr1 << "." << endl << endl;
       // Use resize to increase size by 2 elements: exclamations
       str1.resize ( str1.size ( ) + 2 , '!' );
       cout << "The resized string str1 is: " << str1 << endl;
       sizeStr1 = str1.size ( );
       capStr1 = str1.capacity ( );
       // Compare size & capacity of a string after resizing
       cout << "The current size of resized string str1 is: "
            << sizeStr1 << "." << endl;
       cout << "The capacity of resized string str1 is: "
            << capStr1 << "." << endl << endl;
       // Use resize to increase size by 20 elements:
       str1.resize ( str1.size ( ) + 20 );
       cout << "The resized string str1 is: " << str1 << endl;
       sizeStr1 = str1.size ( );
       capStr1 = str1.capacity ( );
       // Compare size & capacity of a string after resizing
       // note capacity increases automatically as required
       cout << "The current size of modified string str1 is: "
            << sizeStr1 << "." << endl;
       cout << "The capacity of modified string str1 is: "
            << capStr1 << "." << endl << endl;
       // Use resize to downsize by 28 elements:
       str1.resize ( str1.size ( ) - 28 );
       cout << "The downsized string str1 is: " << str1 << endl;
       sizeStr1 = str1.size (  );
       capStr1 = str1.capacity (  );
       // Compare size & capacity of a string after downsizing
       cout << "The current size of downsized string str1 is: "
            << sizeStr1 << "." << endl;
       cout << "The capacity of downsized string str1 is: "
            << capStr1 << "." << endl;
    
    The original string str1 is: Hello world
    The current size of original string str1 is: 11.
    The capacity of original string str1 is: 15.
    The resized string str1 is: Hello world!!
    The current size of resized string str1 is: 13.
    The capacity of resized string str1 is: 15.
    The resized string str1 is: Hello world!!
    The current size of modified string str1 is: 33.
    The capacity of modified string str1 is: 47.
    The downsized string str1 is: Hello
    The current size of downsized string str1 is: 5.
    The capacity of downsized string str1 is: 47.
    

    basic_string::reverse_iterator

    提供对存储在字符串中的元素的引用的类型。

    typedef std::reverse_iterator<iterator> reverse_iterator;
    

    类型 reverse_iterator 可用于修改字符值并用于反向循环访问字符串。

    有关如何声明和使用 reverse_iterator 的示例,请参阅 rbegin 的示例。

    basic_string::rfind

    向后搜索字符串,搜索与指定字符序列匹配的第一个子字符串。

    size_type rfind(
        value_type char_value,
        size_type offset = npos) const;
    size_type rfind(
        const value_type* ptr,
        size_type offset = npos) const;
    size_type rfind(
        const value_type* ptr,
        size_type offset,
        size_type count) const;
    size_type rfind(
        const basic_string<CharType, Traits, Allocator>& str,
        size_type offset = npos) const;
    

    char_value
    成员函数要搜索的字符值。

    offset
    搜索开始处的索引。

    成员函数要搜索的 C 字符串。

    count
    在成员函数要搜索的 C 字符串中从第一个字符开始计数的字符数。

    成员函数要搜索的字符串。

    后向搜索时,在搜索成功时子字符串第一个字符的最后一个匹配项的索引;否则为 npos

    // basic_string_rfind.cpp
    // compile with: /EHsc
    #include <string>
    #include <iostream>
    int main( )
       using namespace std;
       // The first member function
       // searches for a single character in a string
       string str1 ( "Hello Everyone" );
       cout << "The original string str1 is: " << str1 << endl;
       basic_string <char>::size_type indexCh1a, indexCh1b;
       static const basic_string <char>::size_type npos = -1;
       indexCh1a = str1.rfind ( "e" , 9 );
       if ( indexCh1a != npos )
          cout << "The index of the 1st 'e' found before the 9th"
               << " position in str1 is: " << indexCh1a << endl;
          cout << "The character 'e' was not found in str1 ." << endl;
       indexCh1b = str1.rfind ( "x" );
       if ( indexCh1b != npos )
          cout << "The index of the 'x' found in str1 is: "
               << indexCh1b << endl << endl;
          cout << "The character 'x' was not found in str1."
               << endl << endl;
       // The second member function searches a string
       // for a substring as specified by a C-string
       string str2 ( "Let me make this perfectly clear." );
       cout << "The original string str2 is: " << str2 << endl;
       basic_string <char>::size_type indexCh2a, indexCh2b;
       const char *cstr2 = "perfect";
       indexCh2a = str2.rfind ( cstr2 , 30 );
       if ( indexCh2a != npos )
          cout << "The index of the 1st element of 'perfect' "
               << "before\n the 30th position in str2 is: "
               << indexCh2a << endl;
          cout << "The substring 'perfect' was not found in str2 ."
               << endl;
       const char *cstr2b = "imperfectly";
       indexCh2b = str2.rfind ( cstr2b , 30 );
       if ( indexCh2b != npos )
          cout << "The index of the 1st element of 'imperfect' "
               << "before\n the 5th position in str3 is: "
               << indexCh2b << endl;
          cout << "The substring 'imperfect' was not found in str2 ."
               << endl << endl;
       // The third member function searches a string
       // for a substring as specified by a C-string
       string str3 ( "It is a nice day. I am happy." );
       cout << "The original string str3 is: " << str3 << endl;
       basic_string <char>::size_type indexCh3a, indexCh3b;
       const char *cstr3a = "nice";
       indexCh3a = str3.rfind ( cstr3a );
       if ( indexCh3a != npos )
          cout << "The index of the 1st element of 'nice' "
               << "in str3 is: " << indexCh3a << endl;
          cout << "The substring 'nice' was not found in str3 ."
               << endl;
       const char *cstr3b = "am";
       indexCh3b = str3.rfind ( cstr3b , indexCh3a + 25 , 2 );
       if ( indexCh3b != npos )
          cout << "The index of the next occurrence of 'am' in "
               << "str3 begins at: " << indexCh3b << endl << endl;
          cout << "There is no next occurrence of 'am' in str3 ."
               << endl << endl;
       // The fourth member function searches a string
       // for a substring as specified by a string
       string str4 ( "This perfectly unclear." );
       cout << "The original string str4 is: " << str4 << endl;
       basic_string <char>::size_type indexCh4a, indexCh4b;
       string str4a ( "clear" );
       indexCh4a = str4.rfind ( str4a , 15 );
       if (indexCh4a != npos )
          cout << "The index of the 1st element of 'clear' "
               << "before\n the 15th position in str4 is: "
               << indexCh4a << endl;
          cout << "The substring 'clear' was not found in str4 "
               << "before the 15th position." << endl;
       string str4b ( "clear" );
       indexCh4b = str4.rfind ( str4b );
       if ( indexCh4b != npos )
          cout << "The index of the 1st element of 'clear' "
               << "in str4 is: "
               << indexCh4b << endl;
          cout << "The substring 'clear' was not found in str4 ."
               << endl << endl;
    
    The original string str1 is: Hello Everyone
    The index of the 1st 'e' found before the 9th position in str1 is: 8
    The character 'x' was not found in str1.
    The original string str2 is: Let me make this perfectly clear.
    The index of the 1st element of 'perfect' before
    the 30th position in str2 is: 17
    The substring 'imperfect' was not found in str2 .
    The original string str3 is: It is a nice day. I am happy.
    The index of the 1st element of 'nice' in str3 is: 8
    The index of the next occurrence of 'am' in str3 begins at: 20
    The original string str4 is: This perfectly unclear.
    The substring 'clear' was not found in str4 before the 15th position.
    The index of the 1st element of 'clear' in str4 is: 17