Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No matching constructor for initalization of 'ostream_iterator<int>'

for the code, why error, osteam_iterator is a template class ,why no matching constructor for initalization of 'ostream_iterator', please give a help , thank you. define ostream_iterator template > class _LIBCPP_VISIBLE ostream_iterator

int main(int argc, const char * argv[])
{
    vector<int> sentence1;
    sentence1.reserve(5);// 设置每次分配内存的大小

    sentence1.push_back(1);
    sentence1.push_back(2);
    sentence1.push_back(3);
    sentence1.push_back(4);
    sentence1.push_back(5);

    int c = 5;

    copy(sentence1.begin(), sentence1.end(), ostream_iterator<int>(cout, 1));
    cout << endl;
like image 419
Best Water Avatar asked Jul 24 '13 02:07

Best Water


2 Answers

ostream_iterator constructor takes const CharT* delim as second parameter:

ostream_iterator(ostream_type& stream, const CharT* delim) (1)

ostream_iterator(ostream_type& stream) (2)

To make your code work, you need to pass in a string:

std::copy(sentence1.begin(), sentence1.end(), std::ostream_iterator<int>(cout, "1"));
//                                                                             ^^^^
like image 162
billz Avatar answered Nov 09 '22 23:11

billz


The std::ostream_iterator takes a string as the second parameter to the constructor. This is the string that will be output after each integer in the sequence.

like image 43
Vaughn Cato Avatar answered Nov 09 '22 23:11

Vaughn Cato