P
P
Powerbute2022-02-09 20:13:35
C++ / C#
Powerbute, 2022-02-09 20:13:35

Inserting data into std::vector< GLfloat >?

How can I enter data in std::vector<GLfloat>by index, for example 1?
In Java, I have implemented this:
this.vertexBuffer.put(1, x);
How can I do the same in C ++?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
Evgeny Shatunov, 2022-02-09
@Powerbute

Before working with its indices, the vector must be reduced to the required size.
This is done using the resize[ ? ].
After that, you can access the value by index directly.
If the size of the vector has already been determined and it is necessary to insert data by index, then you can use the insert[ ? ].
However, the first parameter of the method is not an index, but an iterator inside the vector where you want to insert. This iterator can be obtained by offsetting the start iterator of the vector by the required index.

vertexBuffer.insert( vertexBuffer.begin() + 1, x );

At the same time, it is important to control that the insertion index does not go beyond the size of the vector.
You should not forget that when inserting, there is a high probability of memory reallocation for vector elements, as a result of which all iterators and references to vector elements will be invalidated.

V
Vasily Demin, 2022-02-09
@includedlibrary

https://g.zeos.in/?q=c%2B%2B%20vector

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question