Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/string/basic string/resize"

From cppreference.com
< cpp‎ | string‎ | basic string
m ({{c}}, headers sorted, fmt)
(Added LWG issue #2250 DR (part 7/16).)
 
(3 intermediate revisions by 3 users not shown)
Line 2: Line 2:
 
{{cpp/string/basic_string/navbar}}
 
{{cpp/string/basic_string/navbar}}
 
{{dcl begin}}
 
{{dcl begin}}
{{dcl rev multi|num=1
+
{{|num=1|=c++20|
|until1=c++20|dcl1=
+
 
void resize( size_type count );
 
void resize( size_type count );
|dcl2=
 
constexpr void resize( size_type count );
 
 
}}
 
}}
{{dcl rev multi|num=2
+
{{|num=2|=c++20|
|until1=c++20|dcl1=
+
 
void resize( size_type count, CharT ch );
 
void resize( size_type count, CharT ch );
|dcl2=
 
constexpr void resize( size_type count, CharT ch );
 
 
}}
 
}}
 
{{dcl end}}
 
{{dcl end}}
Line 20: Line 14:
 
If the current size is less than {{c|count}}, additional characters are appended:
 
If the current size is less than {{c|count}}, additional characters are appended:
  
@1@ Initializes appended characters to {{c|CharT()}} ({{c|'\0'}} if {{tt|CharT}} is {{c|char}}).
+
@1@ Initializes appended characters to {{c|CharT()}} ({{c|'\0'}} if {{tt|CharT}} is {{c|char}}).
 
@2@ Initializes appended characters to {{c|ch}}.
 
@2@ Initializes appended characters to {{c|ch}}.
  
Line 30: Line 24:
 
{{par|ch|character to initialize the new characters with}}
 
{{par|ch|character to initialize the new characters with}}
 
{{par end}}
 
{{par end}}
 
===Return value===
 
(none)
 
<!--
 
===Complexity===
 
Linear in the size of the string. -->
 
  
 
===Exceptions===
 
===Exceptions===
{{lc|std::length_error}} if {{c|count > max_size()}}.
+
{{lc|std::length_error}} if {{c|count > max_size()}}.
 
Any exceptions thrown by corresponding {{tt|Allocator}}.
 
Any exceptions thrown by corresponding {{tt|Allocator}}.
  
{{rev inl|since=c++11|
+
{{strong exception guarantee}}
If an exception is thrown for any reason, this function has no effect (strong exception guarantee).}}
+
  
 
===Example===
 
===Example===
 
{{example
 
{{example
|code =
+
|code=
 
#include <iomanip>
 
#include <iomanip>
 
#include <iostream>
 
#include <iostream>
Line 56: Line 43:
 
     std::string long_string("Where is the end?");
 
     std::string long_string("Where is the end?");
 
     std::string short_string("H");
 
     std::string short_string("H");
 
+
 
     std::cout << "Basic functionality:\n"
 
     std::cout << "Basic functionality:\n"
 
               << "Shorten:\n"
 
               << "Shorten:\n"
               << "1. Before: " << quoted(long_string) << '\n';
+
               << "1. Before: " << quoted(long_string) << '\n';
 
     long_string.resize(desired_length);
 
     long_string.resize(desired_length);
     std::cout << "2. After:  " << quoted(long_string) << '\n';
+
     std::cout << "2. After:  " << quoted(long_string) << '\n';
 
+
 
     std::cout << "Lengthen with a given value 'a':\n"
 
     std::cout << "Lengthen with a given value 'a':\n"
               << "3. Before: " << quoted(short_string) << '\n';
+
               << "3. Before: " << quoted(short_string) << '\n';
 
     short_string.resize(desired_length, 'a');
 
     short_string.resize(desired_length, 'a');
     std::cout << "4. After:  " << quoted(short_string) << '\n';
+
     std::cout << "4. After:  " << quoted(short_string) << '\n';
 
+
 
     std::cout << "Lengthen with char() == " << static_cast<int>(char()) << '\n'
 
     std::cout << "Lengthen with char() == " << static_cast<int>(char()) << '\n'
               << "5. Before: " << quoted(short_string) << '\n';
+
               << "5. Before: " << quoted(short_string) << '\n';
 
     short_string.resize(desired_length + 3);
 
     short_string.resize(desired_length + 3);
 
     std::cout << "6. After:  \"";
 
     std::cout << "6. After:  \"";
Line 75: Line 62:
 
         std::cout << (c == char() ? '@' : c);
 
         std::cout << (c == char() ? '@' : c);
 
     std::cout << "\"\n\n";
 
     std::cout << "\"\n\n";
 
+
 
     std::cout << "Errors:\n";
 
     std::cout << "Errors:\n";
 
     std::string s;
 
     std::string s;
 
+
 
     try
 
     try
 
     {
 
     {
Line 89: Line 76:
 
         std::cout << "1. Exception: " << ex.what() << '\n';
 
         std::cout << "1. Exception: " << ex.what() << '\n';
 
     }
 
     }
 
+
 
     try
 
     try
 
     {
 
     {
Line 100: Line 87:
 
         std::cout << "2. Exception: " << ex.what() << '\n';
 
         std::cout << "2. Exception: " << ex.what() << '\n';
 
     }
 
     }
 
+
 
     try
 
     try
 
     {
 
     {
Line 129: Line 116:
 
3. Length error: basic_string::_M_replace_aux
 
3. Length error: basic_string::_M_replace_aux
 
}}
 
}}
 +
 +
 +
 +
 +
 +
  
 
===See also===
 
===See also===

Latest revision as of 19:02, 11 December 2024

 
 
 
std::basic_string
 
void resize( size_type count );
(1) (constexpr since C++20)
void resize( size_type count, CharT ch );
(2) (constexpr since C++20)

Resizes the string to contain count characters.

If the current size is less than count, additional characters are appended:

1) Initializes appended characters to CharT() ('\0' if CharT is char).
2) Initializes appended characters to ch.

If the current size is greater than count, the string is reduced to its first count elements.

Contents

[edit] Parameters

count - new size of the string
ch - character to initialize the new characters with

[edit] Exceptions

std::length_error if count > max_size() is true. Any exceptions thrown by corresponding Allocator.

If an exception is thrown for any reason, this function has no effect (strong exception safety guarantee).

[edit] Example

#include <iomanip>
#include <iostream>
#include <stdexcept>
 
int main()
{
    const unsigned desired_length{8};
    std::string long_string("Where is the end?");
    std::string short_string("H");
 
    std::cout << "Basic functionality:\n"
              << "Shorten:\n"
              << "1. Before: " << std::quoted(long_string) << '\n';
    long_string.resize(desired_length);
    std::cout << "2. After:  " << std::quoted(long_string) << '\n';
 
    std::cout << "Lengthen with a given value 'a':\n"
              << "3. Before: " << std::quoted(short_string) << '\n';
    short_string.resize(desired_length, 'a');
    std::cout << "4. After:  " << std::quoted(short_string) << '\n';
 
    std::cout << "Lengthen with char() == " << static_cast<int>(char()) << '\n'
              << "5. Before: " << std::quoted(short_string) << '\n';
    short_string.resize(desired_length + 3);
    std::cout << "6. After:  \"";
    for (char c : short_string)
        std::cout << (c == char() ? '@' : c);
    std::cout << "\"\n\n";
 
    std::cout << "Errors:\n";
    std::string s;
 
    try
    {
        // size is OK, no length_error
        // (may throw bad_alloc)
        s.resize(s.max_size() - 1, 'x');
    }
    catch (const std::bad_alloc& ex)
    {
        std::cout << "1. Exception: " << ex.what() << '\n';
    }
 
    try
    {
        // size is OK, no length_error
        // (may throw bad_alloc)
        s.resize(s.max_size(), 'x');
    }
    catch (const std::bad_alloc& ex)
    {
        std::cout << "2. Exception: " << ex.what() << '\n';
    }
 
    try
    {
        // size is BAD, throw length_error
        s.resize(s.max_size() + 1, 'x');
    }
    catch (const std::length_error& ex)
    {
        std::cout << "3. Length error: " << ex.what() << '\n';
    }
}

Possible output:

Basic functionality:
Shorten:
1. Before: "Where is the end?"
2. After:  "Where is"
Lengthen with a given value 'a':
3. Before: "H"
4. After:  "Haaaaaaa"
Lengthen with char() == 0
5. Before: "Haaaaaaa"
6. After:  "Haaaaaaa@@@"
 
Errors:
1. Exception: std::bad_alloc
2. Exception: std::bad_alloc
3. Length error: basic_string::_M_replace_aux

[edit] Defect reports

The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

DR Applied to Behavior as published Correct behavior
LWG 847 C++98 there was no exception safety guarantee added strong exception safety guarantee
LWG 2250 C++98 the behavior was undefined if
count > max_size() is true
always throws an exception in this case

[edit] See also

returns the number of characters
(public member function) [edit]
reserves storage
(public member function) [edit]
reduces memory usage by freeing unused memory
(public member function) [edit]