简体   繁体   English

无法复制到输出迭代器

[英]Cannot copy to the output iterator

Helo. Could anyone tell me why can I assign to output operator but cannot perform copy on it? 谁能告诉我为什么我可以分配给输出运算符但不能对其执行复制? Copy needs OutputIterator as a thrid argument but I have got some weird errors that you can see here : http://cpp.sh/5akdx 复制需要OutputIterator作为主要参数,但是我有一些奇怪的错误,您可以在这里看到: 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;
}

The problem is that this works 问题是这可行

*os++ = string(i, j); 

But it does not: 但这不是:

copy(i, j, os); 
*os++ = string(i, j); 

This line creates one string from the two character iterators and writes it to the output iterator. 该行从两个字符迭代器创建一个字符串,并将其写入输出迭代器。

copy(i, j, os); 

This line attempts to write each character in the iterator range to the output iterator. 该行尝试将迭代器范围内的每个字符写入输出迭代器。

This means that while the first line writes a string to the output iterator, the second line attempts to write individual characters. 这意味着第一行将字符串写入输出迭代器,而第二行则尝试写入各个字符。 Those two types are not compatible, and in particular the output iterator only accepts strings. 这两种类型不兼容,特别是输出迭代器仅接受字符串。 That's the problem. 那就是问题所在。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM