std::string::resize() in C++

26 Jul, 2019
resize() lets you change the number of characters. Here are we will describe two syntaxes supported by std::string::resize() in C++Return Value : None
Syntax 1: Resize the number of characters of *this to num.
void string ::resize (size_type num)
num: New string length, expressed in number of characters.
Errors: 
Throws length_error if num is equal to string ::npos.
Throws length_error if the resulting size exceeds the maximum number of
characters(max_size()).

Note : If num > size() then, the rest of characters are initialized by the ‘\0’.
// CPP code for resize (size_type num)  #include <iostream>#include <string>using namespace std;   // Function to demonstrate insertvoid resizeDemo(string str){       // Resizes str to a string with    // 5 initial characters only     str.resize(5);    cout << "Using resize : ";    cout << str;}  // Driver codeint main(){    string str("GeeksforGeeks ");       cout << "Original String : " << str << endl;    resizeDemo(str);       return 0;}
Output:
Original String : GeeksforGeeks 
Using resize : Geeks

Syntax 2: Uses a character to fill the difference between size() and num.
void string ::resize (size_type num, char c )
num: is the new string length, expressed in number of characters.
c: is the character needed to fill the new character space.
If num > size() : character c is used to fill space.
If num < size() : String is simply resized to num number of characters.
Errors: 
Throws length_error if num is equal to string ::npos.
Throws length_error if the resulting size exceeds the maximum number of
characters(max_size()).

// CPP code for resize (size_type num, char c )  #include <iostream>#include <string>using namespace std;   // Function to demonstrate insertvoid resizeDemo(string str){    cout << "Using resize :" << endl;    cout << "If num > size() : ";          // Resizes str to character length of    // 15 and fill the space with '$'    str.resize(15, '$');    cout << str << endl;    cout << "If num < size() : ";          // Resizes str to a string with    // 5 initial characters only     str.resize(5, '$');    cout << str;}  // Driver codeint main(){    string str("GeeksforGeeks");       cout << "Original String : " << str << endl;    resizeDemo(str);       return 0;}
Output:
Original String : GeeksforGeeks
Using resize :
If num > size() : GeeksforGeeks$$
If num < size() : Geeks

This article is contributed by Sakshi Tiwari. If you like GeeksforGeeks(We know you do!) and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
ManasChhabra2
cpp-strings-library
STL
C++
STL
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Sorting a vector in C++
Polymorphism in C++
Friend class and function in C++
std::string class in C++
Pair in C++ Standard Template Library (STL)
Queue in C++ Standard Template Library (STL)
Unordered Sets in C++ Standard Template Library
List in C++ Standard Template Library (STL)
std::find in C++
Inline Functions in C++