Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/265.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++ 改变boost::combine的结果_C++_Boost_Compiler Errors_C++17 - Fatal编程技术网

C++ 改变boost::combine的结果

C++ 改变boost::combine的结果,c++,boost,compiler-errors,c++17,C++,Boost,Compiler Errors,C++17,我希望下面的代码在基于for循环的范围之后编译并修改v1的值为{7,9,11,13,15} #include <boost/range/combine.hpp> #include <vector> int main() { std::vector<int> v1{1, 2, 3, 4, 5}; std::vector<int> v2{6, 7, 8, 9, 10}; for(auto&& [a, b] : boost:

我希望下面的代码在基于for循环的范围之后编译并修改
v1
的值为
{7,9,11,13,15}

#include <boost/range/combine.hpp>
#include <vector>

int main()
{
  std::vector<int> v1{1, 2, 3, 4, 5};
  std::vector<int> v2{6, 7, 8, 9, 10};
  for(auto&& [a, b] : boost::combine(v1, v2)) {
    a += b;
  }


  return 0;
}

如何实现这一点?

因为
b
tuple
(在boost中,它是内部
cons
辅助模板,以两个参数作为头和尾),其头指的是
int
(作为原始tuple的第二个字段-由
combine
返回),您可以使用
boost::get
阅读以下内容:

  for(auto&& [a, b] : boost::combine(v1, v2)) {
    a += boost::get<0>(b);
  }
for(auto&&[a,b]:boost::combine(v1,v2)){
a+=boost::get(b);
}


在boosttuple站点上,我们可以阅读

元组在内部表示为cons列表。例如 元组

tuple
从类型继承

cons

当对
boost::combine
auto&q
返回的所有元素进行迭代时,
q
tuple
,通过调用
get(q)
(其中
N
可以是0或1),我们得到
int&


但是在结构化绑定版本-
auto&[a,b]
中,
a
指的是
int&
b
指的是boost internal
cons
struct,这就是为什么我们需要使用
get
从输入序列中访问第二个整数值的原因。

这说明了一些奇怪的事情(boost中的bug?)(auto&q:…){boost::get(q)+=boost::get(q)}的“standard”
for(auto&q:…){boost::get(q)+=boost::get(q)}
工作正常。应该正确阅读错误消息……我对您的答案非常满意(但对需要这个
boost::get
的事实完全不满意!)我想知道为什么
b
是一个元组?对我来说这是出乎意料的。也许这可以处理长度不均匀的序列。
  for(auto&& [a, b] : boost::combine(v1, v2)) {
    a += boost::get<0>(b);
  }