-2

I want to cout the contents of one of my vectors in the following simple program:

#include<iostream>
#include<ios>
#include<iomanip>
#include<string>
#include<algorithm>
#include<vector>
using namespace std;

int main()
{
    string name;
    double median;
    int x;
    vector<double> numb, quartile1, quartile2, quartile3, quartile4;

    cout << "Please start entering the numbers" << endl;
    while (cin >> x)
    {
        numb.push_back(x);
    }

    int size = numb.size();
    sort(numb.begin(), numb.end());
    for (int i = 0; i < size; i++)
    {
        double y = numb[(size / 4) - i];
        quartile1.push_back(y);
    }
    cout << quartile1; // Error here
    return 0;
}

Whenever I try to compile this I get this error:

Error   1   error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'std::vector<double,std::allocator<_Ty>>'
(or there is no acceptable conversion)
c:\users\hamza\documents\visual studio 2013\projects\project1\project1\source.cpp   30  1   Project1

2   IntelliSense: no operator "<<" matches these operands
operand types are: std::ostream << std::vector<double, std::allocator<double>>  
c:\Users\Hamza\Documents\Visual Studio 2013\Projects\Project1\Project1\Source.cpp   29  7   Project1

What is the error with the << operator?

intcreator
  • 4,206
  • 4
  • 21
  • 39
  • 4
    There is no `<<` operator for a vector on the right. What output are you expecting to see? – M.M Jul 11 '15 at 21:47
  • Take a look at http://stackoverflow.com/questions/10750057/c-printing-out-the-contents-of-a-vector there is no off-the-shelf overloaded operator for std::vector – Shmil The Cat Jul 11 '15 at 21:48

2 Answers2

5

You can send the contents of the whole vector to cout using std::copy as follows:

copy(quartile1.begin(), quartile1.end(), ostream_iterator<double>(cout, ", "));

Note that you need

#include<iterator>

for that.

vukung
  • 1,824
  • 10
  • 23
1

There is no << operator for a vector. If you want to print each double contained in the vector, you must print one double at a time. You can use an iterator to do that:

for (vector<double>::const_iterator it = quartile1.begin(); it != quartile1.end(); ++it)
{
    cout << *it << endl;
}
João Neves
  • 21
  • 1
  • 5