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) | ||
|
|
||
| (2) | ||
|
|
(do C++11) | |
|
|
(od C++11) | |
| (3) | ||
|
|
(od C++11) | |
|
|
(4) | |
|
|
(5) | |
|
|
(5) | (od C++11) |
| (6) | ||
|
|
(od C++11) | |
|
|
(7) | (od C++11) |
|
|
(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
countkopiami 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,
otheris guaranteed to be empty().
7) Allocator-extended move constructor. Using
allocas 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,
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
count4) Liniowa względem odległości między
firsta
last5) Liniowa względem rozmiaru
other6) 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]