back_inserter
建立可將項目插入至位於指定之容器的上一頁的 Iterator。
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