C++reverse使用介绍

功能描述:
*
将容器内元素进行反转(首尾位置互换)

函数原型:

reverse(iterator beg, iterator end);

//反转指定范围的元素

//beg开始迭代器

//end结束迭代器

代码示例:

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
void myPrint(int val)
{
cout << val << " ";
}
void test01()
{
vector<int>v;
v.push_back(10);
v.push_back(30);
v.push_back(50);
v.push_back(20);
v.push_back(40);
cout << "反转前:" << endl;
for_each(v.begin(), v.end(), myPrint);
cout << endl;
cout << "反转后:" << endl;
reverse(v.begin(), v.end());
for_each(v.begin(), v.end(), myPrint);
cout << endl;
}
int main()
{
test01();
system("pause");
return 0;
}

总结:reverse反转区间内元素,面试题可能涉及到