Search code examples
c++c++11stliteratorauto

How can I loop through all the elements in a two pair STL set<pair<t1,t2>,pair<t1,t2>>?


Here is the data type I am using.

  set< std::pair<string,string>,std::pair<string,string>> foo;

Here is my failed attempt at looping over it

for(auto &e: foo){
    cout << e.first << " " << e.second // this is where I am having an issue.
}

Is it possible to use auto this way? e.g

e.first, e.second // some c++ magic (i realize -> is wrong)  In pseudo -> //  e.third ...

I would love to use auto, but if not how would I write an iterator for the data type I am using?


Solution

  • You are doing something utterly weird.

    The signature of set is:

    template<
        class Key,
        class Compare = std::less<Key>,
        class Allocator = std::allocator<Key>
    > class set;
    

    So you are using std::pair<std::string, std::string> as comparator. It will fail to compile as soon as you try to insert something.

    But I am pretty sure that's not what you want.

    You probably either want

    map<pair<string, string>, pair<string, string>>;
    

    or

    set<pair<pair<string, string>, pair<string<string>>>;
    

    or maybe

    set<tuple<string, string, string, string>>;