back_inserter
建立可以在指定的容器中插入項目的迭代器。
template<class Container>
back_insert_iterator<Container> back_inserter(
Container& _Cont
);
參數
- _Cont
後面插入要執行的容器。
傳回值
與 back_insert_iterator 相關聯的容器 _Cont物件。
備註
在標準樣板程式庫中,引數必須以成員函式 push_back三個序列容器的其中一項: 雙向佇列類別、 清單類別。或 Vector 類別。
範例
// 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;
}
需求
標頭:<迭代器>
命名空間: std