C++ Beginner

cpp_012: Introduction to Vectors in C++

codeaddict 2025. 2. 17. 23:42

Vectors in C++ provide a more flexible alternative to arrays. They are part of the Standard Template Library (STL) and offer dynamic resizing, built-in functions, and better memory management. In this lesson, we’ll explore what vectors are, how they differ from arrays, and compare them to Python lists.


1. What is a Vector?

A vector is a dynamic array that automatically resizes as elements are added or removed. Unlike arrays, which have a fixed size, vectors grow and shrink as needed.

To use vectors in C++, you must include the <vector> header:

#include <vector>

Key Differences Between Vectors and Arrays

2. Declaring and Initializing Vectors

Vectors are defined using the std::vector keyword.

Basic Syntax

#include <iostream>
#include <vector>  // Include the vector library

using namespace std;

int main() {
    vector<int> numbers; // Declaring an empty vector
    vector<int> predefined = {10, 20, 30, 40}; // Declaring and initializing

    cout << "First element: " << predefined[0] << endl; // Accessing elements
}

Output:

First element: 10

3. Adding and Removing Elements

Vectors support dynamic modification using functions like .push_back() and .pop_back().

#include <iostream>
#include <vector>

using namespace std;

int main() {
    vector<int> numbers;

    numbers.push_back(10); // Adds 10 to the vector
    numbers.push_back(20); // Adds 20 to the vector
    numbers.push_back(30);

    cout << "Vector size: " << numbers.size() << endl; // Prints size

    numbers.pop_back(); // Removes the last element (30)
    cout << "New size after pop: " << numbers.size() << endl;
}

Output:

Vector size: 3  
New size after pop: 2

4. Iterating Over a Vector

Vectors can be accessed using loops or iterators.

Using a for loop

#include <iostream>
#include <vector>

using namespace std;

int main() {
    vector<int> nums = {1, 2, 3, 4, 5};

    for (int i = 0; i < nums.size(); i++) {
        cout << nums[i] << " ";
    }
}

Output:

1 2 3 4 5

Using a for-each loop

#include <iostream>
#include <vector>

using namespace std;

int main() {
    vector<int> nums = {1, 2, 3, 4, 5};

    for (int num : nums) {
        cout << num << " ";
    }
}

Output:

1 2 3 4 5

5. Comparison with Python Lists

Vectors in C++ are similar to Python lists because they allow dynamic resizing.

Python List Example

numbers = [10, 20, 30]
numbers.append(40)  # Equivalent to push_back()
print(numbers)  # [10, 20, 30, 40]

numbers.pop()  # Equivalent to pop_back()
print(numbers)  # [10, 20, 30]

Output:

[10, 20, 30, 40]  
[10, 20, 30]

7. Two-Dimensional Vectors

Vectors can store other vectors, just like a 2D array.

#include <iostream>
#include <vector>

using namespace std;

int main() {
    vector<vector<int>> matrix = {
        {1, 2, 3},
        {4, 5, 6},
        {7, 8, 9}
    };

    cout << "Element at (1,1): " << matrix[1][1] << endl;
}

Output:

Element at (1,1): 5

Conclusion

Vectors are a powerful alternative to arrays in C++. They provide dynamic resizing, built-in functions, and flexibility, making them a preferred choice for many applications. Unlike arrays, vectors manage memory automatically and prevent overflow issues.

Key Takeaways

✅ Use vectors when you need a dynamic data structure.
✅ Arrays are faster but require manual memory management.
Vectors in C++ work similarly to lists in Python.