C ++从back_insert_iterator转换为仿函数中的迭代器 [英] C++ cast from back_insert_iterator to iterator within functors

查看:128
本文介绍了C ++从back_insert_iterator转换为仿函数中的迭代器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有点想要实现一种复制操作符。
目标是有两个类:一个用于浏览容器,另一个用于处理容器。 Browse类还维护(出于某种原因)输出容器上的迭代器,另一个可以用它来计算增量。

I'm somewhat trying to implement a kind of copy operator. The aim is to have two classes: one that browse a container, and one that do something with it. The Browse class also maintain (for some reason) an iterator on the ouput container, and the other one can compute an increment with it.

不幸的是,编译器似乎是无法将back_insert_iterator转换为输出迭代器。为什么?

Unfortunately, the compiler seems to be unable to convert a back_insert_iterator to the output iterator. Why?

#include <iostream>
#include <iterator>
#include <vector>

typedef std::vector<int> Vec;

// An operator that copy an item onto another
template< class TIN, class TOUT >
class DoCopy
{
    TOUT operator()( const typename TIN::iterator i_in, const typename TOUT::iterator i_out )
    {
        const typename TOUT::iterator i_incr = i_out;
        (*i_incr) = (*i_in);
        std::advance( i_incr, 1 );
        return i_incr;
    }
};

// The class that iterate over a container, calling an operator for each item
template< class TIN, class TOUT >
class Browse
{
    public:
        // We keep a reference to the operator that really do the job
        DoCopy<TIN,TOUT> & _do;
        Browse( DoCopy<TIN,TOUT> & op ) : _do(op) {}

        // Iterate over an input container
        TOUT operator()(
                const typename TIN::iterator in_start,
                const typename TIN::iterator in_end,
                const typename TOUT::iterator out_start
            )
        {
            TOUT i_out = out_start;

            for( TIN i_in = in_start; i_in != in_end; ++i_in ) {
                // it is not shown why here, but we DO want the operator to increment i_out
                i_out = _do(i_in, i_out);
            }
        }
};

int main()
{
    // in & out could be the same type or a different one
    Vec in;
    Vec out;
    DoCopy<Vec,Vec> do_copy;
    Browse<Vec,Vec> copy(do_copy);

    std::back_insert_iterator< Vec > insert_back(out);

    // Here, g++ cannot find the corresponding function :
    copy( in.begin(), in.end(), insert_back );

}

g ++无法编译时出现以下错误:

g++ fail to compile with the following errors:

$ g++ test.cpp && ./a.out
    test.cpp: In function ‘int main()’:
    test.cpp:54:49: erreur: no match for call to ‘(Browse<std::vector<int>, std::vector<int> >) (std::vector<int>::iterator, std::vector<int>::iterator, std::back_insert_iterator<std::vector<int> >&)’
    test.cpp:22:11: note: candidate is:
    test.cpp:30:18: note: TOUT Browse<TIN, TOUT>::operator()(typename TIN::iterator, typename TIN::iterator, typename TOUT::iterator) [with TIN = std::vector<int>, TOUT = std::vector<int>, typename TIN::iterator = __gnu_cxx::__normal_iterator<int*, std::vector<int> >, typename TOUT::iterator = __gnu_cxx::__normal_iterator<int*, std::vector<int> >]
    test.cpp:30:18: note:   no known conversion for argument 3 from ‘std::back_insert_iterator<std::vector<int> >’ to ‘__gnu_cxx::__normal_iterator<int*, std::vector<int> >’


推荐答案

以下是问题的主要来源: std :: back_insert_iterator< V氮化T> > std :: vector< T> :: iterator 在其继承树中没有直接关联:

Here is the main source of the problem: std::back_insert_iterator< V<T> > and std::vector<T>::iterator aren't directly related in their inheritance tree:


  • std :: vector< T> :: iterator __ normal_iterator< T> (没有其他超类)(查看 stl_vector.h 获取 std :: vector< T> :: iterator stl_iterator.h for __ normal_iterator

  • 的std :: back_insert_iterator< V氮化T> > 是一个迭代器(没有其他超类)(查看 stl_iterator.h for std :: back_insert_iterator stl_iterator_base_types.h for std :: iterator )。

  • std::vector<T>::iterator is a __normal_iterator<T> (no other super class) (look at stl_vector.h for std::vector<T>::iterator and stl_iterator.h for __normal_iterator)
  • std::back_insert_iterator< V<T> > is an iterator (no other super class) (look at stl_iterator.h for std::back_insert_iterator and stl_iterator_base_types.h for std::iterator).

它们无法向任何方向转换。

They can't be converted in any direction.

因此,第二个模板参数应直接为 std :: back_insert_iterator iterator<> ,带有良好的第一个参数,表明它是一个输出操作符。

Hence, the second template argument should directly be the std::back_insert_iterator or the iterator<> with the good first parameter indicating that's an output operator.

std :: advance(iterator,1),我假设你的意思是 ++ iterator ,这是转到迭代器的下一个元素的标准方法。

By std::advance( iterator, 1 ), I assume you meant ++iterator, which is the standard way to go to the next element for iterators.

此外,out迭代器不应该是 const ,否则他们没有实现做法 operator =

Furthermore, out iterators shouldn't be const, otherwise they don't implement the affectation operator=.

第38行, i_in 的类型应为 typename TIN :: iterator 而不是 TIN
浏览运算符()还必须返回out迭​​代器。

Line 38, the i_in should be of type typename TIN::iterator and not TIN. The Browse operator() must also return the out iterator.

最终代码如下所示:

#include <iostream>
#include <iterator>
#include <vector>

typedef std::vector<int> Vec;

// An operator that copy an item onto another
template< class TIN, class TOUT >
class DoCopy
{
    public:

    TOUT operator()( const typename TIN::iterator i_in, const TOUT i_out )
    {
        TOUT i_incr = i_out;
        (*i_incr) = (*i_in);
        //std::advance( i_incr, 1 );
        ++i_incr;
        return i_incr;
    }
};

// The class that iterate over a container, calling an operator for each item
template< class TIN, class TOUT >
class Browse
{
    public:
        // We keep a reference to the operator that really do the job
        DoCopy<TIN,TOUT> & _do;
        Browse( DoCopy<TIN,TOUT> & op ) : _do(op) {}

        // Iterate over an input container
        TOUT operator()(
                const typename TIN::iterator in_start,
                const typename TIN::iterator in_end,
                const TOUT out_start
            )
        {
            TOUT i_out = out_start;

            for( typename TIN::iterator i_in = in_start; i_in != in_end; ++i_in ) {
                // it is not shown why here, but we DO want the operator to increment i_out
                i_out = _do(i_in, i_out);
            }
            return i_out;
        }
};

int main()
{
    // in & out could be the same type or a different one
    Vec in;

    in.push_back(1);
    in.push_back(3);
    in.push_back(3);
    in.push_back(7);

    Vec out;
    DoCopy<Vec, std::back_insert_iterator<Vec> > do_copy;
    Browse<Vec, std::back_insert_iterator<Vec> > copy(do_copy);

    std::back_insert_iterator< Vec > insert_back(out);

    // Here, g++ cannot find the corresponding function :
    copy( in.begin(), in.end(), insert_back );

    for( unsigned i = 0, s = out.size(); i < s; ++i )
    {
        std::cout << out[i] << " ";
    }
    std::cout << std::endl;
}

感谢 clang ++ ,这使得C ++错误更加清晰。

Thanks to clang++ which makes C++ errors more clear.

这篇关于C ++从back_insert_iterator转换为仿函数中的迭代器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆