Helo. Könnte mir jemand sagen, warum ich dem Ausgabeoperator zuweisen kann, aber keine Kopie darauf ausführen kann? Kopieren Sie muss als thrid Argument OutputIterator aber ich habe einige seltsame Fehler bekam, die Sie hier sehen können: http://cpp.sh/5akdxKopieren in den Ausgabe-Iterator nicht möglich
#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;
}
Das Problem ist, dass diese
*os++ = string(i, j);
funktioniert, aber es funktioniert nicht:
copy(i, j, os);
Die eine, die nicht funktioniert, erfordert eine 'std :: ostream_iterator', beachten Sie die 'char', nicht' string'. 'i' und' j' wiederholen 'char' in einer' string'. –
WhozCraig