Skip to content
    SeaCode
    Learning tracksCode ArenaWorkshops & EventsInstructors
    Log inSign up
    C++C++

    C++ fundamentals

    • Your first C++ program
    • Variables and types
    • Operators and input
    • Conditions: if and switch
    • Loops

    Functions and collections

    • Functions
    • References: by value or by reference?
    • Strings and arrays
    • std::vector
    • map, set and unordered_map

    Memory and pointers

    • Pointers
    • Stack, heap, new and delete
    • Smart pointers and RAII

    Classes and OOP

    • struct and class
    • Constructors and destructors
    • Inheritance and virtual functions
    • Templates

    The STL and professional C++

    • Algorithms and lambdas
    • Exceptions
    • Recursion
    • Project: a grade book
    C++

    Functions and collections · Lesson 9 of 21

    std::vector

    An array that grows itself: push_back, size versus capacity, and how it's stored.

    10 minutes2 games

    std::vector is C++'s most important container: an array whose size changes, with elements side by side in memory, so access is very fast. When in doubt, use a vector.

    C++
    #include <iostream>
    #include <vector>
    #include <string>
    
    int main() {
        std::vector<std::string> names = {"Laila", "Karim"};
        names.push_back("Omar");
        names.insert(names.begin(), "Nour");     // at the front (slower)
        names.pop_back();                        // removes "Omar"
    
        for (const std::string& n : names) std::cout << n << " ";
        std::cout << "\n" << names.size() << " names, first " << names.front() << "\n";
    
        std::vector<int> zeros(5, 0);            // five zeros
        zeros[2] = 7;
        for (int z : zeros) std::cout << z;
        std::cout << "\n";
        return 0;
    }

    size and capacity

    A vector reserves more room than it needs (capacity). When it fills up, it allocates a bigger block (often double), moves every element, and frees the old one. That's why push_back is fast on average even though it's occasionally slow.

    C++
    #include <iostream>
    #include <vector>
    
    int main() {
        std::vector<int> v;
        std::size_t lastCap = v.capacity();
        for (int i = 0; i < 20; i++) {
            v.push_back(i);
            if (v.capacity() != lastCap) {
                std::cout << "size " << v.size() << " -> capacity " << v.capacity() << "\n";
                lastCap = v.capacity();
            }
        }
        return 0;
    }

    v (capacity 8) =

    0[0]
    1[1]
    2[2]
    3[3]
    4[4]
    [5]
    [6]
    [7]

    size = 5; the 3 highlighted slots are reserved but empty. Once they fill, the whole vector moves somewhere bigger.

    Important

    When a vector moves, every pointer, reference or iterator into it becomes invalid. So don't hold on to &v[0] while calling push_back. If you know the size up front, v.reserve(n) allocates once and saves the moves.

    Spot the bug

    This program may crash or print garbage. Which line is the cause?

    Interview question

    What's the time complexity of push_back, and why is it called "amortized O(1)"?

    Exercise

    From the vector {4, 9, 2, 7, 5, 8} build a new vector holding only the numbers above 4, and print it.

    Expected output: 9 7 5 8

    C++
    #include <iostream>
    #include <vector>
    
    int main() {
        std::vector<int> nums = {4, 9, 2, 7, 5, 8};
        std::vector<int> big;
        
        for (int x : big) std::cout << x << " ";
        std::cout << "\n";
        return 0;
    }

    Learn more from

    • cppreference: std::vectoren.cppreference.com
    • learncpp: Introduction to std::vectorlearncpp.com
    • C++ Core Guidelines: Prefer vector by defaultisocpp.github.io

    Played 0 of 2 games

    Sign in to save your progress and earn XP.

    PreviousStrings and arraysNextmap, set and unordered_map
    SeaCode

    Learn to code for real, online or at our branch in front of Cairo University, Giza.

    Learn

    • All courses
    • Interactive tutorials
    • Interview practice problems
    • Mock interview
    • Learning tracks
    • Code Arena
    • Workshops & Events
    • Knowledge library
    • Team projects

    Your account

    • Log in
    • Sign up
    • Learning plan
    • Certificates
    • Site map

    Contact us

    • WhatsApp
    • 010 9094 1104
    • In front of Cairo University, Giza

    © 2026 SeaCode. All rights reserved.