Question

I'm trying to create a vector where each element is a multiple of 3 below 1000. I tried two ways, only one of which worked. The non-functioning way was:

int main() {
    vector<int> multiples_of_three;
    for (int i = 0; i <= 1000/3; ++i)
        multiples_of_three[i] = 3*i;
        cout << multiples_of_three[i] << "\n";
}

That gave an out of range error specifically on multiples_of_three[i]. This next bit of code worked:

int main() {
    vector<int> multiples_of_three(334);
    for (int i = 0; i <  multiples_of_three.size(); ++i) {
        multiples_of_three[i] = 3*i;
        cout <<  multiples_of_three[i];
}

So if I defined the size of the vector I could keep it within it's constraints. Why is it that if I try and let the for loop dictate the number of elements I get an out of range error?

Thanks!

Was it helpful?

Solution

This works perfectly fine:

#include <iostream>
#include <vector>
using namespace std;

//this one is the edited version

int main() {
    vector<int> multiples_of_three(334);      //notice the change: I declared the size
    for (int i = 0; i <= 1000 / 3; ++i){
        multiples_of_three[i] = 3 * i;
        cout << multiples_of_three[i] << "\n";
    }
    system("pause");
}


Consider these two examples below:

//=========================the following example has errors =====================
int main() {

    vector<int> multiples_of_three;
    multiples_of_three[0] = 0;  // error
    multiples_of_three[1] = 3;  // error

    cout << "Here they are: " << multiples_of_three[0]; cout << endl;
    cout << "Here they are: " << multiples_of_three[1]; cout << endl;

    cout << endl;
    system("pause");

return 0;
}
//============================the following example works==========================

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

    cout << "Here they are: " << multiples_of_three[0]; cout << endl;
    cout << "Here they are: " << multiples_of_three[1]; cout << endl;

    cout << endl;
    system("pause");
return 0;
}

So unless, you have declared the size, never use indices directly for assigning values (as in the first example). However, if the values have already been assigned, you can use indices to retrieve the values (as in the 2nd example). And in case, You want to use indices to assign values, first declare the size of the array (as in the edited version)!

OTHER TIPS

the default constructor (that is called here: vector<int> multiples_of_three;) creates an empty vector. you can populate them with push_back or better if you know the number of objects you have to add, pass that number to the constructor, so it reserves the required amount of memory at once instead of constantly growing (that means allocating memory and copying the old menory into the new) the vector.

another alternative is to call reserve from the empty default contructed vector and use push_back to populate it. reserve reserves enough memory to keep the required amount of objects but without changing the size of the vector. advantage of reserve is that the default constructor is not called for every object (as it will be done with resize or parameterized constructor) that would be not necessary since you overwrite the the object in your initialization loop right after the creation of the vector.

You need to use push_back() rather than add via the indexer.

The indexer can be used for read/write access to a vector only within the bounds.

A vector doesn't magically grow in size because you use []. It started out with 0 elements in the first example and you never grew it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top