Created
November 3, 2011 18:17
-
-
Save CrBoy/1337261 to your computer and use it in GitHub Desktop.
A simple implementation like Python's zip
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <list> | |
| #include <utility> | |
| #include <iostream> | |
| using namespace std; | |
| template<class T1, class T2> | |
| list< pair<T1,T2> > zip(const list<T1>& container1, const list<T2>& container2) | |
| { | |
| list< pair<T1,T2> > result; | |
| typename list<T1>::const_iterator it1; | |
| typename list<T2>::const_iterator it2; | |
| for(it1=container1.begin(), it2=container2.begin(); it1!=container1.end() && it2!=container2.end(); it1++, it2++) | |
| result.push_back(make_pair(*it1, *it2)); | |
| return result; | |
| } | |
| int main(int argc, const char *argv[]) | |
| { | |
| int a[] = {1,2,3,4,5,6,7}; | |
| double b[] = {9.0,8.0,7.0,6.0,5.0,4.0,3.0,2.0,1.0}; | |
| list<int> c1(a, a+7); | |
| list<double> c2(b,b+9); | |
| list< pair<int,double> > result = zip(c1,c2); | |
| list< pair<int,double> >::const_iterator iter; | |
| for(iter=result.begin(); iter!=result.end(); iter++) | |
| cout << "[" << iter->first << ", " << iter->second << "]" << endl; | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment