c++ - how do i add elements to an empty vector in a loop? -


i trying create empty vector inside loop , want add element vector each time read in loop.

#include <iostream> #include <vector>  using namespace std;  int main() {    std::vector<float> myvector();     float x;    while(cin >> x)       myvector.insert(x);     return 0; } 

but keeps giving error messages.

you need use std::vector::push_back() instead:

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

and not std::vector::insert(), which, can see in link, needs iterator indicate position want insert element.

also, what @joel has commented, should remove parentheses in vector variable's definition.

std::vector<float> myvector; 

and not

std::vector<float> myvector(); 

by doing latter, run c++'s most vexing parse problem.


Comments

Popular posts from this blog

basic authentication with http post params android -

vb.net - Virtual Keyboard commands -

How to get multiresult with multicondition in Sql Server -