1

Here is the map with a string key and a structure value 1. Firstly I am creating a map of integer and a structure as value std::map<int,struct value>; and then I am adding all these map objects to a set std::set<std::map<int,struct value>> and I would like to understand how to loop through this set I am not able to access the maps that are the part of this set, please suggest

struct values
{
    std::string a;
    std::string b;
    values():a("milepost"),b("dummyval"){};
    values( std::string ab, std::string bc)
    {
        a=ab;
        b=bc;
    };

    bool operator<(const values& other) const {
        return (a< other.a && b < other.b) ;
    }
    friend std::ostream& operator<<(std::ostream& os, const values& val);

};

std::ostream& operator<< (std::ostream& os , const values& val)
{
  os << val.a <<"\t"<< val.b;
  return os;

}

typedef std::map<std::string,values>  myWsData;

main() 
{
    values a;

    myWsData et_Data1,pt_Data2;

    et_Data2.insert(std::make_pair("780256", a));

    pt_Data2.insert(std::make_pair("780256", a));

    std::set<myWsData> myet_pt_data;

    myet_pt_data.insert(et_Data1);

    myet_pt_data.insert(pt_Data2);

    for (auto &i:myet_pt_data)
    {
        std::cout<<i<<"\n";
    }
}
Jonas
  • 6,915
  • 8
  • 35
  • 53
LearningCpp
  • 972
  • 12
  • 29
  • `Iam not able to access the maps that are the part of this set`, please detail....what's your problem: compilation? runtime? What are you experiencing that makes you tell it does not work? – jpo38 Mar 27 '17 at 08:34
  • You're going to have a problem with a less-than comparison of two maps aren't you? What are you really trying to solve? – Richard Hodges Mar 27 '17 at 08:36
  • 1
    @RichardHodges I think (as he's still a learner) we should add that the *less-than comparison* is need because `std:set` is implemented as a *sorted* set. Therefore we need to define a order of its elements which this code lacks. – Christoph Diegelmann Mar 27 '17 at 08:46
  • @Christoph : Does that mean the template valuues in the std::set need to have a comparision operator unless its a pod – LearningCpp Mar 27 '17 at 08:57
  • @LearningCpp The full template of `std::set` is `template< class Key, class Compare = std::less, class Allocator = std::allocator > class set;`. So either `std::less` is valid for your type (e.g. by default or because you've defined it) or you have to override `Compare` with a comparator that defines the order of your elements. – Christoph Diegelmann Mar 27 '17 at 09:07

3 Answers3

2

You have to use two loops, like so:

for (auto const& it1 : myet_pt_data)
{
    for (auto const& it2 : it1)
    {
       std::cout << it2.first << '\t' << it2.second << std::endl;
    }
}

When you use auto const& in your range-based for loops you avoid copying the set and all its content. The types are as follows:

  • decltype(it1) is std::map<std::string,values> const&, and
  • decltype(it2) is std::pair<std::string const,values> const&

For completeness, note that the std::string in it2 (the std::pair) is const.

Jonas
  • 6,915
  • 8
  • 35
  • 53
1

You are probably missing a inner loop:

// iterates through all elements of the set:
for (const auto& the_map : myet_pt_data)
{
    // the_map takes all values from the set
    // the_map actual type is const std::map<std::string,values>&
    for (const auto& the_value : the_map)
    {
       // the_value takes all value of the current map (the_map)
       // the_value actual type is const std::pair<std::string,values>&
       std::cout << the_value.first << " value is " << the_value.second << std::endl;
    }
}
jpo38
  • 20,821
  • 10
  • 70
  • 151
1

How about this:

#include <iostream>
#include <map>
#include <set>
#include <algorithm>

using namespace std;

int main(int argc, char *argv[])
{
    set<map<int,string> > list;
    //fill list
    std::for_each(list.begin(), list.end(), [](auto set_el){
        std::for_each(set_el.begin(),set_el.end(),[](auto map_el) {
            std::cout<<map_el.first<<"\t"<<map_el.second<<std::endl;
        });
    });
    cout << "Hello World!" << endl;
    return 0;
}
The Quantum Physicist
  • 24,987
  • 19
  • 103
  • 189