C++ How to loop through a list of structs and access their properties

后端 未结 2 1755
灰色年华
灰色年华 2021-01-14 03:58

I know I can loop through a list of strings like this:

list::iterator Iterator;
 for(Iterator = AllData.begin(); 
   Iterator != AllData.end();         


        
相关标签:
2条回答
  • 2021-01-14 04:28

    (*Iterator).property1 or Iterator->property1

    0 讨论(0)
  • 2021-01-14 04:36

    It's as easy as Iterator->property. Your first attempt is almost correct, it just needs some parentheses due to operator precedence: (*Iterator).property

    In order to use for_each, you would have to lift the cout statments into a function or functor like so:

    void printData(AllDataType &data)
    {
        cout << "\t" + data.property1 + "\n";
        cout << "\t" + data.property2 + "\n";
    }
    
    for_each(AllData.begin(), AllData.end(), printData);
    
    0 讨论(0)
提交回复
热议问题