A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://cplusplus.com/reference/string/string/resize/ below:

public member function

<string>

std::string::resize
void resize (size_t n);void resize (size_t n, char c);

Resize string

Resizes the string to a length of n characters.

If n is smaller than the current string length, the current value is shortened to its first n character, removing the characters beyond the nth.

If n is greater than the current string length, the current content is extended by inserting at the end as many characters as needed to reach a size of n. If c is specified, the new elements are initialized as copies of c, otherwise, they are value-initialized characters (null characters).



Parameters
n
New string length, expressed in number of characters.
size_t is an unsigned integral type (the same as member type string::size_type).
c
Character used to fill the new character space added to the string (in case the string is expanded).

Return Value none

Example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// resizing string
#include <iostream>
#include <string>

int main ()
{
  std::string str ("I like to code in C");
  std::cout << str << '\n';

  unsigned sz = str.size();

  str.resize (sz+2,'+');
  std::cout << str << '\n';

  str.resize (14);
  std::cout << str << '\n';
  return 0;
}

Output:
I like to code in C
I like to code in C++
I like to code


Complexity Unspecified, but generally up to linear in the new string length.

Iterator validity Any iterators, pointers and references related to this object may be invalidated.
Data races The object is modified.

Exception safetyStrong guarantee: if an exception is thrown, there are no changes in the string.

If n is greater than max_size, a length_error exception is thrown.


A bad_alloc exception is thrown if the function needs to allocate storage and fails.

See also
string::size
Return length of string (public member function)
string::clear
Clear string (public member function)
string::max_size
Return maximum size of string (public member function)

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4