如何判断输出迭代器是否被修改

发布于 2024-08-03 00:26:21 字数 875 浏览 2 评论 0原文

我有一个采用以下形式的模板函数:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

青柠芒果 2024-08-10 00:26:21

编写一个包装迭代器,它委派给另一个迭代器,您可以从插入迭代器构造该迭代器。然后,您的委托可以在 operator= 方法中将 modified 成员设置为 true。

类似这样的事情:

template<typename ITER>
class ModifiedIterator : public std::iterator<std::output_iterator_tag, void, void, void, void>
{
protected:
    typedef ITER Iter;
    Iter iter;
    bool& isModified;

public:
    explicit ModifiedIterator (Iter i, bool& isMod)
    : iter(i), isModified (isMod)
    {}

    template<typename T>
    ModifiedIterator& operator= (const T& value)
    {
        iter = value;
        ++iter;
        isModified = true;
        return *this;
    }

    ModifiedIterator& operator* () { return *this; }
    ModifiedIterator& operator++ () { return *this; }
    ModifiedIterator operator++ (int) { return *this; }
};

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 the operator= method.

Something along the lines of this:

template<typename ITER>
class ModifiedIterator : public std::iterator<std::output_iterator_tag, void, void, void, void>
{
protected:
    typedef ITER Iter;
    Iter iter;
    bool& isModified;

public:
    explicit ModifiedIterator (Iter i, bool& isMod)
    : iter(i), isModified (isMod)
    {}

    template<typename T>
    ModifiedIterator& operator= (const T& value)
    {
        iter = value;
        ++iter;
        isModified = true;
        return *this;
    }

    ModifiedIterator& operator* () { return *this; }
    ModifiedIterator& operator++ () { return *this; }
    ModifiedIterator operator++ (int) { return *this; }
};
栀子花开つ 2024-08-10 00:26:21

OutputIterator 不允许这样做。但就您而言,是什么阻止您返回 Input1 != Input2

OutputIterators don't allow that. But in your case, what prevent you to return Input1 != Input2?

孤檠 2024-08-10 00:26:21

您至少有以下两个选择:

  1. 编写自己的版本 inserter ,它采用额外的布尔引用参数作为参数,如果 cont.insert (...).second 则设置为 true 是真的。
  2. 在调用者代码中的“if”语句之前,存储容器的大小。如果调用后有更多元素,那么您就知道复制插入了元素。

如果大小成本不是 O(1),则第二个选项具有潜在的性能考虑。

You have at least the following two options:

  1. Write your own version inserter which takes an extra boolean reference parameter as an argument which is set to true if cont.insert (...).second is true.
  2. Before the 'if' statement in the caller code, store the size of the container. If it has more elements after the call, then you know that the copy inserted elements.

The second options has potential performance considerations if the cost of size is not O(1).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文