Professional Documents
Culture Documents
String Assign
String Assign
Search: Go
Not logged 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>
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.
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/...
3 of 3 1/1/70, 5:01 AM