Vector of Vector

Hi I am having difficult accessing the data in the code below.
1
2
3
4
5
struct Node
{
  int target;
  double weight;
};


1
2
3
4
5
6
7
 vector<vector<Node> > a;
 // Assuming I have fill the 2D vector with data, How can I perform a print to see all the data?
 for(int i = 0; i < a.size(); i++){
   for(int j = 0; j < a[i].size; j++)
    //Error here: cout << a[i].target << endl;
}
1
2
3
4
5
6
7
8
9
    for (std::vector<std::vector<Node> >::size_type i = 0; i < vvn.size(); ++i)
    {
        //for (std::vector<Node>::size_type j = 0; j < vvn.at(i).size(); ++j)
        for (std::vector<Node>::size_type j = 0; j < vvn[i].size(); ++j)
            //std::cout << '(' << vvn.at(i).at(j).target << ", " << vvn.at(i).at(j).weight << ") ";
            std::cout << '(' << vvn[i][j].target << ", " << vvn[i][j].weight << ") ";

        std::cout << '\n';
    }


If your compiler supports C++11's range based for() loops, use them instead.

Edit: slight beautification.
Last edited on
//Error here: cout << a[i].target << endl;


cout << a[i][j].target << endl;
Topic archived. No new replies allowed.