C++ string

来自泡泡学习笔记
跳到导航 跳到搜索

int转string

std::string str = std::to_string(num);


string比较

str1.compare(str2);


basic_string::find

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


// 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;
   else
      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;
   else
      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;
   else
      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;
   else
      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;
   else
      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;
   else
      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;
   else
      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;
   else
      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::length

basic_string::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::substr

从字符串起始处的指定位置复制最多某个数目的字符的子字符串。

#include <string>
#include <iostream>

int main()
{
    using namespace std;

    string  str1("Heterological paradoxes are persistent.");
    cout << "The original string str1 is: \n " << str1
        << endl << endl;

    basic_string <char> str2 = str1.substr(6, 7);
    cout << "The substring str1 copied is: " << str2
        << endl << endl;

    basic_string <char> str3 = str1.substr();
    cout << "The default substring str3 is: \n " << str3
        << "\n which is the entire original string." << endl;
}


The original string str1 is:
Heterological paradoxes are persistent.

The substring str1 copied is: logical

The default substring str3 is:
Heterological paradoxes are persistent.
which is the entire original string.


basic_string::clear

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

#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;
    else
        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::erase

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

#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::compare

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

#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;
    else
        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;
    else
        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;
    else
        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;
    else
        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;
    else
        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;
    else
        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;
    else
        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.


<string> 函数

  1. getline
  2. stod
  3. stof
  4. stoi
  5. stol
  6. stold
  7. stoll
  8. stoul
  9. stoull
  10. swap
  11. to_string
  12. to_wstring


getline

将字符串从输入流中一行一行地提取出来。

以下代码演示两种模式下的 getline():第一种使用默认分隔符(换行),第二种使用空格作为分隔符。 文件尾字符(键盘上的 CTRL-Z)用于控制 while 循环的终止。 此值会将 cin 的内部状态标志设置为 eofbit,后者必须使用 basic_ios::clear() 进行消除,这样第二个 while 循环才能正确运行。

// compile with: /EHsc /W4
#include <string>
#include <iostream>
#include <vector>

using namespace std;

int main()
{
    string str;
    vector<string> v1;
    cout << "Enter a sentence, press ENTER between sentences. (Ctrl-Z to stop): " << endl;
    // Loop until end-of-file (Ctrl-Z) is input, store each sentence in a vector.
    // Default delimiter is the newline character.
    while (getline(cin, str)) {
        v1.push_back(str);
    }

    cout << "The following input was stored with newline delimiter:" << endl;
    for (const auto& p : v1) {
        cout << p << endl;
    }

    cin.clear();

    vector<string> v2;
    // Now try it with a whitespace delimiter
    while (getline(cin, str, ' ')) {
        v2.push_back(str);
    }

    cout << "The following input was stored with whitespace as delimiter:" << endl;
    for (const auto& p : v2) {
        cout << p << endl;
    }
}