Sdílet prostřednictvím


front_insert_iterator – třída

Popisuje adaptér iterátoru, který splňuje požadavky výstupního iterátoru. Vloží prvky místo přepsání do přední části sekvence. Poskytuje tedy sémantiku, která se liší od sémantiky přepsání poskytované iterátory kontejnerů sekvence jazyka C++. Třída front_insert_iterator je templatizována na typ kontejneru.

Syntaxe

template <class Container>
class front_insert_iterator;

Parametry

Kontejner
Typ kontejneru do přední části, které prvky mají být vloženy pomocí front_insert_iterator.

Poznámky

Kontejner musí splňovat požadavky pro sekvenci vložení do přední části, je-li možné vložit prvky na začátek sekvence v amortizovaném konstantním času. Kontejnery sekvence standardní knihovny C++ definované deque Class a list Class poskytují potřebnou push_front členská funkce a splňují tyto požadavky. Naproti tomu kontejnery sekvence definované třídou vektoru nesplňují tyto požadavky a nelze je přizpůsobit pro použití s front_insert_iterator. Musí front_insert_iterator být vždy inicializován pomocí svého kontejneru.

Konstruktory

Konstruktor Popis
front_insert_iterator Vytvoří iterátor, který může vložit prvky do přední části zadaného objektu kontejneru.

Typedefs

Název typu Popis
container_type Typ, který představuje kontejner, do jehož přední části má být vložení provedeno.
odkaz Typ, který poskytuje odkaz na prvek v sekvenci řízené přiřazeným kontejnerem.

Operátory

Operátor Popis
operátor* Operátor dereferencingu použitý k implementaci výstupního výrazu iterátoru * i = x pro přední vložení.
operator++ Zvýší front_insert_iterator hodnotu na další místo, do kterého může být uložena hodnota.
operator= Operátor přiřazení použitý k implementaci výrazu výstupního iterátoru * i = x pro přední vložení.

Požadavky

Záhlaví: <iterátor>

Obor názvů: std

front_insert_iterator::container_type

Typ, který představuje kontejner, do jehož přední části má být vložení provedeno.

typedef Container container_type;

Poznámky

Typ je synonymem parametru šablony Container.

Příklad

// 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

Vytvoří iterátor, který může vložit prvky do přední části zadaného objektu kontejneru.

explicit front_insert_iterator(Container& _Cont);

Parametry

_Pokrač
Objekt kontejneru front_insert_iterator , do kterého se mají vložit prvky.

Návratová hodnota

A front_insert_iterator pro objekt kontejneru parametru.

Příklad

// 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 insert iterator returning the element it iterence.

front_insert_iterator<Container>& operator*();

Návratová hodnota

Členová funkce vrátí hodnotu prvku adresovaného.

Poznámky

Používá se k implementaci výstupního výrazu iterátoru *Hodnota Iteru = . Pokud Iter je iterátor, který řeší prvek v posloupnosti, *Iter = hodnota nahradí tento prvek hodnotou a nezmění celkový počet prvků v sekvenci.

Příklad

// 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++

Zvýší back_insert_iterator hodnotu na další místo, do kterého může být uložena hodnota.

front_insert_iterator<Container>& operator++();

front_insert_iterator<Container> operator++(int);

Návratová hodnota

Adresování front_insert_iterator dalšího umístění, do kterého může být uložena hodnota.

Poznámky

Operátory preinkrementace i postinkrementace vrátí stejný výsledek.

Příklad

// 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=

Připojí (nasdílí) hodnotu na přední straně kontejneru.

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

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

Parametry

Val
Hodnota, která se má přiřadit ke kontejneru.

Návratová hodnota

Odkaz na poslední prvek vložený před kontejner.

Poznámky

První operátor členu vyhodnocuje container.push_front( val)a vrátí hodnotu *this.

Druhý operátor členu vyhodnocuje.

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

poté vrátí hodnotu *this.

Příklad

// 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, který poskytuje odkaz na prvek v sekvenci řízené přiřazeným kontejnerem.

typedef typename Container::reference reference;

Příklad

// 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.
*/

Viz také

<iterátor>
Bezpečný přístup z více vláken ve standardní knihovně C++
Standardní knihovna C++ – referenční dokumentace