Vector Basics in C++
Basic Vector Operations in C++.
BeginnerSTL Containers ProgramsExample 1 of 10
vector-basics.cpp
1#include <iostream>2#include <vector>3using namespace std;45int main() {6 // Create vector7 vector<int> vec;89 // Add elements10 vec.push_back(10);11 vec.push_back(20);12 vec.push_back(30);13 vec.push_back(40);14 vec.push_back(50);1516 cout << "Vector elements: ";17 for (int i = 0; i < vec.size(); i++) {18 cout << vec[i] << " ";19 }20 cout << endl;2122 // Access elements23 cout << "First element: " << vec.front() << endl;24 cout << "Last element: " << vec.back() << endl;25 cout << "Element at index 2: " << vec.at(2) << endl;2627 // Size and capacity28 cout << "Size: " << vec.size() << endl;29 cout << "Capacity: " << vec.capacity() << endl;3031 // Initialize vector with values32 vector<int> vec2 = {1, 2, 3, 4, 5};33 cout << "\nVector 2: ";34 for (int num : vec2) {35 cout << num << " ";36 }37 cout << endl;3839 // Remove last element40 vec.pop_back();41 cout << "\nAfter pop_back: ";42 for (int num : vec) {43 cout << num << " ";44 }45 cout << endl;4647 return 0;48}
Output
Vector elements: 10 20 30 40 50 First element: 10 Last element: 50 Element at index 2: 30 Size: 5 Capacity: 8 Vector 2: 1 2 3 4 5 After pop_back: 10 20 30 40
What's going on
The program demonstrates basic vector operations:
Creating and initializing vectors
Adding elements with push_back()
Accessing elements (front, back, at, index)
Getting size and capacity
Removing elements with pop_back()
Vectors are the most commonly used STL container.
Header Files Used
1.#include <iostream>
Provides cout and cin for input/output operations.
2.#include <vector>
Provides vector container class.
Understanding Vectors
Vector Concept
:
Dynamic array that resizes automatically
Elements stored contiguously in memory
Random access (like arrays)
Efficient insertion/deletion at end
Key Features
:
Automatic memory management
Can grow and shrink dynamically
Cache-friendly (contiguous memory)
Part of STL (Standard Template Library)
Creating Vectors
Empty Vector
:
vector<int> vec;
Initialized Vector
:
vector<int> vec2 = {1, 2, 3, 4, 5};
How it works
:
Template syntax: vector<dataType>
Can hold any data type
Initially empty (size 0)
Can initialize with values
Adding Elements
push_back()
:
vec.push_back(10);
vec.push_back(20);
vec.push_back(30);
How it works
:
Adds element to end of vector
Automatically resizes if needed
O(1) amortized time complexity
Most efficient way to add elements
Accessing Elements
Methods
:
vec[i]: array-like access (no bounds check)
vec.at(i): access with bounds check (throws exception)
vec.front(): first element
vec.back(): last element
Example
:
vec.front() // Returns 10
vec.back() // Returns 30
vec.at(2) // Returns 30 (with bounds check)
vec[1] // Returns 20 (no bounds check)
Size and Capacity
Size
:
vec.size() // Number of elements
Capacity
:
vec.capacity() // Allocated memory size
How it works
:
Size: actual number of elements
Capacity: memory allocated (usually larger)
Vector pre-allocates extra memory
Resizes when capacity exceeded
Removing Elements
pop_back()
:
vec.pop_back(); // Removes last element
How it works
:
Removes element from end
O(1) time complexity
Reduces size (not capacity)
Efficient operation
When to Use Vectors
Best For
:
Dynamic arrays (size changes)
Random access requirements
Frequent insertion/deletion at end
General-purpose container
Most common STL container
Example Scenarios
:
Reading unknown number of elements
Dynamic data structures
Arrays that need to grow
General data storage
Important Considerations
Memory Layout
:
Elements stored contiguously
Cache-friendly (good performance)
Like arrays but dynamic
Performance
:
push_back(): O(1) amortized
pop_back(): O(1)
Random access: O(1)
Insert/delete in middle: O(n)
Automatic Management
:
Memory allocated automatically
Resizes when needed
No manual memory management
Exception-safe
return 0;
This ends the program successfully.