Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/solr/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++11 带boost::Adapter::indexed的基于范围的for循环_C++11_Boost Range - Fatal编程技术网

C++11 带boost::Adapter::indexed的基于范围的for循环

C++11 带boost::Adapter::indexed的基于范围的for循环,c++11,boost-range,C++11,Boost Range,基于C++11范围的for循环取消对迭代器的引用。这是否意味着将其与boost::adapters::index一起使用毫无意义?例如: boost::counting_range numbers(10,20); for(auto i : numbers | indexed(0)) { cout << "number = " i /* << " | index = " << i.index() */ // i is an integer! <

基于C++11范围的for循环取消对迭代器的引用。这是否意味着将其与
boost::adapters::index
一起使用毫无意义?例如:

boost::counting_range numbers(10,20);
for(auto i : numbers | indexed(0)) {
  cout << "number = " i 
  /* << " | index = " << i.index() */ // i is an integer!
  << "\n";
}
boost::计算范围数(10,20);
用于(自动i:编号|索引(0)){
cout简短的回答(正如评论中提到的每个人)是“是的,这毫无意义”。我也发现这很烦人。根据您的编程风格,您可能会喜欢我编写的“zipfor”包(只是一个标题):

它允许像这样的语法

std::vector v;
zipfor(x,i eachin v, icounter) {
   // use x as deferenced element of x
   // and i as index
}
不幸的是,我无法找到一种使用基于范围的语法的方法,只能求助于“zipfor”宏:(

标题最初是为以下内容设计的

std::vector v,w;
zipfor(x,y eachin v,w) {
   // x is element of v
   // y is element of w (both iterated in parallel)
}


我在经过充分优化的g++4.8上进行的测试表明,生成的代码并不比手工编写慢。

这在Boost 1.56(2014年8月发布)中得到了修复;元素间接位于
值类型
后面,带有
索引()
值()
成员函数

例如:

auto number=boost::counting_范围(10,20);
对于(自动i:numbers | boost::adapters::index())

std::cout
index
很糟糕,因为它添加了
索引()
方法,而不是从取消引用迭代器返回的值。:/Xeo确实如此。我不时需要范围内元素的索引。首先我感觉很糟糕。然后,我引入了一个计数器。如果可以使用普通的旧循环轻松访问容器,我会再次感觉很糟糕,并将基于范围的循环重写为普通循环Xoo提到的Boost索引是不好的。如果你不介意切换库,那么就有一些基于Python的ItRealToC的C++范围库,例如:注:自从Boost 1.56(2014年8月发布)以来,这是固定的;元素在<代码> > ValueSype类型< <代码>中使用<代码>索引()/<代码>和<代码>值()
成员函数。@gnzlbg,您是否能够将
boost::adapter::indexed
更正为
boost::adapters::indexed
,因为我花了一段时间才意识到
s
丢失了?
std::map m;
mapfor(k,v eachin m)
   // k is key and v is value of pair in m
auto numbers = boost::counting_range(10, 20);
for (auto i : numbers | boost::adaptors::indexed())
    std::cout << "number = " << i.value()
        << " | index = " << i.index() << "\n";