C++常见排序算法-reverse()

tech2026-10-03  2

C++常见排序算法-reverse()

功能描述:

将容器内元素进行反转 #include<iostream> #include <algorithm> #include <vector> using namespace std; class myPrint { public: void operator()(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(); return 0; }

函数原型:

reverse(iterator beg, iterator end);

// 反转指定范围的元素

// beg 开始迭代器

// end 结束迭代器

C++常见排序算法-reverse()

最新回复(0)