迭代器的基于范围的循环 - 为什么这是抱怨?
Range based loops for iterators - Why is this complaining?
基本上,我有一个 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:
详情见post后的评论...
template<typename Inverse>
void Diag(Inverse begin, Inverse end)
{
for(auto row = begin; row != end; row++)
{
for(auto col : *row)
{
}
}
}
在这个回答中,我忽略了错误消息来自与您向我们展示的不同的代码段这一事实。但是,将 for(auto row : &begin)
更改为 for(auto row : begin)
就足以恢复同步。
问题是 ranged-for 需要一个容器,但您为它提供了一个迭代器。 There are ways to pass (begin,end)
into the loop,但最简单的解决方案是用更传统的循环简单地交换外部范围。
为了性能起见,也不要忘记通过引用进行迭代:
#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)
基本上,我有一个 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:
详情见post后的评论...
template<typename Inverse>
void Diag(Inverse begin, Inverse end)
{
for(auto row = begin; row != end; row++)
{
for(auto col : *row)
{
}
}
}
在这个回答中,我忽略了错误消息来自与您向我们展示的不同的代码段这一事实。但是,将 for(auto row : &begin)
更改为 for(auto row : begin)
就足以恢复同步。
问题是 ranged-for 需要一个容器,但您为它提供了一个迭代器。 There are ways to pass (begin,end)
into the loop,但最简单的解决方案是用更传统的循环简单地交换外部范围。
为了性能起见,也不要忘记通过引用进行迭代:
#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));
}