vector.size() givinig unexpected results with array indexing

Write your question here.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
using namespace std;
#include <vector>

int main(){
vector<int> vec1;
vec1.push_back(0);

vec1[0] = 0;
vec1[1] = 1;
vec1[2] = 2;
vec1[3] = 3;
vec1[4] = 4;

cout << vec1.size() << endl; // outputs 1
cout << vec1[3] << endl; //outputs 3

	return 0;
}


So yeah. vector.size() is apparently not giving me correct results when I use array indexing. It works fine when I use vector.push_back().

Is it supposed to do this, or is something wrong?

ALSO, could anyone explain to me why I have to do a "vec1.push_back()" after the instantiation for the compiler to let me do array indexing on the vector?

If I don't include that one push_back(), the program just crashes. (it compiles and runs, but then it stops responding and doesn't run).

Also, is there like... a general way to figure out quirks like this? I tried looking on google and on C++ reference sites, but I still end up wasting lots of time on little things like this. :(
Last edited on
Also, could anyone explain to me what it means that arrays must have a size that is constant at compile-time?

I assumed that meant that the *actual number* that is the size of the array must be specified at compile-time, but that can't be the case as the following code works:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
using namespace std;
#include <vector>

int main(){
int a=0;

fprintf(stdout, "input"); fflush(stdout);
scanf("%i", &a);

int arr1[a];

for (int i=0; i<a; i++){
	arr1[i] = i;
	cout << arr1[i] << endl;
}

	return 0;
}


/*outputs
0
1
2
3
4
...
(a-1)
*/
Last edited on
1
2
3
4
vec1[1] = 1;
vec1[2] = 2;
vec1[3] = 3;
vec1[4] = 4;
These are out of bounds. If you used vec1.at(1); or the other position it would have thrown and exception saying out of bounds.

Basically push_back inserts an item and operator[] just accesses(returns a reference to the object). You generally will use push_back and emplace_back the most but there are also insert and emplace to add elements. http://www.cplusplus.com/reference/vector/vector/

1
2
3
scanf("%i", &a);

int arr1[a];
This is a static size so it has to know how much memory to reserve. It should have a constant value for the size like:
1
2
int const size = 10;
int array[size];
If you wish to do it the way you are you must use a dynamic array (either a templated container like vector or pointer and new).

1
2
3
4
5
6
7
8
int a = 0;
//read in value of a

int *arr1 = new int[a];


//when you are done
delete [] arr1;
Last edited on
Thank you!

:)
zaqwsx170 wrote:
So yeah. vector.size() is apparently not giving me correct results when I use array indexing. It works fine when I use vector.push_back().

Is it supposed to do this, or is something wrong?


It is supposed to do this. vector elements do not exist until you add them to the vector, and they may not be accessed when they do not exist. Using the [index] or .at(index) notation is only for accessing existing elements.


zaqwsx170 wrote:
ALSO, could anyone explain to me why I have to do a "vec1.push_back()" after the instantiation for the compiler to let me do array indexing on the vector?

With a default constructed vector, it is possible that there is no memory at all allocated for elements. In that case, using the [] notation might result in a null pointer being dereferenced; whereas if you do a single push_back first, you know that memory for at least one element will be allocated.

You could've written the code like so:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <vector>

int main(){
    using namespace std;

    // option 1:
    // vector<int> vec1(5) ;
    // vec1[0] = 0;
    // vec1[1] = 1;
    // vec1[2] = 2;
    // vec1[3] = 3;
    // vec1[4] = 4;

    // option 2:
    // vector<int> vec1 ;
    // vec1.resize(5) ;
    // vec1[0] = 0;
    // vec1[1] = 1;
    // vec1[2] = 2;
    // vec1[3] = 3;
    // vec1[4] = 4;

    // option 3:
    vector<int> vec1 = { 0, 1, 2, 3, 4 };
    
    cout << vec1.size() << endl; // outputs 1 
    cout << vec1[3] << endl; //outputs 3

    return 0;
}


zaqwsx170 wrote:
Also, could anyone explain to me what it means that arrays must have a size that is constant at compile-time?

Exactly as you've said. The number must be determinable at compile time. Your compiler provides an extension which allows what you've done, but may be disabled with appropriate compiler options.
Last edited on
Topic archived. No new replies allowed.