![The C++ Standard Library](/cpp_standard_library/images/cpp-mini-lib-logo.jpg)
- The C Standard Library
- The C Standard Library
- The C++ Standard Library
- C++ Library - Home
- C++ Library - <fstream>
- C++ Library - <iomanip>
- C++ Library - <ios>
- C++ Library - <iosfwd>
- C++ Library - <iostream>
- C++ Library - <istream>
- C++ Library - <ostream>
- C++ Library - <sstream>
- C++ Library - <streambuf>
- C++ Library - <atomic>
- C++ Library - <complex>
- C++ Library - <exception>
- C++ Library - <functional>
- C++ Library - <limits>
- C++ Library - <locale>
- C++ Library - <memory>
- C++ Library - <new>
- C++ Library - <numeric>
- C++ Library - <regex>
- C++ Library - <stdexcept>
- C++ Library - <string>
- C++ Library - <thread>
- C++ Library - <tuple>
- C++ Library - <typeinfo>
- C++ Library - <utility>
- C++ Library - <valarray>
- The C++ STL Library
- C++ Library - <array>
- C++ Library - <bitset>
- C++ Library - <deque>
- C++ Library - <forward_list>
- C++ Library - <list>
- C++ Library - <map>
- C++ Library - <multimap>
- C++ Library - <queue>
- C++ Library - <priority_queue>
- C++ Library - <set>
- C++ Library - <stack>
- C++ Library - <unordered_map>
- C++ Library - <unordered_set>
- C++ Library - <vector>
- C++ Library - <algorithm>
- C++ Library - <iterator>
- C++ Programming Resources
- C++ Programming Tutorial
- C++ Useful Resources
- C++ Discussion
C++ String Library - assign
Description
It appends character c to the end of the string, increasing its length by one.
Declaration
Following is the declaration for std::string::assign.
string& assign (const string& str);
C++11
string& assign (const string& str);
C++14
string& assign (const string& str);
Parameters
c − It is a character object.
str − It is a string object.
Return Value
It returns *this.
Exceptions
if an exception is thrown, there are no changes in the string.
Example
In below example for std::string::assign.
#include <iostream> #include <string> int main () { std::string str; std::string base="Sairamkrishna is a one of the tech person in tutorialspoint."; str.assign(base); std::cout << str << '\n'; str.assign(base,10,9); std::cout << str << '\n'; str.assign("pangrams are cool",7); std::cout << str << '\n'; str.assign("c-string"); std::cout << str << '\n'; str.assign(10,'*'); std::cout << str << '\n'; str.assign<int>(10,0x2D); std::cout << str << '\n'; str.assign(base.begin()+16,base.end()-12); std::cout << str << '\n'; return 0; }
The sample output should be like this −
Sairamkrishna is a one of the tech person in tutorialspoint. hna is a pangram c-string ********** ---------- a one of the tech person in tut
string.htm
Advertisements