Arrays und Vektoren in C++?

Ich bin noch ein Programmieranfänger und lerne C++

Die Grundlegenden Sachen wie Datentypen, In-Output, If-Abfragen, for-Schleifen und Switch-Case.

Aber bei Arrays und Vektoren bekomme ich Probleme vielleicht kann ich auch einfach nicht genug logisch denken dann mir trotzdem jemand helfen

(1 votes)
Loading...

Similar Posts

Subscribe
Notify of
2 Answers
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
regex9
1 year ago

Consider arrays as a collection of variables of the same type. The number is already set from the beginning (i.e. the collection can no longer be reduced or enlarged later) and each entry can be addressed via an index starting with the count at 0.

Example:

std::array numbers;
numbers[0] = 123; // set first entry
numbers[1] = 456; // set second entry

std::cout << numbers[0];

Here an array is created which can record five integers. Consequently, the index range is between 0 and 4 (both limits inclusive).

Only the first two entries are set. The value of the first entry is output.

If you already know the values of the entries from the start, you can also create the array as follows:

std::array numbers { 1, 2, 3, 4, 5 };

The indication of the size can be omitted in this case, because the compiler can now determine the number of elements themselves.

A vector is similar to an array. However, the number of acceptable values can be extended subsequently/dynamically.

Example:

std::vector numbers;
numbers.push_back(123);
numbers.push_back(456);

std::cout << numbers[0];

At the beginning, the vector has a capacitance of 0. With adding the two values, it rises to two.

Dultus, UserMod Light

In short, arrays are static and cannot have expanded. You have a fixed size.

Vectors are dynamic and can be expanded as desired.