Labs ICT
Pro Login

Vector

C++ Vector

std::vector is a dynamic array that can grow and shrink at runtime. Elements are stored contiguously in memory, giving fast random access (O(1)). Use push_back() to add elements to the end and size() to get the number of elements. Vectors are the most commonly used STL container because they combine speed with flexibility.

#include <iostream>
#include <vector>

int main() {
    std::vector<std::string> fruits;
    fruits.push_back("apple");
    fruits.push_back("banana");
    fruits.push_back("cherry");
    std::cout << "Size: " << fruits.size() << "\n";
    for (int i = 0; i < fruits.size(); i++) {
        std::cout << fruits[i] << "\n";
    }
    return 0;
}
Try it Yourself →