back_inserter
创建一个可以在指定的容器后面插入元素的迭代器。
template<class Container>
back_insert_iterator<Container> back_inserter(
Container& _Cont
);
参数
- _Cont
返回执行插入的容器。
返回值
back_insert_iterator 与容器对象 _Cont。
备注
在标准模板库 (STL) 中,参数必须是引用具有成员函数 push_back容器三个序列之一:deque 类、列表类或 矢量类。
示例
// iterator_back_inserter.cpp
// compile with: /EHsc
#include <iterator>
#include <vector>
#include <iostream>
int main( )
{
using namespace std;
int i;
vector<int> vec;
for ( i = 0 ; i < 3 ; ++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 can be done with template function
back_insert_iterator<vector<int> > backiter ( vec );
*backiter = 30;
backiter++;
*backiter = 40;
// Alternatively, insertions can be done with the
// back_insert_iterator member function
back_inserter ( vec ) = 500;
back_inserter ( vec ) = 600;
cout << "After the insertions, the vector vec is: ( ";
for ( vIter = vec.begin ( ) ; vIter != vec.end ( ); vIter++ )
cout << *vIter << " ";
cout << ")." << endl;
}
要求
头文件: <iterator>
命名空间: std