Arrays and Vectors
Arrays store a sequence of same-type elements under numeric positions. A fixed-size array has a size chosen in its declaration; std::vector manages a dynamic sequence whose length can change. Both support direct indexing. This beginner lesson teaches storage and iteration; the separate Data Structures lesson develops deeper tradeoffs. We read a vector, append and remove an element, resize it, and reverse the original prefix.
Prerequisites: Loops; variables; basic function calls.
Build the idea
1. Use zero-based positions
A sequence of n elements has indices 0 through n−1. For vector<int> a(n), these n elements exist and are initialized to zero. a[n] is outside the vector. A declaration int fixed[3]={10,20,30} creates exactly three elements. Standard C++17 does not support runtime-sized stack arrays such as int a[n]; use vector for input-dependent lengths.
2. Separate size from capacity
size() is the number of existing elements, not the allocated capacity. push_back(x) appends and increases size. pop_back removes the last existing element and returns nothing; calling it on an empty vector is invalid. resize(n+2,0) creates two new zero elements here. reserve would only request storage, not create indexable elements. Reallocation can invalidate references and iterators.
3. Walk the program’s changes
For input [4,7,1], append 99 to obtain [4,7,1,99], then pop it to restore [4,7,1]. Resize to five elements, producing [4,7,1,0,0]. The last two positions show that resizing changes the logical sequence. They are not part of the original n-element input prefix we subsequently reverse.
4. Reverse with symmetric indices
For each i<n/2 exchange a[i] with a[n−1−i]. Each position pairs with its mirror; stopping halfway prevents swapping everything back. With n=3, only indices 0 and 2 exchange, giving [1,7,4,0,0]. A temporary variable performs the exchange without needing an algorithm-library operation before the STL lesson.
5. Choose a useful indexing convention
Zero-based indexing matches vector and string operations and will be the default here. Sometimes a one-based prefix-sum table simplifies formulas; then explicitly allocate n+1 and leave a[0] as a deliberate extra position. Allocating an extra slot does not make a[n+1] valid. Empty input is allowed in this program, and loop bounds avoid touching a nonexistent original element.
C++17 implementation
This is a complete program. Compile with C++17 and enter the input described below.
#include<iostream>
#include<vector>
int main(){
int n;
std::cin>>n;
std::vector<int>a(n);
for(int i=0;i<n;++i){
std::cin>>a[i];
}
int fixed[3]={10,20,30}; // Fixed-size standard array.
a.push_back(99);
a.pop_back(); // The vector is nonempty when popped.
a.resize(n+2,0);
for(int i=0;i<n/2;++i){
int temp=a[i];
a[i]=a[n-1-i];
a[n-1-i]=temp;
}
std::cout<<fixed[1]<<' '<<a.size()<<'\n';
for(int i=0;i<(int)a.size();++i){
std::cout<<a[i]<<(i+1==(int)a.size()?'\n':' ');
}
}
Complexity analysis
O(n) time and O(n) total vector storage. Index access and size are O(1). Vector push_back is amortized O(1), but an individual reallocation may copy/move O(n) elements. resize to a larger length initializes new elements and may reallocate. The mirror reversal uses O(1) auxiliary space beyond the stored sequence.
Example input and output
Read n (0..100000) then n int values. Print fixed[1] and the final vector size, then the reversed original prefix followed by two appended zeros.
Input
3 4 7 1
Output
20 5 1 7 4 0 0
The fixed array’s middle value is 20. The dynamic vector grows to five elements; only its original three entries are reversed.
Practical applications
Use fixed arrays for small compile-time storage and vectors for runtime-sized indexed sequences. Prefer a direct loop or standard algorithm over pointer tricks while learning. Linked structures and associative containers solve different access patterns and are taught separately.
Check your understanding
Which vector positions exist after vector<int> a(3)?
Put it into practice
Try n=0 and n=1, then change the resize fill value to −1.
Show a hint
New entries receive the fill value; existing elements keep their values.