- C++ Basics
- C++ Introduction
- C++ Installation
- C++ Syntax
- C++ Hello World
- C++ Comments
- C++ Variables
- C++ Data Types
- C++ Constants
- C++ Type Casting
- C++ Inline
- C++ File Inclusion
- C++ Date & Time
- C++ Return Types
- C++ Object Oriented
- C++ Classes
- C++ Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Exceptions
- C++ Advanced
- C++ Conditions
- C++ Loops
- C++ Functions
- C++ Structures
- C++ Enums
- C++ References
- C++ Pointers
- C++ Data Structures
- C++ Libs
- C++ Data Structures
- C++ Arrays
- C++ Vectors
- C++ Lists
- C++ Linked List
- C++ Deque
- C++ Stacks
- C++ Queues
- C++ Priority Queues
- C++ Sets
- C++ Maps
- C++ Unordered Sets and Maps
- C++ Graphs
C++ Vectors
In C++, std::vector is a part of the Standard Template Library (STL) and is one of the most commonly used container types. It is a dynamic array that can resize itself automatically when elements are added or removed. Here's a detailed look at std::vector:
Basic Features of std::vector
Dynamic Sizing:
std::vectorcan grow or shrink in size. It handles memory management internally.Random Access: Provides constant-time access to elements using the
[]operator orat()method.Contiguous Memory: Elements are stored in contiguous memory locations, which means they can be accessed efficiently.
Efficient Insertions/Deletions: Insertions and deletions at the end of the vector are efficient, while those in the middle or at the beginning can be less efficient due to potential shifts of other elements.
Basic Operations
1. Including the Header
To use std::vector, include the header file:
cpp#include
2. Declaring and Initializing a Vector
cpp#include
#include
using namespace std;
int main() {
// Default constructor
vector<int> vec1;
// Initialization with values
vector<int> vec2 = {1, 2, 3, 4, 5};
// Initialization with size
vector<int> vec3(5, 10); // Creates a vector with 5 elements, all initialized to 10
return 0;
}
3. Adding Elements
cpp#include
#include
using namespace std;
int main() {
vector<int> vec;
// Adding elements at the end
vec.push_back(1);
vec.push_back(2);
vec.push_back(3);
// Inserting elements at a specific position
vec.insert(vec.begin() + 1, 4); // Inserts 4 at the second position
return 0;
}
4. Accessing Elements
cpp#include
#include
using namespace std;
int main() {
vector<int> vec = {1, 2, 3, 4, 5};
// Accessing elements
cout << vec[0] << endl; // Output: 1
cout << vec.at(1) << endl; // Output: 2
cout << vec.front() << endl; // Output: 1
cout << vec.back() << endl; // Output: 5
return 0;
}
5. Modifying Elements
cpp#include
#include
using namespace std;
int main() {
vector<int> vec = {1, 2, 3, 4, 5};
// Modifying elements
vec[0] = 10;
vec.at(1) = 20;
return 0;
}
6. Removing Elements
cpp#include
#include
using namespace std;
int main() {
vector<int> vec = {1, 2, 3, 4, 5};
// Removing the last element
vec.pop_back();
// Removing a specific element
vec.erase(vec.begin() + 1); // Removes the second element
// Clearing all elements
vec.clear();
return 0;
}
7. Iterating Over a Vector
cpp#include
#include
using namespace std;
int main() {
vector<int> vec = {1, 2, 3, 4, 5};
// Using range-based for loop
for (int num : vec) {
cout << num << " "; // Output: 1 2 3 4 5
}
cout << endl;
// Using iterator
for (auto it = vec.begin(); it != vec.end(); ++it) {
cout << *it << " "; // Output: 1 2 3 4 5
}
cout << endl;
return 0;
}
Advanced Features
1. Resizing
cpp#include
#include
using namespace std;
int main() {
vector<int> vec = {1, 2, 3};
// Resize to a larger size, new elements are default-initialized
vec.resize(5);
// Resize to a smaller size
vec.resize(2);
return 0;
}
2. Capacity and Size
- Size: Number of elements currently in the vector (
size()method). - Capacity: Number of elements the vector can hold before requiring a reallocation (
capacity()method).
cpp#include
#include
using namespace std;
int main() {
vector<int> vec = {1, 2, 3};
cout << "Size: " << vec.size() << endl; // Output: 3
cout << "Capacity: " << vec.capacity() << endl; // Capacity can be greater than size
vec.push_back(4);
cout << "Size: " << vec.size() << endl; // Output: 4
cout << "Capacity: " << vec.capacity() << endl; // May increase
return 0;
}
3. Swapping
cpp#include
#include
using namespace std;
int main() {
vector<int> vec1 = {1, 2, 3};
vector<int> vec2 = {4, 5, 6};
vec1.swap(vec2);
for (int num : vec1) {
cout << num << " "; // Output: 4 5 6
}
cout << endl;
for (int num : vec2) {
cout << num << " "; // Output: 1 2 3
}
cout << endl;
return 0;
}
Performance Considerations
- Insertion/Deletion: Adding or removing elements at the end of the vector is efficient. Inserting or deleting elements in the middle or at the beginning is less efficient due to potential shifting of elements.
- Memory Management:
std::vectorhandles its own memory allocation and deallocation. It can also reserve memory in advance to optimize performance.
Summary
std::vectoris a dynamic array that provides random access and efficient resizing.- It supports a wide range of operations including adding, removing, and accessing elements.
- It provides flexibility with dynamic sizing and is suitable for many use cases where a dynamic array is needed.
std::vector is a powerful and versatile container that is widely used in C++ programming due to its efficiency and ease of use.