STL中的简单容器和迭代器:
(1)
#include<iostream>
#include<vector>
#include<iterator>
using namespace std;
int main()
{
vector<int> ivec;
int i;
for(i = 0; i < 5; i++ )
ivec.push_back(i);
for(i = 0; i < 5; i++)
cout<<ivec[i]<<" ";
cout<<endl;
while( !ivec.empty())
{
cout << ivec.back() << " ";
ivec.pop_back();
}
cout << endl;
for(i = 0; i < 5; i++)
cout<<ivec[i]<<" ";
cout<<endl;
copy(ivec.begin(),ivec.end(),ostream_iterator<int>(cout, " "));
cout<<endl;
cout << "size=" << ivec.size() << endl;
return 0;
}
(2)
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> intList;
vector<int>::iterator listIt;
int i;
intList.push_back(1);
intList.push_back(5);
intList.push_back(10);
intList.push_back(15);
cout<<"Line 1: List Elements: ";
for(i=0; i<4; i++)
cout<<intList[i]<<" ";
cout<<endl;
for(i=0; i<4; i++)
intList[i] *=2;
cout<<"Line 2: List Elements: ";
for(listIt=intList.begin(); listIt != intList.end(); ++listIt)
cout<<*listIt<<" ";
cout<<endl;
listIt=intList.begin();
++listIt;
++listIt;
intList.insert(listIt,8);
cout<<"Line 3: List Elements: ";
for(listIt = intList.begin(); listIt != intList.end(); ++listIt)
cout<<*listIt<<" ";
cout<<endl;
return 0;
}
(3)
#include <iterator>
#include <list>
#include <algorithm>
#include <iostream>
using namespace std;
int main()
{
int ia[5] = {1,2,3,4};
list<int> id(ia, ia+4);
ostream_iterator<int> outite(cout, " ");
copy(id.begin(), id.end(), outite);
cout << endl;
copy(ia+1, ia+2, front_inserter(id));
copy(id.begin(), id.end(), outite);
cout << endl;
copy(ia+3, ia+4, back_inserter(id));
copy(id.begin(), id.end(), outite);
cout << endl;
list<int>::iterator ite = find(id.begin(), id.end(), 3);
copy(ia+0, ia+2, inserter(id, ite));
copy(id.begin(), id.end(), outite);
cout << endl;
copy(id.rbegin(), id.rend(), outite);
cout << endl;
return 0;
}
========
MSDN中copy函数:
(Assigns the values of elements from a source range to a destination range, iterating through the source sequence of elements and assigning them new positions in a forward direction.
)
// alg_copy.cpp
// compile with: /EHsc
#include <vector>
#include <algorithm>
#include <iostream>
int main() {
using namespace std;
vector <int> v1, v2;
vector <int>::iterator Iter1, Iter2;
int i;
for ( i = 0 ; i <= 5 ; i++ )
v1.push_back( 10 * i );
int ii;
for ( ii = 0 ; ii <= 10 ; ii++ )
v2.push_back( 3 * ii );
cout << "v1 = ( " ;
for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )
cout << *Iter1 << " ";
cout << ")" << endl;
cout << "v2 = ( " ;
for ( Iter2 = v2.begin( ) ; Iter2 != v2.end( ) ; Iter2++ )
cout << *Iter2 << " ";
cout << ")" << endl;
// To copy the first 3 elements of v1 into the middle of v2
copy( v1.begin( ), v1.begin( ) + 3, v2.begin( ) + 4 );
cout << "v2 with v1 insert = ( " ;
for ( Iter2 = v2.begin( ) ; Iter2 != v2.end( ) ; Iter2++ )
cout << *Iter2 << " ";
cout << ")" << endl;
// To shift the elements inserted into v2 two positions
// to the left
copy( v2.begin( )+4, v2.begin( ) + 7, v2.begin( ) + 2 );
cout << "v2 with shifted insert = ( " ;
for ( Iter2 = v2.begin( ) ; Iter2 != v2.end( ) ; Iter2++ )
cout << *Iter2 << " ";
cout << ")" << endl;
}
输出:
v1 = ( 0 10 20 30 40 50 )
v2 = ( 0 3 6 9 12 15 18 21 24 27 30 )
v2 with v1 insert = ( 0 3 6 9 0 10 20 21 24 27 30 )
v2 with shifted insert = ( 0 3 0 10 20 10 20 21 24 27 30 )