std::list::list
Z cppreference.com
| (1) | ||
explicit list( const Allocator& alloc = Allocator() ); |
||
| (2) | ||
explicit list( size_type count, {{#pad:|4}} const T& value = T(), {{#pad:|4}} const Allocator& alloc = Allocator()); |
(do C++11) | |
list( size_type count, {{#pad:|4}} const T& value, {{#pad:|4}} const Allocator& alloc = Allocator()); |
(od C++11) | |
| (3) | ||
explicit list( size_type count ); |
(od C++11) | |
template< class InputIt > list( InputIt first, InputIt last, {{#pad:|4}} const Allocator& alloc = Allocator() ); |
(4) | |
list( const list& other ); |
(5) | |
list( const list& other, const Allocator& alloc ); |
(5) | (od C++11) |
list( list&& other ); |
(6) | (od C++11) |
list( list&& other, const Allocator& alloc ); |
(7) | (od C++11) |
list( std::initializer_list<T> init, {{#pad:|4}} 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.
1) Domyślny konstruktor. Konstruuje pusty kontener.
2) Konstruuje kontener z count kopiami elementów o wartości value.
3) Konstruuje kontener z count wstawionymi z wartością domyślną(ang) instancjami T. Nie wykonuje żadnego kopiowania.
4) Konstruuje kontener z zawartością przedziału [first, last).
Ten konstruktor ma identyczne działanie, jak list(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.
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. 8) Konstruktor z listy inicjalizacyjnej. Konstruuje kontener z zawartością listy inicjalizacyjnej init.
| Ta sekcja jest niedokończona.
Powód: wymaga tłumaczenia |
Spis treści
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.
Przykład
#include <list>
#include <string>
#include <iostream>
//Wypisuje zawartość kontenera
template<typename T>
std::ostream& operator<<(std::ostream& s, const std::list<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::list<std::string> words1 {"the", "frogurt", "is", "also", "cursed"};
std::cout << "words1: " << words1 << '\n';
// words2 == words1, konstruktor inicjalizujący z przedziału
std::list<std::string> words2(words1.begin(), words1.end());
std::cout << "words2: " << words2 << '\n';
// words3 == words1, konstruktor kopiujący
std::list<std::string> words3(words1);
std::cout << "words3: " << words3 << '\n';
// words4 "==" {"Mo", "Mo", "Mo", "Mo", "Mo"}
std::list<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) |