0
Helo。有谁能告诉我为什么我可以分配给输出操作员,但不能在其上执行拷贝?复制需要输出迭代器作为THRID的说法,但我已经得到了一些奇怪的错误,你可以在这里看到:http://cpp.sh/5akdx无法复制到输出迭代器
#include <iostream>
#include <iterator>
#include <algorithm>
using namespace std;
bool space(const char &c) {
return c == ' ';
}
bool not_space(const char &c) {
return !space(c);
}
template<class Out>
void split(const string &str, Out os) {
typedef string::const_iterator iter;
iter i = str.begin();
while (i != str.end()) {
i = find_if(i, str.end(), not_space);
iter j = find_if(i, str.end(), space);
if (i != str.end())
//*os++ = string(i, j); //THIS WORKS
copy(i, j, os); //THIS DOESN'T WORK
i = j;
}
}
int main()
{
string s;
while (getline(cin, s))
split(s, ostream_iterator<string>(cout, "\n"));
return 0;
}
的问题是,这个工程
*os++ = string(i, j);
但事实并非如此:
不工作需要'std :: ostream_iterator',注意'char',而不是'string'。 'i'和'j'在字符串中迭代'char'。 –
WhozCraig