共用方式為


back_insert_iterator::operator=

Appends or pushes a value onto the back end of a container.

back_insert_iterator<Container>& operator=(
   typename Container::const_reference _Val
);
 back_insert_iterator<Container>& operator=(
   typename Container::value_type&& _Val
);

Parameters

  • _Val
    The value to be inserted into the container.

Return Value

A reference to the last element inserted at the back of the container.

Remarks

The first member operator evaluates Container.push_back(_Val),

then returns *this. The second member operator evaluates

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

then returns *this.

Example

// back_insert_iterator_op_assign.cpp
// compile with: /EHsc
#include <iterator>
#include <vector>
#include <iostream>

int main( )
{
   using namespace std;
   int i;

   vector<int> vec;
   for (i = 1 ; i < 4 ; ++i )  
   {
      vec.push_back ( i );
   }
   
   vector <int>::iterator vIter;
   cout << "The vector vec is: ( ";
   for ( vIter = vec.begin ( ) ; vIter != vec.end ( ); vIter++)
      cout << *vIter << " ";
   cout << ")." << endl;

   back_insert_iterator<vector<int> > backiter ( vec );
   *backiter = 10;
   backiter++;      // Increment to the next element
   *backiter = 20;
   backiter++;

   cout << "After the insertions, the vector vec becomes: ( ";
   for ( vIter = vec.begin ( ) ; vIter != vec.end ( ); vIter++)
      cout << *vIter << " ";
   cout << ")." << endl;
}

Output

The vector vec is: ( 1 2 3 ).
After the insertions, the vector vec becomes: ( 1 2 3 10 20 ).

Requirements

Header: <iterator>

Namespace: std

See Also

Reference

back_insert_iterator Class

Standard Template Library

Other Resources

back_insert_iterator Members