back_insert_iterator 类

描述满足输出迭代器需求的迭代器适配器。 它将元素插入到序列后端而非覆盖序列,因此它提供的语义不同于 C++ 序列容器的迭代器所提供的覆盖语义。 back_insert_iterator 类针对容器类型进行模板化。

语法

template <class Container>
class back_insert_iterator;

参数

容器
要通过 back_insert_iterator 将元素插入后端的容器的类型。

注解

此容器必须满足末尾插入序列的要求,可以从中在分期常量时间内将元素插入序列末尾。 deque 类list 类vector 类定义的 C++ 标准库序列容器,提供需要的 push_back 成员函数并满足这些需求。 在这三个容器和字符串中,可以对其中每一个进行适配以便与 back_insert_iterator 一起使用。 back_insert_iterator 必须使用其容器进行初始化。

构造函数

构造函数 说明
back_insert_iterator 构造在容器的最后一个元素后插入元素的 back_insert_iterator

Typedef

类型名称 说明
container_type back_insert_iterator 提供容器的类型。
reference back_insert_iterator 提供引用的类型。

运算符

运算符 说明
operator* 取消引用运算符,用于实现末尾插入的输出迭代器表达式 * i = x
operator++ back_insert_iterator 递增到下一个可用来存储值的位置。
operator= 赋值运算符,用于实现末尾插入的输出迭代器表达式 *i = x

要求

标头:<iterator>

命名空间: std

back_insert_iterator::back_insert_iterator

构造在容器的最后一个元素后插入元素的 back_insert_iterator

explicit back_insert_iterator(Container& _Cont);

参数

_Cont
back_insert_iterator 要向其中插入元素的容器。

返回值

针对参数容器的 back_insert_iterator

示例

// back_insert_iterator_back_insert_iterator.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 initial vector vec is: ( ";
   for ( vIter = vec.begin ( ) ; vIter != vec.end ( ); vIter++)
      cout << *vIter << " ";
   cout << ")." << endl;

   // Insertions with member function
   back_inserter ( vec ) = 40;
   back_inserter ( vec ) = 50;

   // Alternatively, insertions can be done with template function
   back_insert_iterator<vector<int> > backiter ( vec );
*backiter = 600;
   backiter++;
*backiter = 700;

   cout << "After the insertions, the vector vec is: ( ";
   for ( vIter = vec.begin ( ) ; vIter != vec.end ( ); vIter++)
      cout << *vIter << " ";
   cout << ")." << endl;
}
The initial vector vec is: ( 1 2 3 ).
After the insertions, the vector vec is: ( 1 2 3 40 50 600 700 ).

back_insert_iterator::container_type

back_insert_iterator 提供容器的类型。

typedef Container
container_type;

备注

该类型是模板参数 Container 的同义词。

示例

// back_insert_iterator_container_type.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 original vector vec is: ( ";
   for ( vIter = vec.begin ( ) ; vIter != vec.end ( ); vIter++)
      cout << *vIter << " ";
   cout << ")." << endl;

   back_insert_iterator<vector<int> >::container_type vec1 = vec;
   back_inserter ( vec1 ) = 40;

   cout << "After the insertion, the vector is: ( ";
   for ( vIter = vec1.begin ( ) ; vIter != vec1.end ( ); vIter++)
      cout << *vIter << " ";
   cout << ")." << endl;
}
The original vector vec is: ( 1 2 3 ).
After the insertion, the vector is: ( 1 2 3 40 ).

back_insert_iterator::operator*

用于实现输出迭代器表达式 * i = x 的取消引用运算符。

back_insert_iterator<Container>& operator*();

返回值

对插入到容器末尾的元素的引用。

备注

用于实现输出迭代器表达式 *Iter = value。 如果 Iter 是对序列中的元素进行寻址的迭代器,则 *Iter = value 会替换该元素的值,且不会改变此序列中元素的总数。

示例

// back_insert_iterator_back_insert.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;
}
The vector vec is: ( 1 2 3 ).
After the insertions, the vector vec becomes: ( 1 2 3 10 20 ).

back_insert_iterator::operator++

back_insert_iterator 递增到下一个可用来存储值的位置。

back_insert_iterator<Container>& operator++();
back_insert_iterator<Container> operator++(int);

返回值

back_insert_iterator,它寻址下一个可用来存储值的位置。

注解

前递增和后递增运算符将返回相同的结果。

示例

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

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

   vector<int> vec;
   for (i = 1 ; i < 3 ; ++i )
   {
      vec.push_back ( 10 * 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 = 30;
   backiter++;      // Increment to the next element
*backiter = 40;
   backiter++;

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

back_insert_iterator::operator=

将值追加或放回到容器的末尾。

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

参数

val
要插入到容器中的值。

返回值

对插入到容器末尾的最后一个元素的引用。

备注

第一个成员运算符会对 Container.push_back( val) 求值,

然后返回 *this。 第二个成员运算符会求值

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

然后返回 *this

示例

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

back_insert_iterator::reference

back_insert_iterator 提供引用的类型。

typedef typename Container::reference reference;

注解

此类型描述对关联容器控制的序列元素的引用。

示例

// back_insert_iterator_reference.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> >::reference
        RefLast = *(vec.end ( ) - 1 );
   cout << "The last element in the vector vec is: "
        << RefLast << "." << endl;
}
The vector vec is: ( 1 2 3 ).
The last element in the vector vec is: 3.

另请参阅

<iterator>
C++ 标准库中的线程安全
C++ 标准库参考