【问题标题】:Range based loops for iterators - Why is this complaining?迭代器的基于范围的循环 - 为什么会抱怨?
【发布时间】:2015-04-30 00:52:17
【问题描述】:

基本上,我有一个二维向量:

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:

【问题讨论】:

  • 谁投诉了?你老婆?
  • @thang 编译器,对不起哈哈!
  • 发布来自编译器的确切错误消息。
  • @thang 更新了我的帖子 :)
  • 你不想从头到尾吗? begin 不是一个集合,所以你不能使用 for(auto row : begin) 之类的集合循环

标签: c++ c++11 iterator


【解决方案1】:

详情请看帖子后面的cmets...

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

        }
    }
}

【讨论】:

  • 请将这些 cmets 带入您的答案中。评论将随时消失。反正我没有看到任何“细节”……
【解决方案2】:

在此答案中,我忽略了错误消息来自与您向我们展示的代码不同的代码的事实。但是,将 for(auto row : &amp;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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-20
    • 1970-01-01
    • 2020-04-03
    相关资源
    最近更新 更多