1.功能:将容器内元素反转
2.函数原型
- reverse( iterator beg, iterator end);
- beg 开始迭代器
- end 结束迭代器
#include<iostream> #include<vector> #include<algorithm> #include<ctime> using namespace std; void myprint(int val) { cout << val << " "; } void test1() { vector<int> v; for (int i = 0; i < 10; i++) { v.push_back(i); } cout << "反转前:" ; for_each(v.begin(), v.end(), myprint); //遍历容器 0 1 2 3 4 5 6 7 8 9 cout << endl; //反转容器 reverse(v.begin(), v.end()); cout << "反转后:" ; for_each(v.begin(), v.end(), myprint); //遍历容器 9 8 7 6 5 4 3 2 1 0 cout << endl; } int main() { srand((unsigned int)time(NULL)); test1(); return 0; }