◐ Shell
clean mode source ↗

std::vector::vector – cppreference.com

Z cppreference.com

<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> <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)

explicit vector( const Allocator& alloc = Allocator() );

(2)

explicit vector( size_type count, {{#pad:|6}} const T& value = T(), {{#pad:|6}} const Allocator& alloc = Allocator());

(do C++11)

         vector( size_type count, {{#pad:|6}} const T& value, {{#pad:|6}} const Allocator& alloc = Allocator());

(od C++11)
(3)

explicit vector( size_type count );

(od C++11)

template< class InputIt > vector( InputIt first, InputIt last, {{#pad:|6}} const Allocator& alloc = Allocator() );

(4)

vector( const vector& other );

(5)

vector( const vector& other, const Allocator& alloc );

(5) (od C++11)
(6)

vector( vector&& other );

(od C++11)

vector( vector&& other, const Allocator& alloc );

(7) (od C++11)

vector( std::initializer_list<T> init, {{#pad:|6}} const Allocator& alloc = Allocator() );

(8) (od C++11)

Konstruuje nowy kontener z różnych źródeł danych, opcjonalnie wykorzystując dostarczony przez użytkownika alokator alloc.

2) Konstruuje kontener z

count

kopiami elementów o wartości

value

.

4) Konstruuje kontener z zawartością przedziału

[first, last)

.

Ten konstruktor ma identyczne działanie, jak vector(static_cast<size_type>(first), static_cast<value_type>(last), a) jeśli InputIt jest typem całkowitym. (do C++11)
To przeciążenie bierze udział w rozwiązywaniu przeciążeń(ang) tylko jeśli InputIt spełnia wymogi InputIterator, aby uniknąć niejednoznaczności z przeciążeniem (2). (od C++11)

5) Konstruktor kopiujący. Konstruuje kontener z kopią zawartości

other

. If

alloc

is not provided, allocator is obtained as if by calling std::allocator_traits<allocator_type>::select_on_container_copy_construction(other.get_allocator()).

6) Konstruktor przenoszący. Konstruuje kontener przenosząc zawartość

other

, using move semantics. Allocator is obtained by move-construction from the allocator belonging to

other

. After the move,

other

is guaranteed to be empty().

7) Allocator-extended move constructor. Using

alloc

as the allocator for the new container, moving the contents from

other

; if alloc != other.get_allocator(), this results in an element-wise move. (in that case,

other

is not guaranteed to be empty after the move)

8) Konstruktor z listy inicjalizacyjnej. Konstruuje kontener z zawartością listy inicjalizacyjnej

init

.

Parametry

alloc - alokator używany do wszystkich alokacji pamięci wykonywanych przez ten kontener
count - rozmiar kontenera
value - wartość, którą zostaną zainicjalizowane elementy kontenera
first, last - przedział, z którego zostaną skopiowane elementy
other - inny kontener, wykorzystywany jako źródło, z którego inicjalizowane są elementy kontenera
init - lista inicjalizacyjna, do zainicjowania wartości elementów kontenera

Złożoność

1) Stała

2-3) Liniowa względem

count

4) Liniowa względem odległości między

first

a

last

5) Liniowa względem rozmiaru

other

6) Stała.

7) Liniowa, jeśli alloc != other.get_allocator(), w przeciwnym wypadku stała.

8) Liniowa względem rozmiaru

init

.

Wyjątki

Wywołania Allocator::allocate mogą wyrzucić wyjątki.

Notka

Po skonstruowaniu kontenera przez przeniesienie (przeciążenie (6)), referencje, wskaźniki i iteratory (inne niż "past-the-end") do other pozostają prawidłowe, ale wskazują na elementy znajdujące się teraz w *this. Obecny standard gwarantuje to przez oświadczenie zbiorcze w §23.2.1[container.requirements.general]/12, i bardziej bezpośrednia gwarancja jest wzięta pod uwagę: LWG 2321.
The overload (3) zeroes out elements of non-class types such as int, which is different from the behavior of new[], which leaves them uninitialized. To match the behavior of new[], a custom Allocator::construct can be provided which leaves such elements uninitialized.

Przykład

#include <vector>
#include <string>
#include <iostream>

//Wypisuje zawartość kontenera
template<typename T>
std::ostream& operator<<(std::ostream& s, const std::vector<T>& v) {
    s.put('[');
    char comma[3] = {'\0', ' ', '\0'};
    for (const auto& e : v) {
        s << comma << e;
        comma[0] = ',';
    }
    return s << ']';
}

int main() 
{
    // c++11 składnia listy inicjalizacyjnej:
    std::vector<std::string> words1 {"the", "frogurt", "is", "also", "cursed"};
    std::cout << "words1: " << words1 << '\n';

    // words2 == words1, konstruktor inicjalizujący z przedziału
    std::vector<std::string> words2(words1.begin(), words1.end());
    std::cout << "words2: " << words2 << '\n';

    // words3 == words1, konstruktor kopiujący
    std::vector<std::string> words3(words1);
    std::cout << "words3: " << words3 << '\n';

    // words4 "==" {"Mo", "Mo", "Mo", "Mo", "Mo"}
    std::vector<std::string> words4(5, "Mo");
    std::cout << "words4: " << words4 << '\n';
}

Wynik:

words1: [the, frogurt, is, also, cursed]
words2: [the, frogurt, is, also, cursed]
words3: [the, frogurt, is, also, cursed]
words4: [Mo, Mo, Mo, Mo, Mo]

Zobacz także