How do I add elements to an empty vector in a loop?

You need to use std::vector::push_back() instead:

while(cin >> x)
  myVector.push_back(x);
//         ^^^^^^^^^

and not std::vector::insert(), which, as you can see in the link, needs an iterator to indicate the position where you want to insert the element.

Also, as what @Joel has commented, you should remove the parentheses in your vector variable’s definition.

std::vector<float> myVector;

and not

std::vector<float> myVector();

By doing the latter, you run into C++’s Most Vexing Parse problem.

Leave a Comment