如何在 C++ 中使用 STL 反转一个矢量?
本教程将讨论一个示例程序,让你了解如何在 C++ 中使用 STL 反转一个矢量。
要反转给定的矢量,我们将使用 C++ 中 STL 库中提供的 reverse() 函数。
示例
#include <bits/stdc++.h>
using namespace std;
int main(){
//collecting the vector
vector<int> a = { 1, 45, 54, 71, 76, 12 };
cout << "Vector: ";
for (int i = 0; i < a.size(); i++)
cout << a[i] << " ";
cout << endl;
//reversing the vector
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
广告
数据结构
网络
关系数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
JavaScript
PHP