C++ STL 逆转旋转 reverse reverse_copy rotate
#include <iostream>
#include <algorithm>
#include <vector>
#include <iterator>
using namespace std;
int main()
{
vector<int> vec1;
for (int k=0;k<10;k++)
{
vec1.push_back(k);
}
vector<int>::iterator vec_iter1;
for (vec_iter1 = vec1.begin();vec_iter1 != vec1.end();++vec_iter1)
{
cout << *vec_iter1 << " ";
}
cout << endl;
cout << "---------------------------------------" << endl;
//逆转
reverse(vec1.begin(), vec1.end());
for (vec_iter1 = vec1.begin(); vec_iter1 != vec1.end(); ++vec_iter1)
{
cout << *vec_iter1 << " ";
}
cout << endl;
cout << "---------------------------------------" << endl;
vector<int> vec2;
reverse_copy(vec1.begin(), vec1.end(), ostream_iterator<int>(cout," "));
cout << endl;
cout << "---------------------------------------" << endl;
//旋转
rotate(vec1.begin(), vec1.begin() + 5, vec1.end());
for (vec_iter1 = vec1.begin(); vec_iter1 != vec1.end(); ++vec_iter1)
{
cout << *vec_iter1 << " ";
}
cout << endl;
cout << "---------------------------------------" << endl;
rotate(vec1.begin(), find(vec1.begin(), vec1.end(), 9), vec1.end());
for (vec_iter1 = vec1.begin(); vec_iter1 != vec1.end(); ++vec_iter1)
{
cout << *vec_iter1 << " ";
}
cout << endl;
cout << "---------------------------------------" << endl;
system("pause");
return 0;
}
=====================================================
0 1 2 3 4 5 6 7 8 9
---------------------------------------
9 8 7 6 5 4 3 2 1 0
---------------------------------------
0 1 2 3 4 5 6 7 8 9
---------------------------------------
4 3 2 1 0 9 8 7 6 5
---------------------------------------
9 8 7 6 5 4 3 2 1 0
---------------------------------------
请按任意键继续. . .