Question

I am trying to insert into an unordered_map with a std::set value declared as such:

class Database {
...
private:
    struct CountryRCID {
        int RCID;
        int Vote;
    };
    struct comp {
        bool operator() (const CountryRCID& left, const CountryRCID& right) const {
            return left.RCID < right.RCID;
        }
    };
    std::unordered_map<const char*, std::set<CountryRCID, comp> > CNTVotes;
};

In the Database constructor I am reading data in from a file and attempting to insert into the unordered_map

Database() {
    char CNT[3];
    CountryRCID RCIDVote;
    ... Insert data into CNT and RCIDVote ...
    CNTVotes.insert(std::make_pair(CNT, RCIDVote));
}

And I have attempted compiling the code with both:

g++ main.cpp -std=gnu++0x

and

g++ main.cpp -std=c++0x

But am receiving the error:

In file included from /usr/include/c++/4.4/bits/stl_algobase.h:66,
                 from /usr/include/c++/4.4/bits/char_traits.h:41,
                 from /usr/include/c++/4.4/ios:41,
                 from /usr/include/c++/4.4/istream:40,
                 from /usr/include/c++/4.4/fstream:40,
                 from db.h:1,
                 from main.cpp:1:
/usr/include/c++/4.4/bits/stl_pair.h: In constructor ‘std::pair<_T1, _T2>::pair(std::pair<_U1, _U2>&&) [with _U1 = char*, _U2 = Database::CountryRCID, _T1 = const char* const, _T2 = std::set<Database::CountryRCID, Database::comp, std::allocator<Database::CountryRCID> >]’:
db.h:50:   instantiated from here
/usr/include/c++/4.4/bits/stl_pair.h:107: error: no matching function for call to ‘std::set<Database::CountryRCID, Database::comp, std::allocator<Database::CountryRCID> >::set(Database::CountryRCID)’
/usr/include/c++/4.4/bits/stl_set.h:212: note: candidates are: std::set<_Key, _Compare, _Alloc>::set(std::initializer_list<_CharT>, const _Compare&, const _Alloc&) [with _Key = Database::CountryRCID, _Compare = Database::comp, _Alloc = std::allocator<Database::CountryRCID>]
/usr/include/c++/4.4/bits/stl_set.h:199: note:                 std::set<_Key, _Compare, _Alloc>::set(std::set<_Key, _Compare, _Alloc>&&) [with _Key = Database::CountryRCID, _Compare = Database::comp, _Alloc = std::allocator<Database::CountryRCID>]
/usr/include/c++/4.4/bits/stl_set.h:188: note:                 std::set<_Key, _Compare, _Alloc>::set(const std::set<_Key, _Compare, _Alloc>&) [with _Key = Database::CountryRCID, _Compare = Database::comp, _Alloc = std::allocator<Database::CountryRCID>]
/usr/include/c++/4.4/bits/stl_set.h:145: note:                 std::set<_Key, _Compare, _Alloc>::set(const _Compare&, const _Alloc&) [with _Key = Database::CountryRCID, _Compare = Database::comp, _Alloc = std::allocator<Database::CountryRCID>]
/usr/include/c++/4.4/bits/stl_set.h:136: note:                 std::set<_Key, _Compare, _Alloc>::set() [with _Key = Database::CountryRCID, _Compare = Database::comp, _Alloc = std::allocator<Database::CountryRCID>]

I have also tried different insert methods:

CNTVotes[CNT] = RCIDVote;

and

std::pair <const char*, CountryRCID> test (CNT, RCIDVote);
CNTVotes.insert(test);

which just results in similar errors

I would very much appreciate it if anyone could help me understand why it is not working and what I could do to make this work.

Thank you.

Was it helpful?

Solution

CountryRCID is a type struct, it is not a std::set ... you would first need to construct a std::set of type std::set<CountryRCID> and use that in your arguments to makePair() in order to match the template arguments for your unordered_map.

I also think the struct comp functor is redundant ... just define operator<() and operator==() for your CountryRCID struct.

If you do that, and change the declaration of CNTVotes to (BTW, note that I'm borrowing Neil's suggestion to use std::string rather than a const char* as the key-value ... I think that's a very good idea)

std::unordered_map<std::string, std::set<CountryRCID> > CNTVotes;

then you can do the following:

CountryRCID RCIDVote;
//... Insert data into CNT and RCIDVote ...
std::string CNTString(CNT);
std::set<CountryRCID> RCIDVoteSet;  // <== make a std::set of type set<CountryRCID>
RCIDVoteSet.insert(RCIDVote);
CNTVotes.insert(std::make_pair(CNTString, RCIDVoteSet));

OTHER TIPS

Possibly nothing directly to do with your problem, but maps of char * never really work. Instead of :

 std::unordered_map<const char*, std::set<CountryRCID, comp> > CNTVotes;

you probably want:

 std::unordered_map<std::string, std::set<CountryRCID, comp> > CNTVotes;

There is no constructor to make a set from a single object of the type that goes into that set. You'll need to create a temporary set and add the item to it, then use that to add to the unordered_map.

EDIT: Actually since you have an actual object I think you can get away with the iterator, iterator constructor like this:

CNTVotes.insert(std::make_pair(CNT, std::set(&RCIDVote, &RCIDVote + 1)));

As Jason mentioned, your value in the map is a set rather than a single CountryRCID struct. If you mean to add the CountryRCID to the set in the map you probably want something like:

typedef std::set<CountryRCID, comp> MySet;
typedef std::unordered_map<const char*, MySet> MyMap;

MyMap::_Pairib ret = CNTVotes.insert(std::make_pair(CNT, MySet()));
ret.first->second.insert(RCIDVote);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top