std::vector


STL Container: 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.

Key Characteristics of std::vector

Basic Operations on std::vector

Example 1: Basic Usage of 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;
}

Explanation:

Example 2: Initializing a std::vector with Values

std::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;
}

Explanation:

Example 3: Inserting and Erasing Elements

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;
}

Explanation:

Example 4: Capacity and Size Management

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;
}

Explanation:

Example 5: Iterating Over a std::vector

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;
}

Explanation:

Example 6: Two-Dimensional Vectors

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;
}

Explanation:

Summary

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.

Previous Page | Course Schedule | Course Content