Monday, 15 September 2014

c++ - Iterating over a map of ints and objects -



c++ - Iterating over a map of ints and objects -

i have class called sar contains map int key values , object info values of sardatapoint type. sardatapoint type has function int getdate() phone call each object contained in map. i'm having problem wrapping head around how accomplish this. think i'm close.

class sar { public: // other attributes removed compactness std::map<int, sardatapoint> data; }; // end of class sar void sar::printsar() { for(auto iter = this->data.cbegin(); iter != this->data.cend(); ++iter) { std::cout << iter->getdate() << '\n'; //incorrect close? } }

any ideas on how implement this?

you have std::map, collection of std::pairs. each std::pair has key (pair::first) , value (pair::second). believe iteration loop correct. phone call ::getdate() on std::pair wrong. need following:

std::cout << iter->second.getdate() << '\n';

another alternative iterate through entire map utilize range-based for

for(auto const& d : data) { std::cout << d.second.getdate() << '\n'; }

c++ map iterator

No comments:

Post a Comment