public member function
<string>

std::basic_string::basic_string

默认 (1)
explicit basic_string (const allocator_type& alloc = allocator_type());
复制 (2)
basic_string (const basic_string& str);
substring (3)
basic_string (const basic_string& str, size_type pos, size_type len = npos,              const allocator_type& alloc = allocator_type());
from c-string (4)
basic_string (const charT* s, const allocator_type& alloc = allocator_type());
from sequence (5)
basic_string (const charT* s, size_type n,              const allocator_type& alloc = allocator_type());
fill (6)
basic_string (size_type n, charT c,              const allocator_type& alloc = allocator_type());
range (7)
template <class InputIterator>  basic_string  (InputIterator first, InputIterator last,                 const allocator_type& alloc = allocator_type());
默认 (1)
explicit basic_string (const allocator_type& alloc = allocator_type());
复制 (2)
basic_string (const basic_string& str);basic_string (const basic_string& str, const allocator_type& alloc);
substring (3)
basic_string (const basic_string& str, size_type pos, size_type len = npos,              const allocator_type& alloc = allocator_type());
from c-string (4)
basic_string (const charT* s, const allocator_type& alloc = allocator_type());
from buffer (5)
basic_string (const charT* s, size_type n,              const allocator_type& alloc = allocator_type());
fill (6)
basic_string (size_type n, charT c,              const allocator_type& alloc = allocator_type());
range (7)
template <class InputIterator>  basic_string  (InputIterator first, InputIterator last,                 const allocator_type& alloc = allocator_type());
initializer list (8)
basic_string (initializer_list<charT> il,              const allocator_type& alloc = allocator_type());
move (9)
basic_string (basic_string&& str) noexcept;basic_string (basic_string&& str, const allocator_type& alloc);
默认 (1)
basic_string();explicit basic_string (const allocator_type& alloc);
复制 (2)
basic_string (const basic_string& str);basic_string (const basic_string& str, const allocator_type& alloc);
substring (3)
basic_string (const basic_string& str, size_type pos, size_type len = npos,              const allocator_type& alloc = allocator_type());
from c-string (4)
basic_string (const charT* s, const allocator_type& alloc = allocator_type());
from buffer (5)
basic_string (const charT* s, size_type n,              const allocator_type& alloc = allocator_type());
fill (6)
basic_string (size_type n, charT c,              const allocator_type& alloc = allocator_type());
range (7)
template <class InputIterator>  basic_string  (InputIterator first, InputIterator last,                 const allocator_type& alloc = allocator_type());
initializer list (8)
basic_string (initializer_list<charT> il,              const allocator_type& alloc = allocator_type());
move (9)
basic_string (basic_string&& str) noexcept;basic_string (basic_string&& str, const allocator_type& alloc);
Construct basic_string object
Constructs a basic_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 constructors
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 basic_string::npos).
(4) from c-string
复制 *s* 指向的空终止字符序列(C 字符串)。
The length is determined by callingtraits_type::length(s).
(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 contructors
获取 *str* 的内容。
str 处于未指定但有效的状态。

The basic_string object keeps an internal copy of alloc, which is used to allocate and free storage for the characters it contains throughout its lifetime.
The copy constructor (2) creates an object that keeps and uses a copy of str's allocator.
The basic_string object keeps an internal copy of alloc, which is used to allocate and free storage for the characters it contains.
The copy constructor (2, first signature) creates a container that keeps and uses a copy of the allocator returned by calling the appropriate selected_on_container_copy_construction trait on str's allocator.
The move constructor (9, first signature) acquires str's allocator.
The basic_string object keeps an internal copy of alloc, which is used to allocate and free storage for the characters it contains.
The default constructor (1, first signature) uses a default-constructed allocator.
The copy constructor (2, first signature) creates a container that keeps and uses a copy of the allocator returned by calling the appropriate selected_on_container_copy_construction trait on str's allocator.
The move constructor (9, first signature) acquires str's allocator.

参数

alloc
Allocator object.
The container keeps and uses an internal copy of this allocator.
成员类型allocator_typeis the internal allocator type used by the container, defined in basic_string as an alias of its third template parameter (Alloc).
如果已知allocator_typeis an instantiation of the default allocator (which has no state), this is not relevant.
str
另一个具有相同类型的basic_string对象(具有相同的类模板参数charT, 特性 (traits)Alloc), whose value is either copied or acquired.
pos
str 中作为子字符串复制到对象的第一个字符的位置。
如果它大于str长度,则抛出out_of_range
注意:str中的第一个字符用值表示0(不是1).
len
要复制的子字符串的长度(如果字符串较短,则会复制尽可能多的字符)。
basic_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应为指向可转换为charT.
如果已知InputIterator是一种整数类型,参数被强制转换为适当的类型,以便使用签名 (5) 代替。
il
il
这些对象是从初始化列表声明符自动构造的。

charTbasic_string 的字符类型(即,它的第一个模板参数)。
成员类型size_type是一种无符号整型类型。

示例

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", 6);
  std::string s5 ("Another character sequence");
  std::string s6 (10, 'x');
  std::string s7a (10, 42);
  std::string s7b (s0.begin(), s0.begin()+7);

  std::cout << "s1: " << s1 << "\ns2: " << s2 << "\ns3: " << s3;
  std::cout << "\ns4: " << s4 << "\ns5: " << s5 << "\ns6: " << s6;
  std::cout << "\ns7a: " << s7a << "\ns7b: " << s7b << '\n';
  return 0;
}

输出
s1: 
s2: Initial string
s3: str
s4: A char
s5: Another character sequence
s6: xxxxxxxxxx
s7a: **********
s7b: 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.
如果结果 字符串长度 超过 max_size,则会抛出 length_error 异常。
If the type uses the default allocator, a bad_alloc exception is thrown if the function fails when attempting to allocate storage.

另见