47

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.

peterh
  • 11,875
  • 18
  • 85
  • 108
Amber Roxanna
  • 1,665
  • 4
  • 24
  • 30

5 Answers5

59

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.

Community
  • 1
  • 1
Mark Garcia
  • 17,424
  • 4
  • 58
  • 94
  • 1
    As my edit suggested, this answer is wrong. You do not Need to use push_back, you May. You Can use insert as is obvious. – metamorphosis Feb 18 '19 at 03:13
6

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;
Yang
  • 7,712
  • 9
  • 48
  • 65
  • The problem is that I keep receiving the following error message whenever i decide to use a method: "request for 'push_back' in myVector which is of non-class type std::" – Amber Roxanna Aug 01 '13 at 02:32
  • @AmberRoxanna Use `std::vector myVector;` instead of `std::vector myVector();` – Yang Aug 01 '13 at 02:36
4

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).

Nikunj
  • 41
  • 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);
}
J-Alex
  • 6,881
  • 10
  • 46
  • 64
2

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;
}
JRbarros
  • 41
  • 6