0

I want a fast method to clear(set a matrix with 0 everywhere) a matrix of type vector<vector<int>>, just simple as the initialization was:

vector<vector<int>> values(nr_rows, vector<int>(nr_cols, 0));

Can I do this without a for?

Roxana Istrate
  • 632
  • 1
  • 6
  • 18
  • 3
    IMO, you're probably starting from the wrong place. Is there a good reason you can't provide 2D addressing of a single vector instead? For example, see: http://stackoverflow.com/a/6465254/179910. With that as a starting point, it's pretty trivial to add a `clear` member function if you see fit (not necessarily a good idea, but easy anyway). – Jerry Coffin Oct 13 '14 at 20:11
  • A first improvement would be continuous memory - as @JerryCoffin suggests –  Oct 13 '14 at 20:13
  • I have a signature of a function that I am not able to modify and it's vector> – Roxana Istrate Oct 13 '14 at 21:12

1 Answers1

1
values.assign(nr_rows, vector<int>(nr_cols, 0));
Dark Falcon
  • 43,592
  • 5
  • 83
  • 98