Iterate over vector

Hi Guys,
Can anyone let me know how to iterate over the data from the

class MainProduct //This contains the main product which can be bought with //addon products.
{
public :
int productID;
char productName[50];
vector<Addon> ListAddon;
};

class Addon //These are the addon products (1-many relationship with Main //product
{
public:
int addonID;
char addonName[50];
};
class ListProduct //Create list of all the products which are purchased along //with addons
{
public:
vector<MainProduct> ListMainProduct;
};

int _tmain(int argc, _TCHAR* argv[])
{

//Iterator1
MainProduct MainProd1;
MainProd1.productID=1;
strcpy_s(MainProd1.productName,"MyProduct1");

Addon Addon1;
Addon1.addonID=1;
strcpy_s(Addon1.addonName,"MyAddonProduct1");

Addon Addon2;
Addon2.addonID=2;
strcpy_s(Addon2.addonName,"MyAddonProduct2");

Addon Addon3;
Addon3.addonID=3;
strcpy_s(Addon3.addonName,"MyAddonProduct3");


MainProd1.ListAddon.push_back(Addon1);
MainProd1.ListAddon.push_back(Addon2);
MainProd1.ListAddon.push_back(Addon3);

ListProduct ListAllProd;
ListAllProd.ListMainProduct.push_back(MainProd1);
//Iterator1 End


//Iterator2
MainProduct MainProd2;
MainProd1.productID=2;
strcpy_s(MainProd2.productName,"MyMainProduct2");

Addon Addon4;
Addon4.addonID=4;
strcpy(Addon1.addonName,"MyAddonProduct4");

Addon Addon5;
Addon5.addonID=5;
strcpy(Addon5.addonName,"MyAddonproduct5");

Addon Addon6;
Addon6.addonID=6;
strcpy(Addon6.addonName,"MyAddonProduct6");


MainProd2.ListAddon.push_back(Addon1);
MainProd2.ListAddon.push_back(Addon2);
MainProd2.ListAddon.push_back(Addon3);

ListAllProd.ListMainProduct.push_back(MainProd2);
//Iterator2 End



return 0;

}

This code is working perfectly but i want to iterate over these Main product and Addons to display in console. I can see the desired result while debugging.

Would appreciate your help.
I tried this and it worked.

ListAllProd.ListMainProduct.size();


ListAllProd.ListMainProduct[1].ListAddon.size();


for(int i = 0 ; i<ListAllProd.ListMainProduct.size();++i)
{
//We get information about Main Product.
std::cout<<ListAllProd.ListMainProduct[i].productID;
for(int j=0 ; j<ListAllProd.ListMainProduct[i].ListAddon.size();++j)
{
std::cout<<ListAllProd.ListMainProduct[i].ListAddon[j].addonID;
std::cout<<ListAllProd.ListMainProduct[i].ListAddon[j].addonName;
//We get information about Addon.


}
std::cout<<endl;
}
Your method works, but I've often heard people say it's not as safe as using an iterator.

This is how you would do it with iterators

1
2
3
4
5
6
7
8
9
10
11
12
13
vector<MainProduct>::iterator it1; // Making an iterator called it1
vector<Addon>::iterator it2;

for (it1 = ListAllProd.ListMainProduct.begin(); it1 < ListAllProd.ListMainProduct.end(); ++it1)
{// it1 cycles through all of the ListMainProducts in ListAllProd
    cout << it1->productID; // We access elements of ListAllProd.ListMainProducts with ->
    for(it2 = it1->ListAddon.begin(); it2 < it1->ListAddon.end(); ++it2)
    { // it2 will now iterate through all of the ListAddon objects.
        cout << it2->addonID;
        cout << it2->addonName;
    }
    cout << endl;
}
Last edited on
wow man! thats cool idea. Thanks a ton for this suggestion. I was also not too sure to use my method as i am new to c++.
Thanks again!!
Topic archived. No new replies allowed.