C++ Vector inserting elements at indices *before* previous indices are used


Is it possible to insert elements in a vector at a specific index before previous indices are used?

For example

vector<int> example_vector;
example_vector.reserve(100);
example_vector[5] = 28;
example_vector[42] = 32;

Related question: After I have finally assigned all 100 vector slots, I assume push_back will operate as normal add new_elements at the end. Correct?
Try the following. Note the reported size.

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <vector>
using namespace std;

int main()
{
   vector<int> example_vector;
   example_vector.reserve(100);
   example_vector[5] = 28;
   example_vector[42] = 32;
   cout << example_vector[5] << " " << example_vector[42] << endl;
   cout << "Size is " << example_vector.size() << endl;
}



Then try
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <vector>
using namespace std;

int main()
{
   vector<int> example_vector(100);
   example_vector[5] = 28;
   example_vector[42] = 32;
   cout << example_vector[5] << " " << example_vector[42] << endl;
   cout << "Size is " << example_vector.size() << endl;
}



My conclusion, anyway: "can" doesn't mean the same as "should".
Last edited on
Interesting...why is size reported as zero?
Because reserve doesn't change the size of the vector. It sets aside memory in case you want to resize it in the future.
Because at no stage in the first example did you declare the size of the array (so it will start ... and stay ... zero). In the first example you reserved (at least) 100 spaces so that the array could be filled by .push_back() or by .resize (maybe some others), but reserved space is not actual size. Those memory spaces will be temporarily filled with whatever you put there, but they would be overwritten as soon as you did enough push_back()s, or a suitable resize.

In the second example the size (not just the potential reserved space) is actually set to 100 in the constructor. I think (not quite sure) the members would also be defaulted to int{} or 0 as well.

The vector always has some space in reserve so it doesn't have to do a complete reallocation of memory and lots of copying every time you increase the size with push_back. I suspect every compiler has its own algorithm for how much reserve it will keep, and you might be able to change that with compile options. A bit like always keeping your bank account well in the black for those odd days of unexpected expenditure.
Last edited on
From experimentation:

vec.reserve(5);

vec[3] = 25;
vec[1] = 4;
vec[2] = 6;
vec[0] = 20;
vec[4] = 26;

results in vec.size() = 0

then calling push_back to add another element:

vec.pushback(32);

results in vec.size() = 1 AND vec[0] = 32;

http://www.cplusplus.com/reference/vector/vector/operator[]/
If the container size is greater than n, the function never throws exceptions (no-throw guarantee).
Otherwise, the behavior is undefined.


IF example_vector.size() <= n
THEN call of example_vector[n] is undefined behavior


Therefore,
Is it possible to insert elements in a vector at a specific index before previous indices are used?

No.

If you need a sparse list, you can use std::map.
Sorry...I'm working my way through this, and thought I would share for anyone else finding this thread later.

Things work more as I expect if I use

vector<int> vec(5) ... to preallocate the vector

instead of vec.reserve().
vector<int> vec(5) ... to preallocate the vector


Yes, that would be fine if you need to put some intermediate values in first, or had a good idea of the final size of array.

Otherwise, build it up from scratch with .push_back() - it will increase in size by 1 each time.

Have a read through your options:
http://www.cplusplus.com/reference/vector/vector/
Last edited on
> Interesting...why is size reported as zero?

Why do you want to reason about the output of a program that engenders undefined behaviour?

Undefined behavoiur 'Renders the entire program meaningless'
https://en.cppreference.com/w/cpp/language/ub

Note that is is not just the particular construct which engenders undefined behavoiur that is meaningless; the entire program is meaningless.
First, what you wrote is incorrect and dangerous;
by writing these lines:
1
2
example_vector[5] = 28;
example_vector[42] = 32;

you are trying to put 28 and 32 at indices 5 and 42 respectively witch don't even exist;
and example_vector.size() = 0

You could declare your vector like this: vector<int> example_vector(100);
that would initialize the vector with 100 zeros and example_vector.size() = 100

However,
1
2
int x = 23;
 example_vector.push_back(x) 

x will be added to the end of the vector at index 100 exactly and the example_vector.size()
will change to 101.

But don't worry, the vector library provides a function insert() that can be used as the following:
1
2
3
4
5
6

vector<int> example_vector(100);
int i = 2;
vector<int>::iterator it = v.begin() + i;
example_vector.insert(it, 10); // this will insert 10 at the index i=2
Last edited on
Topic archived. No new replies allowed.