C++ 基于距离的组合for-loop

C++ 基于距离的组合for-loop,c++,c++11,for-loop,C++,C++11,For Loop,如果两个for循环包含相同的指令集,是否可以组合两个for循环。在我的代码中,我经常重复这个过程:对两个容器执行相同的操作。在每种情况下,我都不想在两个循环中创建函数和调用 #include <array> using std::array; int main() { array<int, 10> a; array<int, 10> b; for (int i: a) { // set of instruct

如果两个for循环包含相同的指令集,是否可以组合两个for循环。在我的代码中,我经常重复这个过程:对两个容器执行相同的操作。在每种情况下,我都不想在两个循环中创建函数和调用

#include <array>
using std::array;

int main()
{
    array<int, 10> a;
    array<int, 10> b;

    for (int i: a)
    {
        // set of instructions
    }

    for (int i: b)
    {
        // set of instructions
    }

    return 0;
}
#包括
使用std::数组;
int main()
{
阵列a;
阵列b;
对于(int i:a)
{
//一套指令
}
对于(int i:b)
{
//一套指令
}
返回0;
}
三年后更新:即使我不明白我想要什么。为什么不使用函数、lambda函数或传统for循环呢。很抱歉,我无法澄清问题,但也不想删除它,因为下面有答案。

您可以使用lambda表达式为两个循环执行相同的指令:

auto&& body = [&](int i) {
    /* set of instructions */
};
for (int i : a) { body(i); }
for (int i : b) { body(i); }
与显式函数不同,lambda表达式可以访问所有局部变量。

您可以使用lambda表达式为两个循环执行相同的指令:

auto&& body = [&](int i) {
    /* set of instructions */
};
for (int i : a) { body(i); }
for (int i : b) { body(i); }

与显式函数不同,lambda表达式可以访问所有局部变量。

不,这是不可能的,因为数组是独立的

基于范围的for循环扩展为:

for ( auto it = a.begin(); a.end != it; ++ it )
{
  // instructions
}

如您所见,它创建了一个迭代器。不可能得到这样的迭代器,它在两个独立的数组上进行迭代。

不,这是不可能的,因为数组是独立的

基于范围的for循环扩展为:

for ( auto it = a.begin(); a.end != it; ++ it )
{
  // instructions
}

如您所见,它创建了一个迭代器。不可能得到这样一个迭代器,它迭代两个独立的数组。

我假设您要求的是如何在一个
for
循环中同时迭代两个数组。这可以通过使用来实现,结合使用,我们可以对

#include <array>
#include <iostream>

#include <boost/tuple/tuple.hpp>
#include <boost/range/iterator_range.hpp>
#include <boost/iterator/zip_iterator.hpp>

int main() 
{
  std::array<int, 4> a{{1, 2, 3, 4}};
  std::array<int, 4> b{{10, 20, 30, 40}};

  for(auto const& tup : 
        boost::make_iterator_range(
          boost::make_zip_iterator(boost::make_tuple(a.begin(), b.begin())),
          boost::make_zip_iterator(boost::make_tuple(a.end(), b.end())))) {
    std::cout << tup.get<0>() << ' ' << tup.get<1>() << '\n';
  }
}
#包括



另一方面,如果您只想单独迭代数组,但避免循环体中的代码重复,那么就创建一个函数,该函数需要一个(可能
const
)引用std::数组,然后对其进行迭代。

我假设您要求的是如何在单个
for
循环中同时对两个数组进行迭代。这可以通过使用来实现,结合使用,我们可以对

#include <array>
#include <iostream>

#include <boost/tuple/tuple.hpp>
#include <boost/range/iterator_range.hpp>
#include <boost/iterator/zip_iterator.hpp>

int main() 
{
  std::array<int, 4> a{{1, 2, 3, 4}};
  std::array<int, 4> b{{10, 20, 30, 40}};

  for(auto const& tup : 
        boost::make_iterator_range(
          boost::make_zip_iterator(boost::make_tuple(a.begin(), b.begin())),
          boost::make_zip_iterator(boost::make_tuple(a.end(), b.end())))) {
    std::cout << tup.get<0>() << ' ' << tup.get<1>() << '\n';
  }
}
#包括



另一方面,如果您只想单独迭代数组,但避免循环体中的代码重复,那么就创建一个函数,该函数需要一个(可能
const
)引用
std::array
,然后对其进行迭代。

我真的很好奇,如果使用默认循环,特别是基于范围的循环,您将如何实现这一点。您是否想过合并阵列,这将是昂贵的,但这将是您的第二个最佳选择;因为,正如大家所提到的,函数调用是最适合您的场景的。比如:

void loop_function(const std::array<int> &array) {
     for(int i : array) {
         // blah blah...    
     }   
}
void循环函数(常量std::数组和数组){
for(int i:array){
//胡说八道。。。
}   
}

我真的很好奇,如果使用一个默认循环,特别是一个基于范围的循环,您将如何实现这一点。您是否想过合并阵列,这将是昂贵的,但这将是您的第二个最佳选择;因为,正如大家所提到的,函数调用是最适合您的场景的。比如:

void loop_function(const std::array<int> &array) {
     for(int i : array) {
         // blah blah...    
     }   
}
void循环函数(常量std::数组和数组){
for(int i:array){
//胡说八道。。。
}   
}

OP有一个奇怪的要求,即函数调用不能在循环中进行,调用lambda与函数调用没有什么不同。@juanchopanza:有人想要什么和有人需要什么之间有区别。OP没有解释为什么他不想要函数调用。目前,人们只能猜测。然而,如上所示的lambda表达式与内联执行指令相比基本上没有缺点。OP有一个奇怪的要求,即函数调用不能在循环中进行,调用lambda与函数调用没有什么不同。@juanchopanza:有人想要什么和有人需要什么之间有区别。OP没有解释为什么他不想要函数调用。目前,人们只能猜测。但是,如上所示的lambda表达式与内联执行指令相比基本上没有缺点。您必须将数组组合成一个结构。您所说的组合是什么意思?您想同时迭代这两个数组,还是分别迭代它们,但不在循环中重复代码?为什么不进行函数调用?如果不想在循环中进行调用,请使用内联函数。这样你就可以编写一个函数而不需要调用。你必须把数组组合成一个结构。你说的组合是什么意思?您想同时迭代这两个数组,还是分别迭代它们,但不在循环中重复代码?为什么不进行函数调用?如果不想在循环中进行调用,请使用内联函数。通过这种方式,您可以编写函数,但仍然不进行调用。