逆转和旋转
reverse()
reverse_copy()
rotate()
retate_copy()
#include <iostream>
#include <vector>
#include <list>
#include <algorithm>
#include <functional>
#include <iterator>
using namespace std;
int main()
{
vector<int> ivec;
for (int i = 1; i <= 9; ++i)
ivec.push_back(i);
for (vector<int>::iterator iter = ivec.begin();
iter != ivec.end(); ++iter)
cout << *iter << ' ';
cout << endl;
reverse(ivec.begin(), ivec.end());
for (vector<int>::iterator iter = ivec.begin();
iter != ivec.end(); ++iter)
cout << *iter << ' ';
cout << endl;
reverse_copy(ivec.begin()+1, ivec.end()-1, ostream_iterator<int>(cout, " "));
cout << endl;
reverse(ivec.begin(), ivec.end());
for (vector<int>::iterator iter = ivec.begin();
iter != ivec.end(); ++iter)
cout << *iter << ' ';
cout << endl;
rotate(ivec.begin(), find(ivec.begin(), ivec.end(), 4), ivec.end());
for (vector<int>::iterator iter = ivec.begin();
iter != ivec.end(); ++iter)
cout << *iter << ' ';
cout << endl;
return 0;
}
#include <iostream>
#include <vector>
#include <list>
#include <set>
#include <algorithm>
#include <functional>
#include <iterator>
using namespace std;
int main()
{
vector<int> ivec;
set<int> iset;
for (int i = 1; i <= 9; ++i)
iset.insert(i);
cout << "iset元素:" << endl;
for (set<int>::iterator iter = iset.begin();
iter != iset.end(); ++iter)
cout << *iter << ' ';
cout << endl;
set<int>::iterator pos = iset.begin();
advance(pos, 4);
rotate_copy(iset.begin(), pos, iset.end(), ostream_iterator<int>(cout, " "));
cout << endl;
pos = iset.end();
advance(pos, -2);
rotate_copy(iset.begin(), pos, iset.end(), ostream_iterator<int>(cout, " "));
cout << endl;
rotate_copy(iset.begin(), iset.find(4), iset.end(), ostream_iterator<int>(cout, " "));
cout << endl;
return 0;
}