Adding elements to C++ arrays and growing a vector safely with push_back.

Append Array C++: Add Elements with a Vector

Use std::vector and push_back when a C++ sequence needs to grow. A built-in array has a fixed number of elements, so appending to a C++ array requires a different container or a separate array with enough space.

C++ Array Append with std::vector

A built-in declaration such as int values[3] = {10, 20, 30}; creates exactly three elements. Writing values[3] goes past the end and causes undefined behavior; it does not add a fourth element. A C++ array append operation therefore cannot enlarge that object. std::array is also fixed-size.

A vector manages a resizable sequence. Include <vector> and append to its end:

#include <iostream>
#include <vector>

int main() {
    std::vector<int> values{10, 20, 30};
    values.push_back(40);

    for (int value : values) {
        std::cout << value << ' ';
    }
    std::cout << '\n';
}

The program prints 10 20 30 40. The call creates a new last element and increases size() to four. Compile with a C++11-or-later compiler, for example c++ -std=c++17 example.cpp -o example, and run the resulting executable. push_back appends one value; it does not insert at an arbitrary position.

Copy an Existing Array into a Vector

If an API gives you an existing built-in array, initialize a vector from its range:

#include <iterator>
#include <vector>

int original[] = {10, 20, 30};
std::vector<int> values(std::begin(original), std::end(original));
values.push_back(40);

The vector contains copies of the original elements. The original array remains three elements long and does not change when the vector grows. To append an array, C++ programs can also insert its range at the end of an existing vector using values.insert(values.end(), std::begin(original), std::end(original)). Ensure the source range remains valid for the operation.

If you must keep a fixed array, track how many slots are occupied and assign only while that count is below its fixed capacity. For example, an array of ten integers with three values can accept a fourth at index three. This fills a slot already in the array; it does not change the array’s length. Check the bound before every such assignment.

Distinguish Size from Capacity

A vector’s size() counts constructed elements. Its capacity() is the number it can hold before another reallocation is needed. reserve(100) can prepare space for at least 100 elements, but it does not create them or change size(). An index is valid only when it is less than size(), even if capacity is larger.

After adding elements to a C++ vector, check the index against the current size before reading it. For example, after the first example’s push_back, index 3 is valid; index 4 is not. values.at(index) performs a bounds check and throws std::out_of_range for an invalid index. Use it when an exception fits your error-handling design.

Account for Reallocation

Growing a vector may move its elements to new storage. If push_back reallocates, pointers, references, and iterators to its elements become invalid. If no reallocation occurs, existing element references remain valid, but the old past-the-end iterator is still invalidated. Reacquire handles after growth, or retain an index and verify it before accessing the element again.

Calling reserve before a known batch can reduce reallocations; the reserve call itself can invalidate handles if it changes capacity. The Microsoft vector reference documents the container operations.

Choose a fixed array when the number of elements is known and stays fixed. Choose a vector when the sequence must grow at runtime, and manage references and bounds around that growth.