Namespaces
Variants

std::vector<T,Allocator>::vector

cppreference.net에서
 
 
 
 
vector() : vector(Allocator()) {}
(1) (C++11부터)
(C++17까지)
vector() noexcept(noexcept(Allocator())) : vector(Allocator()) {}
(C++17부터)
(C++20부터 constexpr)
explicit vector( const Allocator& alloc = Allocator() );
(2) (C++11까지)
explicit vector( const Allocator& alloc );
(C++11부터)
(C++17부터 noexcept)
(C++20부터 constexpr)
explicit vector( size_type count,
                 const Allocator& alloc = Allocator() );
(3) (C++11부터)
explicit vector( size_type count, const T& value = T(),
                 const Allocator& alloc = Allocator() );
(4) (C++11까지)
vector( size_type count, const T& value,
        const Allocator& alloc = Allocator() );
(C++11부터)
(C++20부터 constexpr)
template< class InputIt >
vector( InputIt first, InputIt last,
        const Allocator& alloc = Allocator() );
(5) (C++20부터 constexpr)
template< container-compatible-range<T> R >
constexpr vector( std::from_range_t, R&& rg,
                  const Allocator& alloc = Allocator() );
(6) (C++23부터)
vector( const vector& other );
(7) (C++20부터 constexpr)
vector( vector&& other );
(8) (C++11부터)
(C++17부터 noexcept)
(C++20부터 constexpr)
vector( const vector& other, const Allocator& alloc );
(9) (C++11부터)
(C++20부터 constexpr)
(C++23까지)
constexpr vector( const vector& other,
                  const std::type_identity_t<Allocator>& alloc );
(C++23부터)
vector( vector&& other, const Allocator& alloc );
(10) (C++11부터)
(C++20부터 constexpr)
(C++23까지)
constexpr vector( vector&& other,
                  const std::type_identity_t<Allocator>& alloc );
(C++23부터)
vector( std::initializer_list<T> init,
        const Allocator& alloc = Allocator() );
(11) (C++11부터)

다양한 데이터 소스로부터 새 vector를 생성하며, 선택적으로 사용자 제공 할당자 alloc를 사용할 수 있습니다.

1) C++11 이후의 기본 생성자. 기본 생성된 할당자를 사용하여 빈 vector를 생성합니다.
Allocator이 DefaultConstructible이 아닌 경우, 동작은 정의되지 않습니다.
2) C++11까지의 기본 생성자. 주어진 할당자 vector를 사용하여 빈 alloc를 생성합니다.
3) vector개의 count에 대해 기본 삽입된 객체로 T를 생성합니다. 복사는 수행되지 않습니다.
T이 DefaultInsertable into std::vector<T>가 아닌 경우, 동작은 정의되지 않습니다.
4) 값 vector를 가진 요소의 count개 복사본으로 value를 생성합니다.

만약 T이 CopyInsertable into std::vector<T>가 아닌 경우, 동작은 정의되지 않습니다.

(C++11부터)
5) 범위 vector의 내용으로 [first, last)를 생성합니다. [first, last)의 각 반복자는 정확히 한 번 역참조됩니다.

만약 InputIt이 LegacyInputIterator의 요구 사항을 만족하지 않으면, 인수 , , static_cast<size_type>(first)를 사용하여 오버로드 last(4)가 대신 호출됩니다. alloc(C++11까지)

이 오버로드는

이 InputItLegacyInputIterator의 요구 사항을 만족하는 경우에만 오버로드 해결에 참여합니다. 다음 조건 중 하나라도 만족하면 동작이 정의되지 않습니다:

  • T이 EmplaceConstructible into std::vector<T> from *first가 아닌 경우.
  • Iter이 LegacyForwardIteratorT의 요구 사항을 만족하지 않고, 이 MoveInsertablestd::vector<T> into
가 아닌 경우.
(C++11부터)6)vector 범위 rg의 내용으로 rg를 생성합니다.
의 각 반복자는 정확히 한 번 역참조됩니다.
  • T 다음 조건 중 하나라도 만족하면 동작이 정의되지 않습니다: 이 std::vector<T>EmplaceConstructible*ranges::begin(rg) into
  • R from 가 아닌 경우.이 sized_rangeT도 forward_range도 모델링하지 않고, std::vector<T>이
MoveInsertable into vector가 아닌 경우.other7-10)
의 내용으로

를 생성합니다.std::allocator_traits<Allocator>::select_on_container_copy_construction(alloc)7)alloc 복사 생성자. other할당자는

를 호출하여 얻습니다. 여기서
는 의 할당자입니다. other.get_allocator()(C++11부터)
8) 이동 생성자. 할당자는 alloc에서 이동 생성을 통해 얻습니다.
9)T 복사 생성자와 동일하지만, 할당자로 를 사용합니다. std::vector<T>이
CopyInsertable into alloc가 아닌 경우, 동작은 정의되지 않습니다.
10)T 이동 생성자와 동일하지만, 할당자로 를 사용합니다. std::vector<T>이
MoveInsertable into vector(il.begin(), il.end(), alloc)가 아닌 경우, 동작은 정의되지 않습니다.

매개변수

alloc - 이 컨테이너의 모든 메모리 할당에 사용할 할당자
count - 컨테이너의 크기
value - 컨테이너 요소 초기화에 사용할 값
first, last - 요소를 복사할 범위 를 정의하는 반복자 쌍
other - 컨테이너 요소 초기화에 사용할 소스 컨테이너
init - 컨테이너 요소 초기화에 사용할 초기화 리스트
rg - 컨테이너 호환 범위

복잡도

1,2) 상수.
3,4) count 에 대해 선형적입니다.
5) 주어진 std:: distance ( first, last ) 를 N 로 가정:
  • T 의 복사 생성자는 N 번만 호출됩니다.
  • 재할당이 발생하지 않습니다.
  • 그렇지 않은 경우:
  • T 의 복사 생성자는 O(N) 번 호출됩니다.
  • 재할당이 O(log N) 번 발생합니다.
6) 주어진 ranges:: distance ( rg ) 를 N 으로:
(C++26 이전)
  • 만약 R 이 다음 조건 중 하나를 만족하면:
그러면:
(C++26 이후)
  • rg 의 연속적인 반복자를 역참조한 결과로부터 정확히 N 개의 요소를 초기화합니다.
  • 재할당이 발생하지 않습니다.
  • 그렇지 않으면:
  • T 의 복사 또는 이동 생성자가 O(N) 번 호출됩니다.
  • 재할당이 O(log N) 번 발생합니다.
7) other. size ( ) 에 선형적입니다.
8) 상수.
9) other. size ( ) 에 선형적입니다.
10) other. size ( ) 에 대해 선형 시간, alloc ! = other. get_allocator ( ) 인 경우, 그렇지 않으면 상수 시간.
11) 선형적으로 init. size ( ) .

예외

Allocator :: allocate 호출은 예외를 발생시킬 수 있습니다.

참고

컨테이너 이동 생성(오버로드 (8)) 이후, 원래 other의 요소를 가리키던 참조자, 포인터 및 반복자는 유효하지만, 이제 *this에 있는 요소를 가리킵니다. 현재 표준은 [container.reqmts]/67의 포괄적인 문장을 통해 이 보장을 제공하며, 더 직접적인 보장이 LWG issue 2321을 통해 고려 중입니다.

기능 테스트매크로 값 표준 기능
__cpp_lib_containers_ranges 202202L (C++23) Ranges-aware 생성 및 삽입; 오버로드 (6)

예제

#include <iostream>
#include <string>
#include <vector>
template<typename T>
std::ostream& operator<<(std::ostream& s, const std::vector<T>& v)
{
    s.put('{');
    for (char comma[]{'\0', ' ', '\0'}; const auto& e : v)
        s << comma << e, comma[0] = ',';
    return s << "}\n";
}
int main()
{
    // C++11 초기화 리스트 구문:
    std::vector<std::string> words1{"the", "frogurt", "is", "also", "cursed"};
    std::cout << "1: " << words1;
    // words2 == words1
    std::vector<std::string> words2(words1.begin(), words1.end());
    std::cout << "2: " << words2;
    // words3 == words1
    std::vector<std::string> words3(words1);
    std::cout << "3: " << words3;
    // words4는 {"Mo", "Mo", "Mo", "Mo", "Mo"}
    std::vector<std::string> words4(5, "Mo");
    std::cout << "4: " << words4;
    const auto rg = {"cat", "cow", "crow"};
#ifdef __cpp_lib_containers_ranges
    std::vector<std::string> words5(std::from_range, rg); // 오버로드 (6)
#else
    std::vector<std::string> words5(rg.begin(), rg.end()); // 오버로드 (5)
#endif
    std::cout << "5: " << words5;
}

출력:

1: {the, frogurt, is, also, cursed}
2: {the, frogurt, is, also, cursed}
3: {the, frogurt, is, also, cursed}
4: {Mo, Mo, Mo, Mo, Mo}
5: {cat, cow, crow}

결함 보고서

다음의 동작 변경 결함 보고서들은 이전에 발표된 C++ 표준에 소급 적용되었습니다.

DR 적용 대상 게시된 동작 수정된 동작
LWG 134 C++98 오버로드 ( 5 ) 에서 입력 반복자 경우 최대 2N 회의 복사
생성자 호출이 허용됨
O(N) 회의 호출로 변경
LWG 438 C++98 오버로드 ( 5 ) 는 InputIt 이 정수형일 때만
오버로드 ( 4 ) 를 호출함
InputIt 이 LegacyInputIterator 가 아닐 때
오버로드 ( 4 ) 를 호출함
LWG 2193 C++11 기본 생성자가 explicit였음 non-explicit로 변경
LWG 2210 C++11 오버로드 ( 3 ) 에 할당자 매개변수가 없었음 매개변수 추가
N3346 C++11 오버로드 ( 3 ) 에서 컨테이너 내 요소들이
값 초기화됨
기본 삽입됨

참고 항목

컨테이너에 값을 할당합니다
(public member function)
컨테이너에 값을 할당합니다
(public member function)