中的ostream迭代器用法

中的ostream迭代器用法

这是代码

#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <fstream>
#include <iterator>
using std::vector;
using std::string;
using std::cout;
using std::cin;
using std::ostream_iterator;
using std::cout;

int main(){
     vector <string> me;
     string s;
     while ((cin>>s) && s!="end")
         me.push_back(s);

     std::copy(me.begin(),me.end(),
         std::ostream_iterator(cout,"\n"));



     return 0;
}

但是这里有错误
1>------ Build started: Project: stl_implementation, Configuration: Debug Win32 ------
1>  implementation.cpp
1>c:\users\david\documents\visual studio 2010\projects\stl_implementation\implementation.cpp(21): error C2955: 'std::ostream_iterator' : use of class template requires template argument list
1>          c:\program files\microsoft visual studio 10.0\vc\include\iterator(516) : see declaration of 'std::ostream_iterator'
1>c:\users\david\documents\visual studio 2010\projects\stl_implementation\implementation.cpp(21): error C2955: 'std::ostream_iterator' : use of class template requires template argument list
1>          c:\program files\microsoft visual studio 10.0\vc\include\iterator(516) : see declaration of 'std::ostream_iterator'
1>c:\users\david\documents\visual studio 2010\projects\stl_implementation\implementation.cpp(21): fatal error C1903: unable to recover from previous error(s); stopping compilation
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

最佳答案

ostream_iterator需要模板参数;在这种情况下,请使用ostream_iterator<string>

关于c++ - C++中的ostream迭代器用法,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/3378410/

10-11 18:40