cplusplus.com
- TUTORIALS
- REFERENCE
- ARTICLES
- FORUM
C++
- Tutorials
- Reference
- Articles
- Forum
Reference
C library:
- <cassert> (assert.h)
- <cctype> (ctype.h)
- <cerrno> (errno.h)
- C++11 <cfenv> (fenv.h)
- <cfloat> (float.h)
- C++11 <cinttypes> (inttypes.h)
- <ciso646> (iso646.h)
- <climits> (limits.h)
- <clocale> (locale.h)
- <cmath> (math.h)
- <csetjmp> (setjmp.h)
- <csignal> (signal.h)
- <cstdarg> (stdarg.h)
- C++11 <cstdbool> (stdbool.h)
- <cstddef> (stddef.h)
- C++11 <cstdint> (stdint.h)
- <cstdio> (stdio.h)
- <cstdlib> (stdlib.h)
- <cstring> (string.h)
- C++11 <ctgmath> (tgmath.h)
- <ctime> (time.h)
- C++11 <cuchar> (uchar.h)
- <cwchar> (wchar.h)
- <cwctype> (wctype.h)
Containers:
- C++11 <array>
- <deque>
- C++11 <forward_list>
- <list>
- <map>
- <queue>
- <set>
- <stack>
- C++11 <unordered_map>
- C++11 <unordered_set>
- <vector>
Input/Output:
- <fstream>
- <iomanip>
- <ios>
- <iosfwd>
- <iostream>
- <istream>
- <ostream>
- <sstream>
- <streambuf>
Multi-threading:
- C++11 <atomic>
- C++11 <condition_variable>
- C++11 <future>
- C++11 <mutex>
- C++11 <thread>
Other:
- <algorithm>
- <bitset>
- C++11 <chrono>
- C++11 <codecvt>
- <complex>
- <exception>
- <functional>
- C++11 <initializer_list>
- <iterator>
- <limits>
- <locale>
- <memory>
- <new>
- <numeric>
- C++11 <random>
- C++11 <ratio>
- C++11 <regex>
- <stdexcept>
- <string>
- C++11 <system_error>
- C++11 <tuple>
- C++11 <type_traits>
- C++11 <typeindex>
- <typeinfo>
- <utility>
- <valarray>
<string>
class templates
classes
- string
- C++11 u16string
- C++11 u32string
- wstring
functions
- C++11 stod
- C++11 stof
- C++11 stoi
- C++11 stol
- C++11 stold
- C++11 stoll
- C++11 stoul
- C++11 stoull
- C++11 to_string
- C++11 to_wstring
string
- string::~string
- string::string
member functions
- string::append
- string::assign
- string::at
- C++11 string::back
- string::begin
- string::c_str
- string::capacity
- C++11 string::cbegin
- C++11 string::cend
- string::clear
- string::compare
- string::copy
- C++11 string::crbegin
- C++11 string::crend
- string::data
- string::empty
- string::end
- string::erase
- string::find
- string::find_first_not_of
- string::find_first_of
- string::find_last_not_of
- string::find_last_of
- C++11 string::front
- string::get_allocator
- string::insert
- string::length
- string::max_size
- string::operator[]
- string::operator+=
- string::operator=
- C++11 string::pop_back
- string::push_back
- string::rbegin
- string::rend
- string::replace
- string::reserve
- string::resize
- string::rfind
- C++11 string::shrink_to_fit
- string::size
- string::substr
- string::swap
member constants
non-member overloads
- getline (string)
- operator+ (string)
- operator<< (string)
- operator>> (string)
- relational operators (string)
- swap (string)
- Reference
- <string>
- string
- string
public member function <string>
std::string::string
| default (1) | string(); |
|---|
| copy (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); |
|---|
| default (1) | string(); |
|---|
| copy (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 Copies the null-terminated character sequence (C-string) pointed by
s. (5) from buffer Copies the first
n characters from the array of characters pointed by
s. (6) fill constructor Fills the string with
n consecutive copies of character
c. (7) range constructor Copies the sequence of characters in the range [first,last), in the same order. (8) initializer list Copies each of the characters in
il, in the same order. (9) move constructor Acquires the contents of
str.
str is left in an unspecified but valid state. All constructors above support an object of member type allocator_type as additional optional argument at the end, which for string is not relevant (not shown above, see basic_string's constructor for full signatures).
Parameters
str Another string object, whose value is either copied or acquired. pos Position of the first character in
str that is copied to the object as a substring. If this is greater than
str's length, it throws out_of_range. Note: The first character in
str is denoted by a value of 0 (not 1). len Length of the substring to be copied (if the string is shorter, as many characters as possible are copied). A value of string::npos indicates all characters until the end of
str. s Pointer to an array of characters (such as a
c-string). n Number of characters to copy. 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 Input iterators to the initial and final positions in a range. The range used is [first,last), which includes all the characters between
first and
last, including the character pointed by
first but not the character pointed by
last. The function template argument InputIterator shall be an input iterator type that points to elements of a type convertible to char. If InputIterator is an integral type, the arguments are casted to the proper types so that signature (5) is used instead. il An initializer_list object. These objects are automatically constructed from
initializer list declarators. size_t is an unsigned integral type.
Example
| 1234567891011121314151617181920212223 | // 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; } |
Output:
| s1: s2: Initial string s3: str s4: A character sequence s5: Another char s6a: xxxxxxxxxx s6b: ********** s7: Initial |
Complexity
Unspecified. Unspecified, but generally linear in the resulting string length (and constant for
move constructors).
Iterator validity
The
move constructors (9) may invalidate iterators, pointers and references related to
str.
Data races
The
move constructors (9) modify
str.
Exception safety
The
move constructor with no allocator argument
(9, first) never throws exceptions (no-throw guarantee). In all other cases, there are no effects in case an exception is thrown (strong guarantee). If s is a null pointer, if n == npos, or if the range specified by [first,last) is not valid, it causes
undefined behavior. 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.
See also
string::operator=String assignment
(public member function)string::assignAssign content to string
(public member function)string::resizeResize string
(public member function)string::clearClear string
(public member function) Home page | Privacy policy© cplusplus.com, 2000-2025 - All rights reserved -
v3.3.4s Spotted an error? contact us