Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

string::assign - C++ Reference http://www.cplusplus.com/reference/string/string/...

Search: Go
Not logged in

Reference <string> string assign register log in

C++
Information
Tutorials Test, build and scale mobile and web
Reference backends fast with App Engine.
Articles
TRY IT FREE
Forum
public member function
Reference
C library:
std:: string::assign <string>

Containers: C++98 C++11 C++14


Input/Output:
Multi-threading: string (1) string& assign (const string& str);
Other: substring (2) string& assign (const string& str, size_t subpos, size_t sublen);
<algorithm> c-string (3) string& assign (const char* s);
<bitset> buffer (4) string& assign (const char* s, size_t n);
<chrono> fill (5) string& assign (size_t n, char c);
<codecvt> template <class InputIterator>
<complex> range (6)
string& assign (InputIterator first, InputIterator last);
<exception>
<functional> Assign content to string
<initializer_list> Assigns a new value to the string, replacing its current contents.
<iterator>
<limits> (1) string
<locale> Copies str.
<memory> (2) substring
<new> Copies the portion of str that begins at the character position subpos and spans sublen characters (or until the end of str, if either
<numeric> str is too short or if sublen is string::npos).
<random> (3) c-string
<ratio> Copies the null-terminated character sequence (C-string) pointed by s.
<regex> (4) buffer
<stdexcept> Copies the first n characters from the array of characters pointed by s.
<string> (5) fill
<system_error> Replaces the current value by n consecutive copies of character c.
<tuple> (6) range
<typeindex> Copies the sequence of characters in the range [first,last), in the same order.
<typeinfo> (7) initializer list
<type_traits> Copies each of the characters in il, in the same order.
<utility> (8) move
<valarray> Acquires the contents of str.
str is left in an unspecified but valid state.
<string>
class templates:
basic_string Parameters
char_traits
classes: str
string Another string object, whose value is either copied or moved.
u16string subpos
u32string Position of the first character in str that is copied to the object as a substring.
wstring If this is greater than str's length, it throws out_of_range.
functions: Note: The first character in str is denoted by a value of 0 (not 1).
stod
sublen
stof Length of the substring to be copied (if the string is shorter, as many characters as possible are copied).
stoi A value of string::npos indicates all characters until the end of str.
stol
stold s
Pointer to an array of characters (such as a c-string).
stoll
stoul n
stoull Number of characters to copy.
to_string
c
to_wstring Character value, repeated n times.
string first, last
string::string Input iterators to the initial and final positions in a range. The range used is [first,last), which includes all the characters
string::~string 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
member functions:
char.
string::append
If InputIterator is an integral type, the arguments are casted to the proper types so that signature (5) is used instead.
string::assign
string::at il
string::back An initializer_list object.
string::begin These objects are automatically constructed from initializer list declarators.
string::capacity
string::cbegin size_t is an unsigned integral type.
string::cend
string::clear
string::compare Return Value
string::copy *this
string::crbegin

1 of 3 1/1/70, 5:01 AM
string::assign - C++ Reference http://www.cplusplus.com/reference/string/string/...

string::crend Example
string::c_str
1 // string::assign
string::data
2 #include <iostream>
string::empty
3 #include <string>
string::end 4
string::erase 5 int main ()
string::find 6 {
string::find_first_not_of 7 std::string str;
string::find_first_of 8 std::string base="The quick brown fox jumps over a lazy dog.";
string::find_last_not_of 9
string::find_last_of 10 // used in the same order as described above:
string::front 11
12 str.assign(base);
string::get_allocator
13 std::cout << str << '\n';
string::insert
14
string::length
15 str.assign(base,10,9);
string::max_size 16 std::cout << str << '\n'; // "brown fox"
string::operator+= 17
string::operator= 18 str.assign("pangrams are cool",7);
string::operator[] 19 std::cout << str << '\n'; // "pangram"
string::pop_back 20
string::push_back 21 str.assign("c-string");
string::rbegin 22 std::cout << str << '\n'; // "c-string"
string::rend 23
24 str.assign(10,'*');
string::replace
25 std::cout << str << '\n'; // "**********"
string::reserve
26
string::resize
27 str.assign<int>(10,0x2D);
string::rfind 28 std::cout << str << '\n'; // "----------"
string::shrink_to_fit 29
string::size 30 str.assign(base.begin()+16,base.end()-12);
string::substr 31 std::cout << str << '\n'; // "fox jumps over"
string::swap 32
member constants: 33 return 0;
string::npos 34 }
non-member overloads:
getline (string)
operator+ (string) Output:
operator<< (string) The quick brown fox jumps over a lazy dog.
operator>> (string) brown fox
relational operators (string) pangram
c-string
swap (string)
**********
----------
fox jumps over

Complexity
C++98 C++11
Unspecified.

Iterator validity
Any iterators, pointers and references related to this object may be invalidated.

Data races
The object is modified.
The move assign form (8), modifies str.

Exception safety
For the move assign (8), the function does not throw exceptions (no-throw guarantee).
In all other cases, there are no effects in case an exception is thrown (strong guarantee).

If s does not point to an array long enough, or if the range specified by [first,last) is not valid, it causes undefined behavior.

If subpos is greater than str's length, an out_of_range exception is thrown.


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 needs to allocate storage and fails.

See also
string::operator= String assignment (public member function )
string::append Append to string (public member function )
string::insert Insert into string (public member function )
string::replace Replace portion of string (public member function )

2 of 3 1/1/70, 5:01 AM
string::assign - C++ Reference http://www.cplusplus.com/reference/string/string/...

Home page | Privacy policy


© cplusplus.com, 2000-2017 - All rights reserved - v3.1
Spotted an error? contact us

3 of 3 1/1/70, 5:01 AM

You might also like