Namespaces
Variants
Views
Actions

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

From cppreference.com
< cpp‎ | string‎ | basic string
m (Top: standardized: first/second => @1@/@2@; reorder.)
m (Example: added a case with default initialization = char())
Line 47: Line 47:
 
===Example===
 
===Example===
 
{{example
 
{{example
| p=true
 
 
| code =
 
| code =
 
#include <iostream>
 
#include <iostream>
Line 57: Line 56:
 
     const unsigned  desired_length{ 8 };
 
     const unsigned  desired_length{ 8 };
 
     std::string    long_string( "Where is the end?" );
 
     std::string    long_string( "Where is the end?" );
     std::string    short_string( "Ha" );
+
     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:\n"
 
              << "3. Before: " << quoted( short_string ) << '\n';
 
  
 +
    std::cout << "Lengthen with a given value 'a':\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 << "Lengthen with char() == " << static_cast<int>(char()) << '\n'
 +
              << "5. Before: " << 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 << "4. After:  " << quoted( short_string ) <<  "\n\n";
 
  
     std::cout << "Errors:\n";
+
     std::cout << "Errors:\n";
 
     {
 
     {
 
         std::string s;
 
         std::string s;
Line 102: Line 110:
 
     }
 
     }
 
}
 
}
| output=
+
| p=true
 +
| output=
 
Basic functionality:
 
Basic functionality:
 
Shorten:
 
Shorten:
 
1. Before: "Where is the end?"
 
1. Before: "Where is the end?"
2. After: "Where is"
+
2. After: "Where is"
Lengthen:
+
Lengthen with a given value 'a':
3. Before: "Ha"
+
3. Before: "H"
4. After: "Haaaaaaa"
+
4. After: "Haaaaaaa"
 +
Lengthen with char() == 0
 +
5. Before: "Haaaaaaa"
 +
6. After:  "Haaaaaaa@@@"
  
 
Errors:
 
Errors:

Revision as of 17:06, 18 January 2022

 
 
 
std::basic_string
Member functions
Element access
Iterators
Capacity
Modifiers
basic_string::resize
Search
Operations
Constants
Non-member functions
I/O
Comparison
(until C++20)(until C++20)(until C++20)(until C++20)(until C++20)(C++20)
Numeric conversions
(C++11)(C++11)(C++11)
(C++11)(C++11)
(C++11)(C++11)(C++11)
(C++11)
(C++11)
Literals
Helper classes
Deduction guides (C++17)

 
(1)
void resize( size_type count );
(until C++20)
constexpr void resize( size_type count );
(since C++20)
(2)
void resize( size_type count, CharT ch );
(until C++20)
constexpr void resize( size_type count, CharT ch );
(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().
2) Initializes appended characters to ch.

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

Contents

Parameters

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

Return value

(none)

Exceptions

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

If an exception is thrown for any reason, this function has no effect (strong exception guarantee).(since C++11)

Example

#include <iostream>
#include <iomanip>
#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: " << quoted( long_string ) << '\n';
    long_string.resize( desired_length );
    std::cout << "2. After:  " << quoted( long_string ) << '\n';
 
 
    std::cout << "Lengthen with a given value 'a':\n"
              << "3. Before: " << quoted( short_string ) << '\n';
    short_string.resize( desired_length, 'a' );
    std::cout << "4. After:  " << quoted( short_string ) << '\n';
 
 
    std::cout << "Lengthen with char() == " << static_cast<int>(char()) << '\n'
              << "5. Before: " << 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

See also

returns the number of characters
(public member function) [edit]