BeeeOn Gateway
v2020.3.1-2-g6f737dc
Platform to interconnect the IoT world
|
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 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 |
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; }