C++ 字串庫 - 建構函式



描述

它用於構造字串物件,並根據使用的建構函式版本初始化其值。

宣告

以下是 std::string::string 的宣告。

string();

引數

  • str − 另一個字串物件。

  • pos − 包含第一個字串字元的位置。

  • len − 包含子字串的長度。

  • s − 指向字元陣列的指標。

  • n − 包含要複製的字元數量的資訊。

  • c − 用於填充字串的字元。

  • first, last − 用於查詢範圍中初始和最終位置的輸入迭代器。

  • il − 初始化列表物件。

返回值

異常

從不丟擲任何異常。

示例

在以下 std::string::string 的示例中。

#include <iostream>
#include <string>

int main () {
   std::string s0 ("initial string");
   std::string s1;
   std::string s2 (s0);
   std::string s3 (s0, 8, 3);
   std::string s4 ("A character sequence", 6);
   std::string s5 ("Another character sequence");
   std::string s6a (10, 'x');
   std::string s6b (10, 42);
   std::string s7 (s0.begin(), s0.begin()+7);

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

示例輸出應如下所示:

s1:
s2: initial string
s3: str
s4: A char
s5: Another character sequence
s6a: xxxxxxxxxx
s6b: **********
s7: initial
string.htm
廣告

© . All rights reserved.