std::forward_list::forward_list
Da cppreference.com.
forward_list(); |
(1) | |
explicit forward_list( const Allocator& alloc ); |
(2) | |
forward_list( size_type count, {{#pad:|12}} const T& value, {{#pad:|12}} const Allocator& alloc = Allocator()); |
(3) | (dal C++11) |
| (4) | ||
explicit forward_list( size_type count ); |
(dal C++11) (fino al c++14) |
|
explicit forward_list( size_type count, const Allocator& alloc = Allocator() ); |
(dal C++14) | |
template< class InputIt > forward_list( InputIt first, InputIt last, {{#pad:|12}} const Allocator& alloc = Allocator() ); |
(5) | |
forward_list( const forward_list& other ); |
(6) | |
forward_list( const forward_list& other, const Allocator& alloc ); |
(6) | (dal C++11) |
forward_list( forward_list&& other ); |
(7) | (dal C++11) |
forward_list( forward_list&& other, const Allocator& alloc ); |
(8) | (dal C++11) |
forward_list( std::initializer_list<T> init, {{#pad:|12}} const Allocator& alloc = Allocator() ); |
(9) | (dal C++11) |
Costruisce un nuovo container da una varietà di origini, eventualmente utilizzando un allocatore alloc fornito dall'utente.
1) Default constructor. Costruisce un container vuoto con un allocatore default-costruito.
2) Costruisce un container vuoto con l'allocatore
alloc.3) Costruisce il container con
count copie di elementi di valore value.5) Costruisce il container con il contenuto del range
[first, last).
Questo costruttore ha lo stesso effetto di forward_list(static_cast<size_type>(first), static_cast<value_type>(last), a) se InputIt è un integral type. |
(fino al c++11) |
Questi overload partecipano nella overload resolution solo se InputIt soddisfa LegacyInputIterator, per evitare ambiguità con l'overload (3). |
(dal C++11) |
6) Copy constructor. Costruisce il container con una copia del contenuto di
other. Se alloc non viene fornito, allocator viene ottenuto come se fosse stato invocato std::allocator_traits<allocator_type>::select_on_container_copy_construction(other.get_allocator()).7) Move constructor. Costruisce il container con il contenuto di
other utilizzando la move semantics. L'allocator è ottenuto per move-construction dall'allocator appartenente a other. 8) Allocator-extended move constructor. Utilizza
alloc come allocator per il nuovo container, muovendo il contenuto da other; se alloc != other.get_allocator(), questo si traduce in un move elemento per elemento. 9) Costruisce il container con il contenuto della initializer list
init. Parametri
| alloc | - | allocator da utilizzare per tutte le allocazioni di memoria di questo container |
| count | - | la dimensione del container |
| value | - | il valore con cui inizializzare gli elementi del container |
| first, last | - | il range da cui copiare gli elementi |
| other | - | un secondo container da utilizzare come sorgente per inizializzare gli elementi del nuovo container |
| init | - | initializer list per inizializzare gli elementi del container |
Complessità
1-2) Costante
3-4) Lineare in
count5) Lineare nella distanza fra
first e last6) Lineare nella dimensione di
other7) Costante.
8) Lineare se
alloc != other.get_allocator(), altrimenti costante.9) Lineare nella dimensione di
init.Eccezioni
Chiamate a Allocator::allocate possono lanciare eccezioni.
Note
Dopo la move construction del container (overload (7)), riferimenti, puntatori e iteratori (diversi da end) a other rimangono validi, ma si riferiscono a elementi che ora si trovano in *this. Lo standard corrente rende questa garanzia tramite la dichiarazione generale in [container.requirements.general]/12, ed una garanzia più diretta è in esame in LWG 2321.
Esempio
#include <forward_list>
#include <string>
#include <iostream>
template<typename T>
std::ostream& operator<<(std::ostream& s, const std::forward_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 initializer list syntax:
std::forward_list<std::string> words1 {"the", "frogurt", "is", "also", "cursed"};
std::cout << "words1: " << words1 << '\n';
// words2 == words1
std::forward_list<std::string> words2(words1.begin(), words1.end());
std::cout << "words2: " << words2 << '\n';
// words3 == words1
std::forward_list<std::string> words3(words1);
std::cout << "words3: " << words3 << '\n';
// words4 is {"Mo", "Mo", "Mo", "Mo", "Mo"}
std::forward_list<std::string> words4(5, "Mo");
std::cout << "words4: " << words4 << '\n';
}
Output:
words1: [the, frogurt, is, also, cursed]
words2: [the, frogurt, is, also, cursed]
words3: [the, frogurt, is, also, cursed]
words4: [Mo, Mo, Mo, Mo, Mo]
Defect reports
Template:dr list begin Template:dr list item Template:dr list end
See also
assegna valori al contenitore Original: assigns values to the container The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (metodo pubblico) | |
assegna valori al contenitore Original: assigns values to the container The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (metodo pubblico) | |