Namespaces
Variants
Views
Actions

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

From cppreference.com
< cpp‎ | string‎ | basic string
(exception guarantees per 21.4.1[string.require])
m (Added verbose examples.)
Line 41: Line 41:
 
#include <iostream>
 
#include <iostream>
 
#include <stdexcept>
 
#include <stdexcept>
 
+
 
int main()
 
int main()
 
{
 
{
     std::string s;
+
     std::cout << "Basic functionality:" << std::endl;
 +
    const unsigned  desired_length(8);
 +
    std::string    long_string( "Where is the end?" );
 +
    std::string    short_string( "Ha" );
 +
   
 +
    // Shorten
 +
    std::cout << "Before: \"" << long_string << "\"" << std::endl;
 +
    long_string.resize( desired_length );
 +
    std::cout << "After: \"" << long_string <<  "\"" << std::endl;
 +
   
 +
    // Lengthen
 +
    std::cout << "Before: \"" << short_string <<  "\"" << std::endl;
 +
    short_string.resize( desired_length, 'a' );
 +
    std::cout << "After: \"" << short_string <<  "\"" << std::endl;
 +
   
 +
    std::cout  << std::endl << "Errors:" << std::endl;
 +
    {
 +
        std::string s;  
  
    try {
+
        try {
        // size is OK, no length_error
+
            // size is OK, no length_error
        // (may throw bad_alloc)
+
            // (may throw bad_alloc)
        s.resize(s.max_size() - 1, 'x');
+
            s.resize(s.max_size() - 1, 'x');
    } catch (std::bad_alloc&) {
+
        } catch (std::bad_alloc&) {
        std::cout << "1. bad alloc\n";
+
            std::cout << "1. bad alloc\n";
    }
+
        }
 
+
   
    try {
+
        try {
        // size is OK, no length_error
+
            // size is OK, no length_error
        // (may throw bad_alloc)
+
            // (may throw bad_alloc)
        s.resize(s.max_size(), 'x');
+
            s.resize(s.max_size(), 'x');
    } catch (std::bad_alloc& exc) {
+
        } catch (std::bad_alloc& exc) {
        std::cout << "2. bad alloc\n";
+
            std::cout << "2. bad alloc\n";
    }
+
        }
 
+
   
    try {
+
        try {
        // size is BAD, throw length_error
+
            // size is BAD, throw length_error
        s.resize(s.max_size() + 1, 'x');
+
            s.resize(s.max_size() + 1, 'x');
    } catch (std::length_error&) {
+
        } catch (std::length_error&) {
        std::cout << "3. length error\n";
+
            std::cout << "3. length error\n";
    }
+
        }
 +
    }
 
}
 
}
 
| output=
 
| output=
 +
Basic functionality:
 +
Before: "Where is the end?"
 +
After: "Where is"
 +
Before: "Ha"
 +
After: "Haaaaaaa"
 +
 +
Errors:
 
1. bad alloc
 
1. bad alloc
 
2. bad alloc
 
2. bad alloc

Revision as of 13:00, 4 September 2014

 
 
 
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)

 
void resize( size_type count );
(1)
void resize( size_type count, CharT ch );
(2)

Resizes the string to contain count characters.

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

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

The first version initializes new characters to CharT(), the second version initializes new characters to ch.

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 <stdexcept>
 
int main()
{
    std::cout << "Basic functionality:" << std::endl;
    const unsigned  desired_length(8);
    std::string     long_string( "Where is the end?" );
    std::string     short_string( "Ha" );
 
    // Shorten
    std::cout << "Before: \"" << long_string << "\"" << std::endl;
    long_string.resize( desired_length );
    std::cout << "After: \"" << long_string <<  "\"" << std::endl;
 
    // Lengthen
    std::cout << "Before: \"" << short_string <<  "\"" << std::endl;
    short_string.resize( desired_length, 'a' );
    std::cout << "After: \"" << short_string <<  "\"" << std::endl;
 
    std::cout  << std::endl << "Errors:" << std::endl;
    {
        std::string s;    
 
        try {
            // size is OK, no length_error
            // (may throw bad_alloc)
            s.resize(s.max_size() - 1, 'x');
        } catch (std::bad_alloc&) {
            std::cout << "1. bad alloc\n";
        }
 
        try {
            // size is OK, no length_error
            // (may throw bad_alloc)
            s.resize(s.max_size(), 'x');
        } catch (std::bad_alloc& exc) {
            std::cout << "2. bad alloc\n";
        }
 
        try {
            // size is BAD, throw length_error
            s.resize(s.max_size() + 1, 'x');
        } catch (std::length_error&) {
            std::cout << "3. length error\n";
        }
     }
}

Possible output:

Basic functionality:
Before: "Where is the end?"
After: "Where is"
Before: "Ha"
After: "Haaaaaaa"
 
Errors:
1. bad alloc
2. bad alloc
3. length error

Complexity

Linear in the size of the string.

See also

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