题解 | #迭代器遍历容器#
迭代器遍历容器
https://www.nowcoder.com/practice/0f7ab22e60ee4574a9d9c81412b26595
只能说STL NB!
对于用好STL对于写编程题,还是很有帮助的。
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
vector <int> a;
int n;
for(int i = 1; i <= 5; i++){
cin >> n;
a.push_back(n); //在尾部添加元素
}
//添加完元素进行遍历输出
for(auto x : a){
cout << x << " ";
}
printf("\n");
//对动态数组进行翻转
reverse(a.begin(), a.end());
//对翻转后的数组进行输出
for(auto x : a){
cout << x << " ";
}
return 0;
}
腾讯云智研发成长空间 303人发布
