public member function
<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),其中包括first和last之間的所有字元,包括first指向的字元,但不包括last指向的字元。
函式模板引數InputIterator應為指向可轉換為charT.
如果已知InputIterator是一種整數型別,引數被強制轉換為適當的型別,以便使用簽名 (5) 代替。
- il
- il
這些物件是從初始化列表宣告符自動構造的。
charT是 basic_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.