Vectors

How do I prompt the user for 10 floating point values and then storing them in the vector?
This is as far as I got.I am not sure if it is correct.
1
2
3
4
#include <iostream>
using namespace std;

apvector <float> a(10);
In my opinion, if you already know the number of input you require, just use an array instead of vectors

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
#include<iostream>
#include <vector>
using namespace std;

typedef vector<float> apvector;

int main()
{
  float apArr[10];
  apvector a(10);
  int c;
  
  for(c = 0; c < 10; ++c)
  {
    cin >> apArr[c];
    //cin >> a.at(c) <---safer approach
    //cin >> a[c]; <---faster
    a.at(c) = apArr[c];
    //apArr[c] = a.at(c);
    //apArr[c] = a[c]
  }
  
  cout << "\nArray\tVector\n";
  for (c = 0; c < 10; ++c)
    cout << apArr[c] << "\t" << a.at(c) << endl;
  
  return 0;
}
Last edited on
Well, the assignment requires it to be a vector.
Topic archived. No new replies allowed.