std::string::resize() is used to change the length of a string by adding or removing characters from the end. It is useful when a string needs to be adjusted to a specific number of characters.
- If the new size is smaller, characters are removed from the end.
- If the new size is larger, new characters are added to the end of the string.
Using resize() with One Argument
The resize() function accepts the new size of the string as an argument. If the new size is smaller, characters are removed from the end. If it is larger, '\0' characters are added to the end.
Syntax
void resize(size_type num);
Here, num represents the new length of the string.
#include <iostream>
#include <string>
using namespace std;
// Function to demonstrate insert
void resizeDemo(string str)
{
// Resizes str to a string with
// 5 initial characters only
str.resize(5);
cout << "Using resize : ";
cout << str;
}
// Driver code
int main()
{
string str("GeeksforGeeks ");
cout << "Original String : " << str << endl;
resizeDemo(str);
return 0;
}
Output
Original String : GeeksforGeeks Using resize : Geeks
Explanation: The string is resized from its original length to 5, so all characters after the first five are removed.
Using resize() with Two Arguments
The two-argument version accepts the new size and a character. When the new size is greater than the current size, the specified character is added to fill the new positions.
Syntax
void resize(size_type num, char c);
- num: The new length of the string.
- c: The character used to fill newly added positions when the string grows.
resize() does not return a value. It directly modifies the string.
#include <iostream>
#include <string>
using namespace std;
// Function to demonstrate insert
void 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 code
int 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
Explanation: When resize(15, '$') is called, the string grows from 13 to 15 characters, so two $ characters are added. When resize(5, '$') is called, the string becomes shorter, so the characters after the first five are removed. The $ character is not used because no new characters are added.
Exceptions
resize() throws a std::length_error exception if the requested new size is greater than the maximum number of characters that the string is allowed to contain. The maximum supported size of a string is returned by max_size(). If resize() is called with a value greater than this limit, std::length_error is thrown.
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str;
try {
// Request a size greater than the maximum supported size
str.resize(str.max_size() + 1);
}
catch (const length_error& e) {
cout << "Exception: " << e.what() << endl;
}
return 0;
}
Output
Exception: basic_string::_M_replace_aux
The exact exception message may vary depending on the compiler and standard library implementation, but the exception type is std::length_error.