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

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

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

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>
(1)
void reserve( size_type new_cap = 0 );
( C++20)
constexpr void reserve( size_type new_cap );
( C++20)
void reserve();
(2) ( C++20)
()
1) std::basic_string , .
( C++20)
( C++20)
, , , .

new_cap

()

std::length_error, new_cap , max_size().

, std::allocator_traits<Allocator>::allocate(), std::bad_alloc.

- , ( ).

size() .

#include <cassert>
#include <iostream>
#include <string>

int main()
{
    std::string s;
    const std::string::size_type new_capacity{100u};
    std::cout << ": " << s.capacity() << '\n';
    
    s.reserve(new_capacity);
    std::cout << ":  " << s.capacity() << '\n';
    assert(new_capacity <= s.capacity());
    
    //    
    auto cap{s.capacity()};
    for (int check{}; check != 4; ++check)
    {
        while (cap == s.capacity())
            s += '$';
        cap = s.capacity();
        std::cout << " : " << cap << '\n';
    }
    
    // s.reserve(); //    C++20, :
    s.shrink_to_fit();
    std::cout << ":  " << s.capacity() << '\n';
}

:

: 15
:  100
 : 200
 : 400
 : 800
 : 1600
:  801

C++:

LWG 847 C++98

,
(public -) []

(public -) []

Web Proxy Viewer  |  New URL  |  Original Page