C ++错误与'<<<'运算符(用于cout向量的内容)

时间:2015-07-11 21:43:44

标签: c++ c++11

我想在以下简单程序中cout我的某个向量的内容:

#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;
}

每当我尝试编译时,我都会收到此错误:

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

<<运营商的错误是什么?

2 个答案:

答案 0 :(得分:5)

您可以使用cout将整个矢量的内容发送至std::copy,如下所示:

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

请注意,您需要

#include<iterator>

为此。

答案 1 :(得分:1)

向量没有<<运算符。如果要打印矢量中包含的每个双精度数,则必须一次打印一个双精度数。您可以使用迭代器来执行此操作:

for (vector<double>::const_iterator it = quartile1.begin(); it != quartile1.end(); ++it)
{
    cout << *it << endl;
}