std::vector
std::vector
is one of the most commonly used containers in the C++ Standard Library. It represents a dynamic array that can grow and shrink in size. std::vector provides a convenient way to manage a collection of elements, offering automatic memory management, and supports random access to elements through an index. It is part of the sequence containers in the C++ Standard Library, along with others like std::deque and std::list.
std::vector
std::vector
can dynamically resize itself to accommodate more elements.std::vector
are stored contiguously in memory, which means that you can efficiently access elements with pointer arithmetic and benefit from cache locality.std::vector
automatically handles memory allocation and deallocation, growing as needed.[]
.std::vector
is typically more efficient than other sequence containers for operations like random access and appending elements at the end.std::vector
std::vector
include <iostream>
include <vector>
int main() {
// Create a vector of integers
std::vector<int> myVector;
// Add elements to the vector
myVector.push_back(10);
myVector.push_back(20);
myVector.push_back(30);
// Access and modify elements
myVector[1] = 25;
// Print the elements of the vector
for (int i = 0; i < myVector.size(); ++i) {
std::cout << "Element at index " << i << ": " << myVector[i] << std::endl;
}
return 0;
}
std::vector<int>
is created to store integers.push_back
method is used to add elements to the end of the vector.[].
The element at index 1 is modified.std::vector
with Valuesstd::vector
can be initialized with a list of values using an std::initializer_list
or through a constructor that takes a size and an initial value.
include <iostream>
include <vector>
int main() {
// Initialize vector with values using an initializer list
std::vector<int> myVector = {1, 2, 3, 4, 5};
// Initialize vector with 10 elements, each set to 0
std::vector<int> zeroVector(10, 0);
// Print the elements of myVector
std::cout << "myVector contains: ";
for (int value : myVector) {
std::cout << value << " ";
}
std::cout << std::endl;
// Print the elements of zeroVector
std::cout << "zeroVector contains: ";
for (int value : zeroVector) {
std::cout << value << " ";
}
std::cout << std::endl;
return 0;
}
{1, 2, 3, 4, 5}
.std::vector supports inserting and erasing elements at specific positions using iterators.
include <iostream>
include <vector>
int main() {
std::vector<int> myVector = {1, 2, 3, 4, 5};
// Insert an element at the second position (index 1)
myVector.insert(myVector.begin() + 1, 10);
// Erase the element at the fourth position (index 3)
myVector.erase(myVector.begin() + 3);
// Print the elements of myVector
std::cout << "myVector after insert and erase: ";
for (int value : myVector) {
std::cout << value << " ";
}
std::cout << std::endl;
return 0;
}
std::vector
provides methods to manage its capacity and size, such as resize, reserve, capacity, and size.
include <iostream>
include <vector>
int main() {
std::vector<int> myVector = {1, 2, 3, 4, 5};
// Print size and capacity
std::cout << "Size: " << myVector.size() << ", Capacity: " << myVector.capacity() << std::endl;
// Reserve space for more elements
myVector.reserve(20);
std::cout << "After reserving, Capacity: " << myVector.capacity() << std::endl;
// Resize the vector to contain 10 elements
myVector.resize(10);
std::cout << "After resizing, Size: " << myVector.size() << ", Capacity: " << myVector.capacity() << std::endl;
return 0;
}
size()
returns the number of elements in the vector, while capacity()
returns the number of elements the vector can hold before needing to reallocate.You can iterate over the elements of a std::vector using different methods, such as a range-based for loop, iterators, or indices.
include <iostream>
include <vector>
int main() {
std::vector<int> myVector = {10, 20, 30, 40, 50};
// Range-based for loop
std::cout << "Range-based for loop: ";
for (int value : myVector) {
std::cout << value << " ";
}
std::cout << std::endl;
// Using iterators
std::cout << "Using iterators: ";
for (auto it = myVector.begin(); it != myVector.end(); ++it) {
std::cout << *it << " ";
}
std::cout << std::endl;
// Using indices
std::cout << "Using indices: ";
for (size_t i = 0; i < myVector.size(); ++i) {
std::cout << myVector[i] << " ";
}
std::cout << std::endl;
return 0;
}
std::vector can be used to create multi-dimensional arrays by nesting vectors.
include <iostream>
include <vector>
int main() {
// Create a 2D vector with 3 rows and 4 columns
std::vector<std::vector<int>> matrix(3, std::vector<int>(4, 0));
// Assign values to the 2D vector
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 4; ++j) {
matrix[i][j] = i * 4 + j;
}
}
// Print the 2D vector
for (const auto& row : matrix) {
for (int value : row) {
std::cout << value << " ";
}
std::cout << std::endl;
}
return 0;
}
std::vector:
A dynamic array that can resize itself automatically to accommodate new elements. It provides random access to elements and stores them contiguously in memory.std::vector
supports a wide range of operations, including insertion, deletion, resizing, and iteration.std::vector
is efficient for operations that involve appending elements at the end and accessing elements by index.std::vector
is an essential container in C++ programming, offering a powerful combination of flexibility, efficiency, and ease of use. Its automatic memory management and dynamic resizing capabilities make it a go-to choice for managing collections of data in a wide range of applications.