Python's zip() equivalent in C or C++ [duplica

2019-06-23 22:28发布

问题:

This question already has an answer here:

  • Sequence-zip function for c++11? 13 answers

I would like to zip two arrays in C++ (as in Python) using a standard library function, so is there any equivalent for Python's built-in function zip()?

回答1:

int **zip(int *arr1, int *arr2, int length)
{
    int **ret = new int*[length];
    for(int i = 0; i<length; i++)
    {
        ret[i] = new int[2];
        ret[i][0] = arr1[i];
        ret[i][1] = arr2[i];
    }
    return ret;
}