C ++ 11 for each loop with more than one variable

I would like to translate the following traditional loop into a C ++ 11 loop for each loop without additional loops:

int a[] = { 5, 6, 7, 8, 9, 10 };
int b[] = { 50, 60, 70, 80, 90, 100 };

// Swap a and b array elements 
for (int i = 0; i < sizeof(a)/sizeof(a[0]); i++)
{
   a[i] ^= b[i]; b[i] ^= a[i]; a[i] ^= b[i];
}

Is there any way that you can provide more than one variable in C ++ 11 for each loop, for example:

for (int i, int j : ...)
+3
source share
3 answers

There is no built-in way to do this. If you can use Boost, boost::combinewill work for the iteration of two (or more) bands simultaneously ( Do you offer boost offer make_zip_range? , How can I sort through two vectors simultaneously using BOOST_FOREACH? ):

for (boost::tuple<int&, int&> ij : boost::combine(a, b)) {
    int& i = boost::get<0>(ij);
    int& j = boost::get<1>(ij);
    // ...
}

, zipped . ++ 17 , :

for (auto [&i, &j] : boost::combine(a, b)) {
    // ...
}

, boost::range::for_each :

boost::range::for_each(a, b, [](int& i, int& j)
{
    // ...
});
+6

zip .

for(:) .

:

template<class It>
struct range_t {
  It b,e;
  It begin() const{ return b; }
  It end() const{ return e; }
  range_t without_front( std::size_t count = 1 ) const {
    return {std::next(begin()), end()};
  }
  bool empty() const { return begin()==end(); }
};
template<class It>
range_t<It> range( It b, It e ) { return {b,e}; }
template<class C>
auto range( C& c ) {
  using std::begin; using std::end;
  return range( begin(c), end(c) );
};

, (, ):

template<class R1, class R2>
struct double_foreach_iterator {
  R1 r1;
  R2 r2;
  void operator++() { r1 = r1.without_front(); r2 = r2.without_front(); }
  bool is_end() const { return r1.empty() || r2.empty(); }
  auto operator*()const {
    return std::tie( *r1.begin(), *r2.begin() );
  }
  using self=double_foreach_iterator;
  auto cur() const {
    return std::make_tuple( r1.begin(), r2.begin() );
  }
  friend bool operator==( self const& lhs, self const& rhs ) {
    if (lhs.is_end() || rhs.is_end())
      return lhs.is_end() == rhs.is_end();
    return lhs.cur() == rhs.cur(); 
  }
  friend bool operator!=( self const& lhs, self const& rhs ) {
    return !(lhs==rhs);
  }
};

:

template<class A, class B>
auto zip_iterate(
  A& a, B& b
) {
  auto r1 = range(a);
  auto r2 = range(b);
  auto r1end = range(r1.end(), r1.end());
  auto r2end = range(r2.end(), r2.end());

  using it = double_foreach_iterator<decltype(r1), decltype(r2)>;
  return range( it{r1, r2}, it{r1end, r2end} );
}

:

for (auto tup : zip_iterate(a, b)) {
  int& i = std::get<0>(tup);
  int& j = std::get<1>(tup);
  // ...
}

++ 17:

for (auto&& [i, j] : zip_iterate(a, b)) {
  // ...
}

zip , , .

.

+2

.

, , ++ 11 ( , , ).

( ), ( size()) ( ) , a std::pair std::reference_wrapper n- .

, , .

C, std::array. ++ 11, , std::array.

#include <array>
#include <vector>
#include <iostream>
#include <functional>


template <typename T1, typename T2>
class pairWrapper
 {
   public:

      using V1  = typename std::remove_reference<decltype((T1().at(0)))>::type;
      using V2  = typename std::remove_reference<decltype((T2().at(0)))>::type;
      using RW1 = std::reference_wrapper<V1>;
      using RW2 = std::reference_wrapper<V2>;

      class it
       {
         public:

            it (pairWrapper & pw0, std::size_t p0): pos{p0}, pw{pw0}
             { }

            it & operator++ ()
             { ++pos; return *this; }

            bool operator!= (const it & it0)
             { return pos != it0.pos; }

            std::pair<RW1, RW2> & operator* ()
             {
               static std::pair<RW1, RW2>
                  p{std::ref(pw.t1[0]), std::ref(pw.t2[0])};

               p.first  = std::ref(pw.t1[pos]);
               p.second = std::ref(pw.t2[pos]);

               return p;
             }

         private:

            std::size_t pos;

            pairWrapper &  pw;
       };

      it begin()
       { return it(*this, 0U); }

      it end()
       { return it(*this, len); }

      pairWrapper (T1 & t10, T2 & t20) : len{t10.size()}, t1{t10}, t2{t20}
       { if ( t20.size() != len ) throw std::logic_error("no same len"); }

   private:

      const std::size_t  len;

      T1 &  t1;
      T2 &  t2;
 };


template <typename T1, typename T2>
pairWrapper<T1, T2> makePairWrapper (T1 & t1, T2 & t2)
 { return pairWrapper<T1, T2>(t1, t2); }


int main()
 {
   std::vector<int>    v1 { 1, 2, 3, 4 };
   std::array<long, 4> v2 { { 11L, 22L, 33L, 44L } };

   for ( auto & p : makePairWrapper(v1, v2) )
    {
      std::cout << '{' << p.first << ", " << p.second << '}' << std::endl;

      p.first  +=  3;
      p.second += 55;
    }

   for ( const auto & i : v1 )
      std::cout << '[' << i << ']' << std::endl;

   for ( const auto & l : v2 )
      std::cout << '[' << l << ']' << std::endl;

   return 0;
 }

p.s:

0

Source: https://habr.com/ru/post/1649570/


All Articles