In C++, a vector is a sequence container that is part of the C++ Standard Library, provided by the vector class. It is essentially a dynamic array, meaning it can automatically resize as elements are added or removed.
Methods for vector
push_back(value): Adds an element to the end of the vector.
pop_back(): Removes the last element.
size(): Returns the number of elements in the vector.
clear(): Removes all elements from the vector.
at(index): Provides safe access to an element (throws an exception if the index is out of range).
Example: Adding and Accessing Elements
#include <iostream>
#include <vector>
using namespace std;
int main() {
// Creating a vector of integers
vector<int> numbers;
// Adding elements to the vector
numbers.push_back(10);
numbers.push_back(20);
numbers.push_back(30);
numbers.push_back(40);
numbers.push_back(50);
// Accessing elements using index
std::cout << "First element: " << numbers[0] << std::endl;
std::cout << "Second element: " << numbers[1] << std::endl;
// Iterating through the vector using a range-based for loop
cout << "Elements in the vector: ";
for (int num : numbers) {
cout << num << " ";
}
cout << endl;
return 0;
}
Output:
Second element: 20
Elements in the vector: 10 20 30 40 50
Example: Resizing and Clearing a Vector
#include <iostream>
#include <vector>
using namespace std;
int main() {
// Creating a vector of integers
vector<int> nums = {10, 20, 30, 40, 50};
// Resize the vector to have 2 elements
nums.resize(2);
cout << "After resizing to 2 elements: ";
for (int num : nums) {
cout << num << " ";
}
cout << std::endl;
// Add new elements to the vector
nums.push_back(3);
nums.push_back(4);
nums.push_back(5);
cout << "After adding new elements: ";
for (int num : nums) {
cout << num << " ";
}
cout << endl;
// Clear all elements from the vector
nums.clear();
cout << "After clearing, size of vector: " << nums.size() << std::endl;
return 0;
}
Output:
After adding new elements: 10 20 3 4 5
After clearing, size of vector: 0
Using pop_back() method to remove last element from the elements
#include <iostream>
#include <vector>
using namespace std;
int main() {
// Creating a vector of integers
vector<int> nums = {10, 20, 30, 40, 50};
// remove last element
nums.pop_back();
cout << "list of elements: ";
for (int num : nums) {
cout << num << " ";
}
return 0;
}
Output:
Using at() method to get the element
#include <iostream>
#include <vector>
using namespace std;
int main() {
// Creating a vector of integers
vector<int> nums = {10, 20, 30, 40, 50};
cout << "Get the First element: " << nums.at(2) << endl;
cout << "Get the 4th element: " << nums.at(4);
return 0;
}
Output:
Get the 4th element: 50
C++ Vector – Questions and Answers
Q 1: What is a vector?
Ans: Dynamic array in STL.
Q 2: Is vector resizable?
Ans: Yes.
Q 3: How to add element to vector?
Ans: Using push_back().
Q 4: How to access vector elements?
Ans: Using index or iterator.
Q 5: Advantage of vector?
Ans: Automatic memory management.
C++ Vector – Objective Questions (MCQs)
Q1. Which header file must be included to use vector in C++?
Q2. Which of the following is true about vectors in C++?
Q3. Which function is used to add an element at the end of a vector?
Q4. Which function returns the current number of elements in a vector?
Q5. How can you access the first element of a vector v?