[ Web Proxy ]
URL:
Viewing: http://ru.cppreference.com/cpp/string/basic_string/resize [Back]  [Original]

std::basic_string<CharT,Traits,Allocator>::resize cppreference.com
cppreference.com

std::basic_string<CharT,Traits,Allocator>::resize

cppreference.com
 
C++
(C++20)
(C++20)
(C++11)
(C++20)
/
(C++11)
(C++11)
(C++11)
(C++17)
 
 
std::basic_string
-
(C++17)
,
/
( C++20)( C++20)( C++20)( C++20)( C++20)(C++20)
(C++11)(C++11)(C++11)
(C++11)(C++11)
(C++11)(C++11)(C++11)
(C++11)
(C++11)
 
<tbody> </tbody> <tbody class="t-dcl-rev t-dcl-rev-num "> </tbody><tbody> </tbody> <tbody class="t-dcl-rev t-dcl-rev-num "> </tbody><tbody> </tbody>
(1)
void resize( size_type count );
( C++20)
constexpr void resize( size_type count );
( C++20)
(2)
void resize( size_type count, CharT ch );
( C++20)
constexpr void resize( size_type count, CharT ch );
( C++20)

, count .

count, :

1) CharT() ('\0', CharT char).
2) ch.

, count, count .

count
ch

()

std::length_error, count > max_size(). , Allocator.

- , ( ).

#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 << " :\n"
              << ":\n"
              << "1. : " << std::quoted(long_string) << '\n';
    long_string.resize(desired_length);
    std::cout << "2. : " << std::quoted(long_string) << '\n';
    
    std::cout << "    'a':\n"
              << "3. : " << std::quoted(short_string) << '\n';
    short_string.resize(desired_length, 'a');
    std::cout << "4. : " << std::quoted(short_string) << '\n';
    
    std::cout << "   char() == " << static_cast<int>(char()) << '\n'
              << "5. : " << std::quoted(short_string) << '\n';
    short_string.resize(desired_length + 3);
    std::cout << "6. : \"";
    for (char c : short_string)
        std::cout << (c == char() ? '@' : c);
    std::cout << "\"\n\n";
    
    std::cout << ":\n";
    std::string s;
    
    try
    {
        //  OK,  length_error
        // (  bad_alloc)
        s.resize(s.max_size() - 1, 'x');
    }
    catch (const std::bad_alloc& ex)
    {
        std::cout << "1. : " << ex.what() << '\n';
    }
    
    try
    {
        //  OK,  length_error
        // (  bad_alloc)
        s.resize(s.max_size(), 'x');
    }
    catch (const std::bad_alloc& ex)
    {
        std::cout << "2. : " << ex.what() << '\n';
    }
    
    try
    {
        //  ,  length_error
        s.resize(s.max_size() + 1, 'x');
    }
    catch (const std::length_error& ex)
    {
        std::cout << "3.  : " << ex.what() << '\n';
    }
}

:

 :
:
1. : "Where is the end?"
2. : "Where is"
    'a':
3. : "H"
4. : "Haaaaaaa"
   char() == 0
5. : "Haaaaaaa"
6. : "Haaaaaaa@@@"

:
1. : std::bad_alloc
2. : std::bad_alloc
3.  : basic_string::_M_replace_aux

C++:

LWG 847 C++98


(public -) []

(public -) []

(public -) []

Web Proxy Viewer  |  New URL  |  Original Page