Vector Basics
Completed
Read n integers into a vector<int> with push_back. Print the size, the first and last elements, and the elements in reverse.
Input
Line 1: n. Line 2: n integers.
Output
Four lines.
Example
Input
5 1 2 3 4 5
Output
5 1 5 5 4 3 2 1
Editor
C++
Hints
push_back appends; front() and back() read the ends.
Solution explanation
A vector grows automatically and knows its own size.
Show solution
#include <iostream>
#include <vector>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> v;
for (int i = 0; i < n; i++) {
int x;
cin >> x;
v.push_back(x);
}
cout << v.size() << "\n" << v.front() << "\n" << v.back() << "\n";
for (int i = (int) v.size() - 1; i >= 0; i--) cout << v[i] << (i > 0 ? " " : "\n");
return 0;
}