list::list (STL Samples)

Viene illustrato come utilizzare elenco:: elenco Funzione della libreria di modelli standard (STL) in Visual C++.

explicit list(
   const A& Al = A( )
);
explicit list(
   size_type n,
   const T& v = T( ),
   const A& Al = A( )
);
list(
   const list& x
);
list(
   const_iterator First,
   const_iterator Last,
   const A& Al = A( )
);

Note

[!NOTA]

La classe/nomi di parametro nel prototipo non corrisponde alla versione nel file di intestazione.alcuni sono stati modificati per migliorare la leggibilità.

Il primo costruttore specifica una sequenza selezionata iniziale vuota.il secondo costruttore specifica una ripetizione di n elementi di valore x.Il terzo costruttore specifica una copia della sequenza controllata da x.l'ultimo costruttore specifica la sequenza [First, Last).Tutti i costruttori archiviano l'oggetto dell'allocatore Al, o per il costruttore di copia, il valore restituito di x.get_allocator, nel membro dati allocatore e inizializzare la sequenza selezionata.

Esempio

// list_list.cpp
// compile with: /EHsc
// Demonstrates the different constructors for list<T>

#pragma warning (disable:4786)
#include <list>
#include <string>
#include <iostream>

using namespace std ;

typedef list<string> LISTSTR;

// Try each of the four constructors
int main()
{
    LISTSTR::iterator i;
    LISTSTR test;                   // default constructor

    test.insert(test.end(), "one");
    test.insert(test.end(), "two");

    LISTSTR test2(test);            // construct from another list
    LISTSTR test3(3, "three");      // add several <T>'s
    LISTSTR test4(++test3.begin(),  // add part of another list
             test3.end());

    // Print them all out

    // one two
    cout << "test:";
    for (i =  test.begin(); i != test.end(); ++i)
        cout << " " << *i;
    cout << endl;

    // one two
    cout << "test:";
    for (i =  test2.begin(); i != test2.end(); ++i)
        cout << " " << *i;
    cout << endl;

    // three three three
    cout << "test:";
    for (i =  test3.begin(); i != test3.end(); ++i)
        cout << " " << *i;
    cout << endl;

    // three three
    cout << "test:";
    for (i =  test4.begin(); i != test4.end(); ++i)
        cout << " " << *i;
    cout << endl;
}

Output

test: one two
test: one two
test: three three three
test: three three

Requisiti

intestazione: <elenco>

Vedere anche

Concetti

Esempi di una libreria di modelli standard