公共成员函数
<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.

另见