i have a vector initialized without a size.

vector<int> Vector1;

i initialized the first 5 values via .push_back()
and i am supposed to do this next:

Externally initialize(using a single cin statement) the next four items to: 50 65 77 32760

This doesn't seem to work:

cout << "Enter these four numbers: 50 65 77 32760 :";
cin >> Vector1.push_back() >> Vector1.push_back() >> Vector1.push_back();

what am I doing wrong/how do i fix this?

Recommended Answers

All 5 Replies

push_back requires that you pass an existing object as the argument:

vector<int> v;
int x;

cin>> x;
v.push_back ( x );

You solution is to use a loop:

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

i see thank you

i don't suppose there's another way to write

that just using cin >> .... >> ..... >> ...;

(when i ran the program the cin statement from above never failed)

>(when i ran the program the cin statement from above never failed)
It should have failed to compile first since push_back requires an argument and second since there's no overloaded operator>> that takes void as its right-side argument.

>i don't suppose there's another way to write that just using cin >> .... >> ..... >> ...;
Sure, it just doesn't look like you would expect and has some obscure behavior:

#include <algorithm>
#include <iostream>
#include <iterator>
#include <vector>

using namespace std;

int main()
{
  vector<int> v;

  copy ( istream_iterator<int> ( cin ),
    istream_iterator<int>(), back_inserter ( v ) );
  copy ( v.begin(), v.end(), ostream_iterator<int> ( cout, " " ) );
  cout<<endl;
}

ooh yikes
i can't use that cuz this is an intro class to c++. I'm going to email the TA. Thanks for your time though.

>i can't use that cuz this is an intro class to c++.
Then the answer is no. You still have to use temporary variables:

int x, y, z;

cin>> x >> y >> z;

v.push_back ( x );
v.push_back ( y );
v.push_back ( z );

>I'm going to email the TA.
I seriously doubt that the TA will tell you anything different. That's assuming, of course, that he even knows enough C++ to correctly answer your question.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.