std::vector::vector
| (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.
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) |
std::allocator_traits<allocator_type>::select_on_container_copy_construction(other.get_allocator()).alloc != other.get_allocator(), this results in an element-wise move. (in that case, other is not guaranteed to be empty after the move)| Ta sekcja jest niedokończona.
Powód: wymaga tłumaczenia |
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ść
alloc != other.get_allocator(), w przeciwnym wypadku stała.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.
| Ta sekcja jest niedokończona.
Powód: wymaga tłumaczenia |
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
| przypisuje wartości do kontenera (publiczna metoda) | |
| przypisuje wartości do kontenera (publiczna metoda) |