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

后端 未结 4 2194
[愿得一人]
[愿得一人] 2020-12-15 16:14

I am trying to create an empty vector inside a loop and want to add an element to the vector each time something is read in to that loop.

#include 

        
4条回答
  •  心在旅途
    2020-12-15 16:43

    Use push_back:

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

    The insert function takes an iterator as the first argument, indicating the position to insert.

    Also, you need to get rid of the parentheses in the declaration of myVector:

    std::vector myVector;
    

提交回复
热议问题