Data Structures

C++ Vectors

Working with Vectors

C++ vectors use std::vector for dynamic arrays.

Introduction to C++ Vectors

C++ Vectors, part of the Standard Template Library (STL), are dynamic arrays that allow you to store elements in a contiguous memory space. Unlike traditional arrays, vectors can resize themselves automatically when elements are added or removed.

Creating a Vector

To create a vector in C++, you need to include the <vector> header. You can declare a vector like this:

#include <vector>

int main() {
    std::vector<int> numbers;
    return 0;
}

Adding Elements to a Vector

You can add elements to a vector using the push_back() method. This adds an element to the end of the vector:

#include <vector>
#include <iostream>

int main() {
    std::vector<int> numbers;
    numbers.push_back(10);
    numbers.push_back(20);
    numbers.push_back(30);
    
    for(int i = 0; i < numbers.size(); ++i) {
        std::cout << numbers[i] << " ";
    }
    return 0;
}

Accessing Vector Elements

Vector elements can be accessed using the [] operator or the at() method. Here is an example of both:

#include <vector>
#include <iostream>

int main() {
    std::vector<int> numbers = {10, 20, 30};
    
    std::cout << "First element: " << numbers[0] << std::endl; // Using []
    std::cout << "Second element: " << numbers.at(1) << std::endl; // Using at()
    
    return 0;
}

Removing Elements from a Vector

To remove elements from a vector, you can use the pop_back() method, which removes the last element:

#include <vector>
#include <iostream>

int main() {
    std::vector<int> numbers = {10, 20, 30};
    numbers.pop_back(); // Removes 30
    
    for(int num : numbers) {
        std::cout << num << " ";
    }
    return 0;
}

Vector Size and Capacity

Vectors manage two important properties: size and capacity. The size is the number of elements in the vector, while the capacity is the amount of storage currently allocated for the vector. Here is how you can access these properties:

#include <vector>
#include <iostream>

int main() {
    std::vector<int> numbers = {10, 20, 30};
    
    std::cout << "Size: " << numbers.size() << std::endl;
    std::cout << "Capacity: " << numbers.capacity() << std::endl;
    
    return 0;
}

Resizing a Vector

The resize() method allows you to change the number of elements stored in the vector. If the new size is greater than the current size, new elements are default-initialized. If it's less, elements are removed:

#include <vector>
#include <iostream>

int main() {
    std::vector<int> numbers = {10, 20, 30};
    numbers.resize(5); // Resize to 5, adding two default-initialized elements
    numbers.resize(2); // Resize to 2, removing the last three elements
    
    for(int num : numbers) {
        std::cout << num << " ";
    }
    return 0;
}

Clearing a Vector

To remove all elements from a vector, use the clear() method. This method sets the size of the vector to 0 but does not change its capacity:

#include <vector>
#include <iostream>

int main() {
    std::vector<int> numbers = {10, 20, 30};
    numbers.clear(); // Removes all elements
    
    std::cout << "Size after clear: " << numbers.size() << std::endl;
    
    return 0;
}
Previous
Arrays
Next
Maps
SQL Syntax T-Shirts
No t-shirts available for this site.