c++ - Printing values using Iterator on 2d vector -


here code:

std::vector< std::vector<std::shared_ptr<int>> > om(2, std::vector<std::shared_ptr<int>>(2)); om[0][0] = std::shared_ptr<int>(std::make_shared<int>(1)); om[0][1] = std::shared_ptr<int>(std::make_shared<int>(2)); om[1][0] = std::shared_ptr<int>(std::make_shared<int>(3));       //init values om[1][1] = std::shared_ptr<int>(std::make_shared<int>(4));  std::vector<std::shared_ptr<int>>::iterator pd;                  //inner iterator std::vector< std::vector<std::shared_ptr<int>> >::iterator px;   //outer iterator   for(px = om.begin(); px != om.end(); px++){                         for(pd = (*px).begin(); pd !=  (*px).end(); pd++){      std::cout<< *pd[0] << std::endl;     }  } 

output:

1 2 3 4

i making 2d vector using shared_ptr, , goal print values

my questions:

from other examples have seen, need dereference iterator value, in case if use std::cout<< *pd << std::endl; print raw addresses isn't want.

but if use std::cout<< *pd[0] << std::endl; print right values.

i visualize :

[ [1][2], [3][4] ] 

wherein px iterate 2 blocks , pd have iterate 4 blocks in total.

why have put [0] in order print? or undefined?

since pd of type std::vector<std::shared_ptr<int>>::iterator, *pd of type std::shared_ptr<int>. must dereference once more integer value.

*pd[0] equivalent *(*(pd+0)), in turn equivalent **pd.


Comments