2015-07-11 101 views
-2

我想cout我載體之一的內容在下面的程序:C++誤差「<<」操作符(來清點向量的內容)

#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 

運算符<<的錯誤是什麼?

+4

右邊的矢量沒有'<<'運算符。你期望看到什麼輸出? –

+0

看看http://stackoverflow.com/questions/10750057/c-printing-out-the-contents-of-a-vector沒有現成的重載操作符爲std :: vector

回答

5

可以使用std::copy如下送全矢量的內容cout

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

請注意,您需要

#include<iterator> 

了點。

1

對於向量沒有<<運算符。如果要打印矢量中包含的每個雙精度值,則必須一次打印兩個精度值。你可以使用一個迭代器來做到這一點:

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