front_insert_iterator — Klasa

Opisuje adapter iteratora, który spełnia wymagania iteratora danych wyjściowych. Wstawia, a nie zastępuje, elementy przed sekwencją. W ten sposób zapewnia semantykę, która różni się od semantyki zastępowania udostępnianych przez iteratory kontenerów sekwencji języka C++. Klasa front_insert_iterator jest templatized na typ kontenera.

Składnia

template <class Container>
class front_insert_iterator;

Parametry

kontener
Typ kontenera na przedniej części, które elementy mają zostać wstawione przez element front_insert_iterator.

Uwagi

Kontener musi spełniać wymagania dla sekwencji wstawiania na przód, gdzie jest możliwe wstawianie elementów na początek sekwencji w amortyzowanym stałym czasie. Kontenery sekwencji biblioteki standardowej języka C++ zdefiniowane przez klasę deque i klasę listy zapewniają wymaganą push_front funkcję składową i spełniają te wymagania. Natomiast kontenery sekwencji zdefiniowane przez klasę wektorów nie spełniają tych wymagań i nie można ich dostosować do użycia z elementami front_insert_iterator. Element front_insert_iterator musi być zawsze inicjowany przy użyciu kontenera.

Konstruktory

Konstruktor opis
front_insert_iterator Tworzy iterator, który może wstawić elementy z przodu określonego obiektu kontenera.

Typedefs

Nazwa typu opis
container_type Typ, który reprezentuje kontener, w którym ma być przeprowadzone wstawienie na przód.
Odwołanie Typ, który zawiera odwołanie do elementu w sekwencji kontrolowanej przez skojarzony kontener.

Operatory

Operator opis
Operator* Operator dereferencing używany do implementowania wyrażenia iteratora wyjściowego * i = x dla wstawiania frontu.
operator++ Zwiększa front_insert_iterator wartość do następnej lokalizacji, w której może być przechowywana wartość.
operator = Operator przypisania używany do implementowania wyrażenia iteratora wyjściowego * i = x dla wstawiania z przodu.

Wymagania

Nagłówek: <iterator>

Przestrzeń nazw: std

front_insert_iterator::container_type

Typ, który reprezentuje kontener, w którym ma być przeprowadzone wstawienie na przód.

typedef Container container_type;

Uwagi

Typ jest synonimem parametru szablonu Container.

Przykład

// front_insert_iterator_container_type.cpp
// compile with: /EHsc
#include <iterator>
#include <list>
#include <iostream>

int main( )
{
   using namespace std;

   list<int> L1;
   front_insert_iterator<list<int> >::container_type L2 = L1;
   front_inserter ( L2 ) = 20;
   front_inserter ( L2 ) = 10;
   front_inserter ( L2 ) = 40;

   list <int>::iterator vIter;
   cout << "The list L2 is: ( ";
   for ( vIter = L2.begin ( ) ; vIter != L2.end ( ); vIter++)
      cout << *vIter << " ";
   cout << ")." << endl;
}
/* Output:
The list L2 is: ( 40 10 20 ).
*/

front_insert_iterator::front_insert_iterator

Tworzy iterator, który może wstawić elementy z przodu określonego obiektu kontenera.

explicit front_insert_iterator(Container& _Cont);

Parametry

_Cd
Obiekt kontenera front_insert_iterator , do którego należy wstawić elementy.

Wartość zwracana

Wartość front_insert_iterator dla obiektu kontenera parametrów.

Przykład

// front_insert_iterator_front_insert_iterator.cpp
// compile with: /EHsc
#include <iterator>
#include <list>
#include <iostream>

int main( )
{
   using namespace std;
   int i;
   list <int>::iterator L_Iter;

   list<int> L;
   for (i = -1 ; i < 9 ; ++i )
   {
      L.push_back ( 2 * i );
   }

   cout << "The list L is:\n ( ";
   for ( L_Iter = L.begin( ) ; L_Iter != L.end( ); L_Iter++)
      cout << *L_Iter << " ";
   cout << ")." << endl;

   // Using the member function to insert an element
   front_inserter ( L ) = 20;

   // Alternatively, one may use the template function
   front_insert_iterator< list < int> > Iter(L);
*Iter = 30;

   cout << "After the front insertions, the list L is:\n ( ";
   for ( L_Iter = L.begin( ) ; L_Iter != L.end( ); L_Iter++)
      cout << *L_Iter << " ";
   cout << ")." << endl;
}
/* Output:
The list L is:
( -2 0 2 4 6 8 10 12 14 16 ).
After the front insertions, the list L is:
( 30 20 -2 0 2 4 6 8 10 12 14 16 ).
*/

front_insert_iterator::operator*

Dereferences iterator insert zwraca element, który adresuje.

front_insert_iterator<Container>& operator*();

Wartość zwracana

Funkcja składowa zwraca wartość zaadresowanego elementu.

Uwagi

Służy do implementowania wyrażenia iteratora wyjściowego *wartość iteratora = . Jeśli Iter jest iteratorem, który adresuje element w sekwencji, wartość *Iter = zastępuje ten element wartością i nie zmienia całkowitej liczby elementów w sekwencji.

Przykład

// front_insert_iterator_deref.cpp
// compile with: /EHsc
#include <iterator>
#include <list>
#include <iostream>

int main( )
{
   using namespace std;
   int i;
   list <int>::iterator L_Iter;

   list<int> L;
   for ( i = -1 ; i < 9 ; ++i )
   {
      L.push_back ( 2 * i );
   }

   cout << "The list L is:\n ( ";
   for ( L_Iter = L.begin( ) ; L_Iter != L.end( ); L_Iter++)
      cout << *L_Iter << " ";
   cout << ")." << endl;

   front_insert_iterator< list < int> > Iter(L);
*Iter = 20;

   // Alternatively, you may use
   front_inserter ( L ) = 30;

   cout << "After the front insertions, the list L is:\n ( ";
   for ( L_Iter = L.begin( ) ; L_Iter != L.end( ); L_Iter++)
      cout << *L_Iter << " ";
   cout << ")." << endl;
}
/* Output:
The list L is:
( -2 0 2 4 6 8 10 12 14 16 ).
After the front insertions, the list L is:
( 30 20 -2 0 2 4 6 8 10 12 14 16 ).
*/

front_insert_iterator::operator++

Zwiększa back_insert_iterator wartość do następnej lokalizacji, w której może być przechowywana wartość.

front_insert_iterator<Container>& operator++();

front_insert_iterator<Container> operator++(int);

Wartość zwracana

Adresowanie front_insert_iterator następnej lokalizacji, w której może być przechowywana wartość.

Uwagi

Oba operatory preinkrementacji i postincrementation zwracają ten sam wynik.

Przykład

// front_insert_iterator_op_incre.cpp
// compile with: /EHsc
#include <iterator>
#include <list>
#include <iostream>

int main( )
{
   using namespace std;

   list<int> L1;
   front_insert_iterator<list<int> > iter ( L1 );
*iter = 10;
   iter++;
*iter = 20;
   iter++;
*iter = 30;
   iter++;

   list <int>::iterator vIter;
   cout << "The list L1 is: ( ";
   for ( vIter = L1.begin ( ) ; vIter != L1.end ( ); vIter++ )
      cout << *vIter << " ";
   cout << ")." << endl;
}
/* Output:
The list L1 is: ( 30 20 10 ).
*/

front_insert_iterator::operator=

Dołącza (wypycha) wartość z przodu kontenera.

front_insert_iterator<Container>& operator=(typename Container::const_reference val);

front_insert_iterator<Container>& operator=(typename Container::value_type&& val);

Parametry

Val
Wartość, która ma zostać przypisana do kontenera.

Wartość zwracana

Odwołanie do ostatniego elementu wstawionego z przodu kontenera.

Uwagi

Pierwszy operator elementu członkowskiego oblicza container.push_front( val)wartość , a następnie zwraca wartość *this.

Drugi operator elementu członkowskiego ocenia

container->push_front((typename Container::value_type&&) val),

następnie zwraca wartość *this.

Przykład

// front_insert_iterator_op_assign.cpp
// compile with: /EHsc
#include <iterator>
#include <list>
#include <iostream>

int main( )
{
   using namespace std;

   list<int> L1;
   front_insert_iterator<list<int> > iter ( L1 );
*iter = 10;
   iter++;
*iter = 20;
   iter++;
*iter = 30;
   iter++;

   list <int>::iterator vIter;
   cout << "The list L1 is: ( ";
   for ( vIter = L1.begin ( ) ; vIter != L1.end ( ); vIter++ )
      cout << *vIter << " ";
   cout << ")." << endl;
}
/* Output:
The list L1 is: ( 30 20 10 ).
*/

front_insert_iterator::reference

Typ, który zawiera odwołanie do elementu w sekwencji kontrolowanej przez skojarzony kontener.

typedef typename Container::reference reference;

Przykład

// front_insert_iterator_reference.cpp
// compile with: /EHsc
#include <iterator>
#include <list>
#include <iostream>

int main( )
{
   using namespace std;

   list<int> L;
   front_insert_iterator<list<int> > fiivIter( L );
*fiivIter = 10;
*fiivIter = 20;
*fiivIter = 30;

   list<int>::iterator LIter;
   cout << "The list L is: ( ";
   for ( LIter = L.begin ( ) ; LIter != L.end ( ); LIter++)
      cout << *LIter << " ";
   cout << ")." << endl;

   front_insert_iterator<list<int> >::reference
        RefFirst = *(L.begin ( ));
   cout << "The first element in the list L is: "
        << RefFirst << "." << endl;
}
/* Output:
The list L is: ( 30 20 10 ).
The first element in the list L is: 30.
*/

Zobacz też

<Sterująca>
Bezpieczeństwo wątku w standardowej bibliotece C++
Dokumentacja standardowej biblioteki C++