公共成員函式
<string>

std::string::string

預設 (1)
string();
複製 (2)
string (const string& str);
substring (3)
string (const string& str, size_t pos, size_t len = npos);
from c-string (4)
string (const char* s);
from sequence (5)
string (const char* s, size_t n);
fill (6)
string (size_t n, char c);
range (7)
template <class InputIterator>  string  (InputIterator first, InputIterator last);
預設 (1)
string();
複製 (2)
string (const string& str);
substring (3)
string (const string& str, size_t pos, size_t len = npos);
from c-string (4)
string (const char* s);
from buffer (5)
string (const char* s, size_t n);
fill (6)
string (size_t n, char c);
range (7)
template <class InputIterator>  string  (InputIterator first, InputIterator last);
initializer list (8)
string (initializer_list<char> il);
move (9)
string (string&& str) noexcept;
Construct string object
Constructs a string object, initializing its value depending on the constructor version used

(1) empty string constructor (default constructor)
Constructs an empty string, with a length of zero characters.
(2) copy constructor
Constructs a copy of str.
(3) substring constructor
Copies the portion of str that begins at the character position pos and spans len characters (or until the end of str, if either str is too short or if len is string::npos).
(4) from c-string
複製 *s* 指向的空終止字元序列(C 字串)。
(5) from buffer
從 *s* 指向的字元陣列中複製前 *n* 個字元。
(6) fill constructor
Fills the string with n consecutive copies of character c.
(7) range constructor
複製範圍內的字元序列[first,last),以相同的順序。
(8) initializer list
按相同順序複製 *il* 中的每個字元。
(9) move constructor
獲取 *str* 的內容。
str 處於未指定但有效的狀態。

All constructors above support an object of member typeallocator_typeas additional optional argument at the end, which for string is not relevant (not shown above, see basic_string's constructor for full signatures).

引數

str
Another string object, whose value is either copied or acquired.
pos
str 中作為子字串複製到物件的第一個字元的位置。
如果它大於strlength,則會丟擲out_of_range
注意:str中的第一個字元用值表示0(不是1).
len
要複製的子字串的長度(如果字串較短,則會複製儘可能多的字元)。
string::npos 指示直到 *str* 末尾的所有字元。
s
指向字元陣列的指標(例如c-string)。
n
要複製的字元數。
c
Character to fill the string with. Each of the n characters in the string will be initialized to a copy of this value.
first, last
指向範圍內初始位置和最終位置的輸入迭代器。 使用的範圍是[first,last),其中包括firstlast之間的所有字元,包括first指向的字元,但不包括last指向的字元。
函式模板引數InputIterator應為指向可轉換為char.
如果已知InputIterator是一種整數型別,引數被強制轉換為適當的型別,以便使用簽名 (5) 代替。
il
il
這些物件是從初始化列表宣告符自動構造的。

size_t 是一個無符號整數型別。

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// string constructor
#include <iostream>
#include <string>

int main ()
{
  std::string s0 ("Initial string");

  // constructors used in the same order as described above:
  std::string s1;
  std::string s2 (s0);
  std::string s3 (s0, 8, 3);
  std::string s4 ("A character sequence");
  std::string s5 ("Another character sequence", 12);
  std::string s6a (10, 'x');
  std::string s6b (10, 42);      // 42 is the ASCII code for '*'
  std::string s7 (s0.begin(), s0.begin()+7);

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

輸出
s1: 
s2: Initial string
s3: str
s4: A character sequence
s5: Another char
s6a: xxxxxxxxxx
s6b: **********
s7: Initial


複雜度

未指定。
Unspecified, but generally linear in the resulting string length (and constant for move constructors).

迭代器有效性

The move constructors (9) may invalidate iterators, pointers and references related to str.

資料競爭

The move constructors (9) modify str.

異常安全

The move constructor with no allocator argument (9, first) never throws exceptions (no-throw guarantee).
在所有其他情況下,如果丟擲異常,則不會產生任何影響(強保證)。

如果已知sis a null pointer, ifn == npos, or if the range specified by[first,last)無效,則會導致未定義行為

If pos is greater then str's length, an out_of_range exception is thrown.
If n is greater than the array pointed by s, it causes undefined behavior.
If the resulting string length would exceed the max_size, a length_error exception is thrown.
A bad_alloc exception is thrown if the function fails when attempting to allocate storage.

另見