迭代器的基于范围的循环 - 为什么这会抱怨?

时间:2015-02-27 10:30:26

标签: c++ c++11 iterator

基本上,我有一个2D矢量:

std::vector<std::vector<double> > vect = {
    {1, 2, 3},
    {4, 5, 6}, 
    {7, 8, 9}
};

我使用迭代器将此向量传递给函数:

template<typename Inverse>
void Diag(Inverse begin, Inverse end)
{
    for(auto row : begin)
    {
        for(auto col : row)
        {

        }
    }   
}

我传递给像Diag(std::begin(vect), std::end(vect))

这样的函数

但是我一直抱怨没有匹配的功能,即使我已经看到类似的基于范围的循环Here

可以找到一个例子Here

编辑:

错误讯息:

prog.cpp: In instantiation of 'void Diag(Inverse, Inverse) [with Inverse = __gnu_cxx::__normal_iterator<std::vector<double>*, std::vector<std::vector<double> > >]':
prog.cpp:30:39:   required from here
prog.cpp:10:2: error: no matching function for call to 'begin(__gnu_cxx::__normal_iterator<std::vector<double>*, std::vector<std::vector<double> > >*&)'
  for(auto row : &begin)
  ^
prog.cpp:10:2: note: candidates are:
In file included from /usr/include/c++/4.9/bits/basic_string.h:42:0,
                 from /usr/include/c++/4.9/string:52,
                 from /usr/include/c++/4.9/bits/locale_classes.h:40,
                 from /usr/include/c++/4.9/bits/ios_base.h:41,
                 from /usr/include/c++/4.9/ios:42,
                 from /usr/include/c++/4.9/ostream:38,
                 from /usr/include/c++/4.9/iostream:39,
                 from prog.cpp:1:
/usr/include/c++/4.9/initializer_list:89:5: note: template<class _Tp> constexpr const _Tp* std::begin(std::initializer_list<_Tp>)
     begin(initializer_list<_Tp> __ils) noexcept
     ^
/usr/include/c++/4.9/initializer_list:89:5: note:   template argument deduction/substitution failed:

2 个答案:

答案 0 :(得分:1)

有关详细信息,请参阅帖子后面的评论......

template<typename Inverse>
void Diag(Inverse begin, Inverse end)
{
    for(auto row = begin; row != end; row++)
    {
        for(auto col : *row)
        {

        }
    }
}

答案 1 :(得分:0)

在这个答案中,我忽略了这样一个事实:错误信息来自与您向我们展示的代码不同的代码。但是,如果您已经默默地将for(auto row : &begin)更改为for(auto row : begin),则可以恢复同步。

问题是ranged-for需要一个容器,但是你为它提供了一个迭代器。 There are ways to pass (begin,end) into the loop,但最简单的解决方案是简单地将外部ranged-for与更传统的循环交换。

为了表现,不要忘记按引用进行迭代:

#include <vector>
#include <iostream>

std::vector<std::vector<double> > vect = {
    {1, 2, 3},
    {4, 5, 6}, 
    {7, 8, 9}
};

template<typename Inverse>
void Diag(Inverse begin, Inverse end)
{
    for(auto it = begin; it != end; ++it)
        for(auto& col : *it)
            std::cout << col << ' ';
}

int main()
{
    Diag(std::begin(vect), std::end(vect));
}

live demo