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 email@example.com. 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.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Custom Jumble Word Game
- Comparator Class in C++ with Examples
- Complete Guide to Install C++17 in Windows
- Features of C++
- Draw a triangle in C++ graphics
- Implementation of lower_bound() and upper_bound() in List of Pairs in C++
- How to Setup VSCode with C, C++ and Python for Competitive Programming
- Java System.exit(0) vs C++ return 0
- Features of C++17 with Examples
- Life cycle of Objects in C++ with Example
- Difference between virtual function and inline function in C++
- Difference between Turbo C++ and Dev C++
- Ellipsis in C++ with Examples
- Difference between Static and Dynamic Memory Allocation in C
- Const vs Regular iterators in C++ with examples
Improved By : ManasChhabra2