BeeeOn Gateway  v2020.3.1-2-g6f737dc
Platform to interconnect the IoT world
Data Structures | Public Types | Public Member Functions
BeeeOn::Zip< Container1, Container2 > Class Template Reference

The class provides an elegant way to iterate over 2 containers, which size can be different. Both const and non-const containers are supported. Zip::Iterator provides special functions to find out of which of the iterators did not come to an end. Dereferencing of Zip::Iterator returns std::pair formed by the references to values of containers. The iterators of containers can be also returned from Zip::Iterator. More...

#include <ZipIterator.h>

Data Structures

class  Iterator
 

Public Types

using Iterator1 = decltype(std::declval< Container1 & >().begin())
 
using Iterator2 = decltype(std::declval< Container2 & >().begin())
 
using Value1 = decltype(*std::declval< Iterator1 >())
 
using Value2 = decltype(*std::declval< Iterator2 >())
 

Public Member Functions

 Zip (Container1 &container1, Container2 &container2)
 
Zip< Container1, Container2 >
::Iterator 
begin () const
 
Zip< Container1, Container2 >
::Iterator 
end () const
 
Iterator1 firstEnd () const
 
Iterator2 secondEnd () const
 

Detailed Description

template<typename Container1, typename Container2>
class BeeeOn::Zip< Container1, Container2 >

The class provides an elegant way to iterate over 2 containers, which size can be different. Both const and non-const containers are supported. Zip::Iterator provides special functions to find out of which of the iterators did not come to an end. Dereferencing of Zip::Iterator returns std::pair formed by the references to values of containers. The iterators of containers can be also returned from Zip::Iterator.

The begin method returns the Zip::Iterator formed by begins of iterators, but the end method is different. If one container is smaller than other, the method returns Zip::Iterator formed by end iterator of smaller container and begin iterator of larger container incremented by distance between begin and end of smaller iterator.

This class is not intended to iterate over large containers due to linear complexity of the std::distance. It is used to determine which of the containers is larger to calculate Zip.end();

vector<int> v{0, 1, 2};
const list<char> l{'a', 'b'};
Zip<vector<int>, const list<char>> zip(v, l);
auto it = zip.begin();
for (; it != zip.end(); ++it)
  cout << (*it).first << " " << (*it).second << endl;
if (it.hasBothEnded())
  return;
if (!it.hasFirstEnded()) {
  for (auto firstIt = it.firstIterator(); firstIt != zip.firstEnd(); ++firstIt)
    cout << *firstIt << endl;
}

The documentation for this class was generated from the following file: