Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I iterate through a two dimensional vector

To iterate through and print the elements of a single dimensional vector I use,

vector<int> a;
for(vector<int>::iterator it=a.begin();it<a.end();it++)
    cout<<*it;

How do I do the same for a two dimensional Vector?

like image 369
Leo18 Avatar asked Jun 27 '26 06:06

Leo18


1 Answers

Or since we're using c++11...

#include <iostream>
#include <vector>

using namespace std;

int main() {

  vector<vector<int> > v = {{1,2}, {3,4}};
  for (const auto& inner : v) {
      for (const auto& item : inner) {
          cout << item << " ";
      }
  }
  cout << endl;

  return 0;
}
like image 160
Richard Hodges Avatar answered Jun 29 '26 21:06

Richard Hodges