如何判断输出迭代器是否被修改
我有一个采用以下形式的模板函数:
template < class ITER1, class ITER2 >
bool example(ITER1 Input1, ITER1 Input2, ITER2 Output)
{
ITER2 OrigOutput(Output);
// ...
std::copy(Input1, Input2, Output);
return (OrigOutput != Output);
}
我像这样调用 example()
:
std::vector < int > Input;
std::set < int > Output;
if (example(Input.begin(), Input.end(), inserter(Output, Output.begin())))
{
...
}
我希望 example()
返回 true< /code> 如果元素已插入到
Output
中,但是我收到编译错误 (msvc 2008):
Error 1 error C2678: binary '!=' : no operator found which takes a left-hand
operand of type 'std::insert_iterator<_Container>' (or there is no acceptable
conversion)
有没有办法确定是否有任何元素已插入到输出迭代器中为了返回正确的布尔值?
I have a template function that takes the following form:
template < class ITER1, class ITER2 >
bool example(ITER1 Input1, ITER1 Input2, ITER2 Output)
{
ITER2 OrigOutput(Output);
// ...
std::copy(Input1, Input2, Output);
return (OrigOutput != Output);
}
And I'm calling example()
like this:
std::vector < int > Input;
std::set < int > Output;
if (example(Input.begin(), Input.end(), inserter(Output, Output.begin())))
{
...
}
I'd like example()
to return true
if elements have been inserted into Output
, however I'm getting a compile error (msvc 2008):
Error 1 error C2678: binary '!=' : no operator found which takes a left-hand
operand of type 'std::insert_iterator<_Container>' (or there is no acceptable
conversion)
Is there a way I can determine whether any elements were inserted into the output iterator in order to return the correct bool value?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
编写一个包装迭代器,它委派给另一个迭代器,您可以从插入迭代器构造该迭代器。然后,您的委托可以在
operator=
方法中将modified
成员设置为 true。类似这样的事情:
Write a wrapper iterator which delgates to another iterator, which you would construct from your insert iterator. Your delegate then can set a
modified
member to be true in theoperator=
method.Something along the lines of this:
OutputIterator 不允许这样做。但就您而言,是什么阻止您返回
Input1 != Input2
?OutputIterators don't allow that. But in your case, what prevent you to return
Input1 != Input2
?您至少有以下两个选择:
inserter
,它采用额外的布尔引用参数作为参数,如果cont.insert (...).second 则设置为 true
是真的。如果大小成本不是 O(1),则第二个选项具有潜在的性能考虑。
You have at least the following two options:
inserter
which takes an extra boolean reference parameter as an argument which is set to true ifcont.insert (...).second
is true.The second options has potential performance considerations if the cost of size is not O(1).