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

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 <iostream>
#include <vector>

using namespace std;

int main()
{
   std::vector<float> myVector();

   float x;
   while(cin >> x)
      myVector.insert(x);

   return 0;
}

But this is giving me error messages.



Solution 1:[1]

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<float> myVector;

Solution 2:[2]

If you want to use myVector.insert(), use it like myVector.insert(myVector.end(), x). This will append x at the end of myVector. You can insert x in the beginning by myVector.insert(myVector.begin(), x).

Solution 3:[3]

Another option is to use std::vector::emplace_back() instead of std::vector::push_back(). The makes some optimizations and doesn't take an argument of type vector::value_type, it takes variadic arguments that are forwarded to the constructor of the appended item, while push_back can make unnecessary copies or movements.

This is demonstrated in the std::vector::emplace_back documentation and here is a related question.

Usage example:

std::vector<int> myVector;

while (cin >> x) {
    myVector.emplace_back(x);
}

Solution 4:[4]

The code below may answer your question and also brings some other examples regarding how to insert new elements in different position or index.

#include <iostream>
#include <vector>

using namespace std;

int main()
{
    vector<int> vector_of_integers{};

    vector_of_integers.push_back(1); // O(1)
    vector_of_integers.push_back(3); // O(1)
    vector_of_integers.push_back(5); // O(1)
    vector_of_integers.push_back(7); // O(1)

    for (int i = 8; i <= 10; i++)
        vector_of_integers.push_back(i);

    // Printing out all the elements of vector of integers - Method 1
    copy(vector_of_integers.begin(), vector_of_integers.end(), ostream_iterator<int>(cout, " ")); // 1 3 5 7 8 9 10
    cout << endl << endl;

    // Inserting '2' at index 1
    vector<int>::iterator it{ vector_of_integers.begin() };
    advance(it, 1);
    vector_of_integers.insert(it, 2); // O(N+M) => M is size of elements to be inserted

    // Printing out all the elements of vector of integers - Method 2
    for (auto const& element : vector_of_integers)
        std::cout << element << " "; // 1 2 3 5 7 8 9 10
    cout << endl << endl;

    // "it" no longer valid, get a new one
    it = vector_of_integers.begin();
    vector_of_integers.insert(it + 4, 6); // O(N+M) => M is size of elements to be inserted

    // Printing out all the elements of vector of integers - Method 3
    for (it = vector_of_integers.begin(); it != vector_of_integers.end(); it++)
        std::cout << *it << ' '; // 1 2 3 5 6 7 8 9 10
    cout << endl << endl;

    // insert '4' 7 times at index 3
    vector<int> new_vector_to_be_inserted(7, 4);
    vector_of_integers.insert(vector_of_integers.begin() + 3, new_vector_to_be_inserted.begin(), new_vector_to_be_inserted.end()); // O(N+M) => M is size of elements to be inserted

    // Printing out all the elements of vector of integers - Method 4
    for (int i = 0; i < vector_of_integers.size(); i++)
        cout << vector_of_integers.at(i) << ' '; // 1 2 3 4 4 4 4 4 4 4 5 6 7 8 9 10
    cout << endl << endl;

    return 0;
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Nikunj
Solution 3 J-Alex
Solution 4