简体   繁体   中英

display all values of a vector matrix in C++

I have a vector< vector< pair<int,int> > > and I want to print all its values I tried doing this with 2 iterators, but failed miserably on the 2nd one:

vector< vector< pair<int, int> > > list;
vector< vector< pair<int, int> > >::iterator it1;
vector< pair<int, int> >::iterator it2;

for( it1=list.begin(); it1<list.end(); ++it1 ){
  for( it2=it1.begin(); it2<it1.end(); ++it2 ){
    printf("%d, %d", *it2.first, *it2.second);
  }
}

any ideas on how to traverse the second vector as well? Thanks

maybe you meant something like this:

for( it1=list.begin(); it1 != list.end(); ++it1 )
{
  for( it2=it1->begin(); it2 != it1->end(); ++it2 )
  {
    printf("%d, %d", (*it2).first, (*it2).second);
  }
}
for (it1 = list.begin(); it1 != list.end(); ++it1) {
    vector< pair<int, int> >& v = *it1;      
    for (it2 = v.begin(); it2 != v.end(); ++it2) {
        printf("%d, %d", (*it2).first, (*it2).second);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM