这是代码
#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 ==========
答案 0 :(得分:5)
ostream_iterator
需要一个模板参数;在这种情况下使用ostream_iterator<string>
。
答案 1 :(得分:1)
我写这个例子。
// ostream_iterator example
#include<iostream>
#include<iterator>
#include<string>
#include<vector>
using namespace std;
int main(void)
{
vector<string> svec;
string mystr;
cout << "Enter the strings (Ctrl + Z to end):";
while(cin >> mystr)
{
svec.push_back(mystr);
}
ostream_iterator<string> out_it(cout, "\n"); // be careful. not like yours
copy(svec.begin(), svec.end(), out_it);
return 0;
}
我很好地编译。