example1_vector_dynamic_array.cpp

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <vector>
#include <algorithm>

int main() {
    std::vector<int> numbers = {5, 2, 8, 1, 9};

    // Adding elements
    numbers.push_back(7);

    // Accessing elements
    std::cout << "First element: " << numbers[0] << std::endl;

    // Iterating
    for (const auto& num : numbers) {
        std::cout << num << " ";
    }
    std::cout << std::endl;

    // Sorting
    std::sort(numbers.begin(), numbers.end());

    // Size and capacity
    std::cout << "Size: " << numbers.size() << std::endl;
    std::cout << "Capacity: " << numbers.capacity() << std::endl;

    return 0;
}
Back to containers