1. ## Help!A simple question...

hi,i am just a novice to cpp programming.
here is a simple program to store 25 floating-numbers using a vector and then output all the elements in the vector.
when i excute it , some problem arise and i don't know why
Could anyone tell me why???thx~~~

Code:
// This program asks user to input 25 floating numbers and then output them

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

int main(){
vector<float> v;
int i=0;
while(i<3){
cin >> v[i] ;
i++;
}
cout << "\n";

cout << v[0] << endl;

for(int j=0;j<v.size();j++){
cout << v[j] << endl;
}

return 0;
}//

2. > This program asks user to input 25 floating numbers and then output them
> while(i<3){
Comment says 25, code says 3
Is that the problem?

If not, try being specific and say what the problem really is, rather than just "a problem"

3. Ignoring the whole 25 vs 3 debate for the moment (assuming 3 elements for purposes of the examples below)...

You haven't allocated any space for the vector. You need to reserve space for 3 elements before you can start adding them to the vector using that syntax. You can reserve space when you declare the vector or you can call the reserve member function at a later time (but before you start adding the elements). You can also just cin the value from the user into a temp float variable and then call the push_back member function on that variable:

Code:
vector<float> v(3);  // Reserves space for 3 floats
int i = 0;
while(i<3){
cin >> v[i] ;
i++;
}

or...

vector<float> v;
v.reserve(3);// Reserves space for 3 floats
int i = 0;
while(i<3){
cin >> v[i] ;
i++;
}

or...

vector<float> v;
int i = 0;
float temp;
while(i<3){
cin >> temp;
v.push_back(temp);
i++;
}

4. Originally Posted by hk_mp5kpdw
Ignoring the whole 25 vs 3 debate for the moment (assuming 3 elements for purposes of the examples below)...

You haven't allocated any space for the vector. You need to reserve space for 3 elements before you can start adding them to the vector using that syntax. You can reserve space when you declare the vector or you can call the reserve member function at a later time (but before you start adding the elements). You can also just cin the value from the user into a temp float variable and then call the push_back member function on that variable:
This is not correct. There is a difference between reserve and resize. The reserve function affects the capacity of the vector (how much memory is reserved) and the resize function affects the size of the vector (how many elements exist).

The first example sets the vector's initial size to 3, with all three values being default constructed (to 0.0f in this case). The capacity could be anything 3 or higher. It works because in the loop you are changing the value from 0.0f to whatever the user inputs.

The second example sets the initial size of the vector to 0, then makes sure there is enough memory for 3 elements. It does not actually create 3 elements, so when you try to change the value of the elements in the loop, you will get undefined behavior. If you check the size of the vector after the loop it should still be 0.

The third example does not set the initial size or reserve space. By using push_back, memory is allocated as necessary, and new objects are created at the time you push them back. This example works because push_back creates new elements, whereas operator[] just tries to access existing elements.

To fix the second example, change reserve to resize, and do the same in the explanation.

As for the OP, I'd suggest the first example, or a combination of the last two - using reserve and push_back.

5. You could try asking for the numbers.

6. Yep, the second example is wrong, should use resize and not reserve, my mistake... I was rushing out the door when I posted.