无法复制到输出迭代器
Cannot copy to the output iterator
Helo。有人能告诉我为什么我可以分配给输出操作符,但不能对它执行复制吗?Copy需要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;
}
问题是这是
*os++ = string(i, j);
但事实并非如此:
copy(i, j, os);
*os++ = string(i, j);
这一行从两个字符的迭代器中创建一个字符串,并将其写入输出迭代器。
copy(i, j, os);
此行尝试将迭代器范围中的每个字符写入输出迭代器。
这意味着,当第一行将字符串写入输出迭代器时,第二行尝试写入单个字符。这两种类型不兼容,尤其是输出迭代器只接受字符串。这就是问题所在。
相关文章:
- 集合上的输出迭代器:assign和increment迭代器
- 在静态库中使用输出迭代器时出现链接器错误
- 在插入容器之前转换输出迭代器
- 根据 boost::iterator_facade 定义基于代理的输出迭代器
- C++:为什么输出迭代器不支持比较操作?
- 输入迭代器可以重复读取,而输出迭代器只能写入一次
- 是C 正向/BIDI/随机迭代器总是输出迭代器
- stl风格的算法:如何管理输出迭代器
- 如何在C++中获取任意 (STL) 输出迭代器的"assignable type"
- 无法复制到输出迭代器
- 如何在C++中输出迭代器
- 要写入输出迭代器的模板成员函数
- 输入和输出迭代器
- std::transform 中的输入迭代器和输出迭代器来自同一个容器是否安全?
- 使用输出迭代器不起作用的通用函数示例
- 仅移动输入和输出迭代器
- 用Python生成器替换c++ STL输出迭代器
- 使用先前已移动的输出迭代器调用move()是标准的c++吗?
- STL映射输出迭代器查找值
- 输入迭代器+输出迭代器+X=正向迭代器