Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
410 views
in Technique[技术] by (71.8m points)

c++ - 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;
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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"));
//                                                                             ^^^^

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...