dimanche 24 avril 2016

Iterating over all but the first element in a vector

Suppose we have a vector of vectors and we initialize all elements to 0.

vector<vector<int>> P(MyObjects.size() + 1, vector<int>(MyBag.MaxCapacity + 1, 0));

My question is:

Is it possible to iterate over a vector starting from 1 column and 1 changing in someway following code?

for (auto& row : P) //Tried to add (row + 1) : P but I'm receiving an Error
  {
    for (auto& elem : row) //Tried to add(elem + 1) : row but I'm receiving an Error
    {
       std::cout << elem << "  ";
    }
  }

I was looking for the answer here on SO and on Web but there was nothing even similiar to it.

I'm only interested in solutions which use auto

EDIT: Here is the output of Errors

main.cpp:74:18: error: expected ‘;’ before ‘+’ token
   for (auto& row +1 : P)
                  ^
main.cpp:74:21: error: expected ‘;’ before ‘:’ token
   for (auto& row +1 : P)
                     ^
main.cpp:74:21: error: expected primary-expression before ‘:’ token
main.cpp:74:21: error: expected ‘)’ before ‘:’ token
main.cpp:74:21: error: expected primary-expression before ‘:’ token

And there is a Code which I was trying to use

for (auto& row + 1 : P)
  {
    for (auto& elem + 1 : row)
    {
     std::cout << elem << "  ";
    }
  }

Yes I know that we can use the following syntax

for(vector< vector<int> >::iterator row = v.begin() + 1; row != v.end(); ++row) {
    for(vector<int>::iterator col = row->begin() + 1; col != row->end(); ++col) {
        cout << *col;
    }
}

but I do not want to use it.

Aucun commentaire:

Enregistrer un commentaire