相关疑难解决方法(0)

为什么从C++ 11中删除了对范围访问?

我刚刚发现,在某一点上,C++ 11草案具有std::begin/ std::end重载std::pair,允许将一对迭代器视为适合在基于范围的for循环中使用的范围(N3126,第20.3.5.5节),但这有自从被删除.

有谁知道为什么它被删除了?

我发现删除非常不幸,因为似乎没有其他方法可以将一对迭代器视为范围.确实:

  • 在基于范围的for循环中开始/结束的查找规则表示在1)中作为范围对象2)的成员函数查找开始/结束作为"关联命名空间"中的自由函数
  • std::pair 没有开始/结束成员函数
  • 通常唯一关联的命名空间std::pair<T, U>是namespace std
  • 我们不允许超载std::begin/ std::endstd::pair自己
  • 我们不能专门化std::begin/ std::endfor std::pair(因为专业化必须是部分的,而且不允许使用函数)

还有其他一些我失踪的方式吗?

c++ foreach range c++11 std-pair

54
推荐指数
3
解决办法
8009
查看次数

基于对<Iterator,Iterator>的范围

我对以下答案有疑问:

/sf/answers/1108020651/

如上所述,我们不能像BGL这样使用基于范围的BGL:

   for(auto e : boost::edges(g))
       // do something with e
Run Code Online (Sandbox Code Playgroud)

但是,这里指出,我们可以重载使用基于语义的范围所需的begin()和end()函数.所以我尝试过:

   template<class I>
   I begin(std::pair<I,I>& p)
   { return p.first;}

   template<class I>
   I end(std::pair<I,I>& p)
   { return p.second;}
Run Code Online (Sandbox Code Playgroud)

但是,编译器仍抱怨:

错误:没有匹配函数来调用' begin(std::pair<some_really_ugly_type,some_really_ugly_type>&)'

我究竟做错了什么?名称查找不起作用吗?或者这毕竟不可能吗?我也找到了这个答案,这个答案是有效的,但是开头/结尾自由函数的过度也不应该是可能的吗?问候,马蒂

顺便说一句:我觉得写作真的很烦人

   typename Graph::edge_iterator ebegin, eend;
   std::tie(ebegin,eend) = boost::edges(_graph);
   std::for_each(ebegin,eend,[&](const edge_descriptor& e){/*do something with e*/;});
Run Code Online (Sandbox Code Playgroud)

更新:C++ 17现在应该允许以下内容:-)

auto [ebegin,eend] = boost::edges(_graph);
Run Code Online (Sandbox Code Playgroud)

c++ boost iterator for-loop

6
推荐指数
2
解决办法
4732
查看次数

标签 统计

c++ ×2

boost ×1

c++11 ×1

for-loop ×1

foreach ×1

iterator ×1

range ×1

std-pair ×1