Namespaces
Variants

std::inplace_vector<T,N>::resize

cppreference.net에서 가져옴

 
 
 
 
constexpr void resize( size_type count );
(1) (C++26부터)
constexpr void resize( size_type count, const value_type& value );
(2) (C++26부터)

컨테이너의 크기를 count개 요소로 조정합니다. count == size()이면 아무 일도 하지 않습니다.

현재 크기가 count보다 크면 컨테이너는 처음 count개 요소로 축소됩니다.

현재 크기가 count보다 작으면 다음이 수행됩니다:

1) 추가 기본 삽입 요소들이 추가됩니다.
2) 추가 value의 복사본들이 추가됩니다.

매개변수

count - 컨테이너의 새로운 크기
value - 새로운 요소들을 초기화할 값
타입 요구사항
-
다음 조건이 만족될 경우 동작은 정의되지 않음:
1) T 가 DefaultInsertable 이 아닌 경우 inplace_vector 에.
2) T 가 CopyInsertable 이 아닌 경우 inplace_vector 에.

복잡도

현재 크기와 count 사이의 차이에 선형적으로 비례합니다.

예외

1,2) std::bad_alloc 이 발생하면 count > N 입니다.

어떤 이유로든 예외가 발생하면, 이 함수들은 아무런 효과를 가지지 않습니다( strong exception safety guarantee ).

예제

#include <inplace_vector>
#include <print>
int main()
{
    std::inplace_vector<int, 6> v(6, 9);
    std::println("Initially, v = {}", v);
    v.resize(2);
    std::println("After resize(2), v = {}", v);
    v.resize(4);
    std::println("After resize(4), v = {}", v);
    v.resize(6, -1);
    std::println("After resize(6, -1), v = {}", v);
    try
    {
        std::print("Trying resize(13): ");
        v.resize(13); // throws, because count > N; v is left unchanged
    }
    catch(const std::bad_alloc& ex)
    {
        std::println("ex.what(): {}", ex.what());
    }
    std::println("After exception, v = {}", v);
}

가능한 출력:

Initially, v = [9, 9, 9, 9, 9, 9]
After resize(2), v = [9, 9]
After resize(4), v = [9, 9, 0, 0]
After resize(6, -1), v = [9, 9, 0, 0, -1, -1]
Trying resize(13): ex.what(): std::bad_alloc
After exception, v = [9, 9, 0, 0, -1, -1]


참고 항목

[static]
가능한 최대 원소 개수를 반환합니다
(public static member function)
원소 개수를 반환합니다
(public member function)
[static]
현재 할당된 저장 공간에 보관할 수 있는 원소 개수를 반환합니다
(public static member function)
컨테이너가 비어 있는지 확인합니다
(public member function)