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’.
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()).
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 firstname.lastname@example.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.
- Synchronous Chatting Application using C++ boost::asio
- C/C++ For loop with Examples
- How to input a comma separated string in C++?
- Program to Parse a comma separated string in C++
- Difference between Private and Protected in C++ with Example
- Difference between Inheritance and Polymorphism
- Difference between Single and Multiple Inheritance in C++
- Difference between Abstraction and Encapsulation in C++
- return statement in C/C++ with Examples
- How to implement our own Vector Class in C++?
- Web Programming in C++
- C++ Program to print an Array using Recursion
- Storage Classes in C++ with Examples
- fill_n() function in C++ STL with examples
- fill() function in C++ STL with examples
Improved By : ManasChhabra2