[Solved] How to return the index where values change in a c++ vector?


There are a few ways to solve this, but I believe this should work:

std::vector<std::vector<int>> vec2D;
int index = -1;
// Identify the indices of the position vector and use that to identify the 
// correct indices of the vec. 
for (int i = 0; i != position.size(); ++i)
{
    // if the value at i of the position vector is 0, 
    // push_back the value at i of the vec vector into
    // the correct vector of vector.
    if (0 == position[i])
    {
        vec2D[index].push_back(vec[i])
    }
    else if (1 == position[i])
    {
        ++index; //increment to the next vector
        std::vector<int> temp;
        vec2D.push_back(temp);
        vec2D[index].push_back(vec[i])  
    }
}

3

solved How to return the index where values change in a c++ vector?