Aller au contenu

Arrays

Fixed and Dynamic Arrays

In most object-oriented programming languages like Java or C++, the programmer may create and use arrays. As for objects, there are some differences in the way arrays may be created. In C++, we may distinguish between arrays of fixed or dynamic size:

  • Arrays of fixed size are arrays for which the size is known at compile time.
  • Arrays of dynamic size are arrays for which the size is not known at compile time and can be computed by the program (possible since C99/C++99).

The code snippet demonstrates the creation and usage of arrays. Observe how arrays are created on the stack or on the heap as explained below. As you will see, in C++ unlike in Java, complex objects like arrays can be allocated on the stack as well.

Array Bounds and Indexing

For an array a, the valid indices are between 0 and a.size()-1 (just like for Java). However, unlike in Java, there is no runtime check for legal indices, so accessing an illegal index could cause you to access garbage data without even realizing it. It could also cause crashes.

Vector

C++ also provides the vector template class from the Standard Library, which provides a more flexible way to work with dynamic arrays.

The C++ std::vector class owns the best features of the array and ArrayList in Java. A C++ std::vector has convenient elemental access (using the familiar [] operator) and it can grow dynamically.

The std::vector is a template class (similar to a generic class in Java). If T is some type, then vector<T> is a dynamic array of elements of type T. More details will be given later regarding template classes in C++.

A vector also provides the convenient push and pop functionalities of a stack, where you can add an element to the back of the vector using the push_back() method, and you can remove the last element of the vector using the pop_back() method. It’s possible to access the first element with the front() function and the last element with the back() function.

For a vector v, the valid indices are between 0 and v.size()-1 (just like for arrays or for Java). However, unlike in Java, there is no runtime check for legal indices, so accessing an illegal index could cause you to access garbage data without even realizing it. In fact, accessing a nonexistent element of a vector through the [] operator is undefined behavior in C++.

For more information about vectors and its methods, check out this official page

example

#include <iostream>
#include <vector>

int main() {
    // Create a vector containing integers
    std::vector<int> v = {7, 5, 16, 8};

    // Add two more integers at the end of the vector
    v.push_back(25);
    v.push_back(13);
    // Removes the last element
    v.pop_back();

    // Access first element
    std::cout << "First element is : " << v.front() << std::endl;
    // Access last element
    std::cout << "Last element is : " << v.back() << std::endl;

    // Size of the vector
    int size = v.size();

    // Print out the vector
    std::cout << "v = { ";
    for (int i = 0; i < size; i++) {
        std::cout << v[i] << ", ";
    }
    std::cout << "}; \n";

    return 0;
}