C++ Array
Some notes for myself for everything I learned that are helpful in competitive programming.
min_element and max_element
Initialize on the Heap Memory
Working with Ranges
Polymorphic Array
It you try to do Polymorphism with arrays, you might easily run into issues…
What does f
expect:
a[0]. a[1] ...
[x, y, x, y, ]
What it actually looks like:
a[0]. a[1] ...
[7, 8, 9, 10, 5, 6]
Data ends up misaligned. All of Vec2{7,8}
is written into myArray[0]
. Half of Vec2{9,10}
is written into myArray[0]
, the other half is in myArray[1]
.
Lesson
Be very careful when using arrays of objects polymorphically. Solutions:
- Use an array of pointers:
Vec3* myArray[2]
- Use a vector of
Vec3*
How does an array of pointers fix the issue?
..?