如何在C ++中使用STL反转Vector?
在本教程中,我们将讨论一个程序,以了解如何在C++中使用STL反转向量。
为了反转给定的矢量,我们将使用reverse()C++中STL库中的函数。
示例
#include <bits/stdc++.h>
using namespace std;
int main(){
//收集向量
vector<int> a = { 1, 45, 54, 71, 76, 12 };
cout << "Vector: ";
for (int i = 0; i < a.size(); i++)
cout << a[i] << " ";
cout << endl;
//反转向量
reverse(a.begin(), a.end());
cout << "Reversed Vector: ";
for (int i = 0; i < a.size(); i++)
cout << a[i] << " ";
cout << endl;
return 0;
}输出结果
Vector: 1 45 54 71 76 12 Reversed Vector: 12 76 71 54 45 1
热门推荐
5 短祝福语简短暖心
10 结婚祝福语粤语大全简短
11 晚上祝福语女生文案简短
12 法语妈妈生日祝福语简短
13 药厂开工祝福语大全简短
14 蛋糕节日祝福语简短英文
15 跨年的生日祝福语简短
16 文案祝福语英文短句简短
17 在家聚餐婚礼祝福语简短
18 学生节祝福语大全简短