0

我正在尝试使用声明为 std::set 值的 unordered_map 插入:

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;
};

在数据库构造函数中,我正在从文件中读取数据并尝试插入 unordered_map

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

我已经尝试用这两种方法编译代码:

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

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

但我收到错误:

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>]

我也尝试过不同的插入方法:

CNTVotes[CNT] = RCIDVote;

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

这只会导致类似的错误

如果有人能帮助我理解它为什么不工作以及我能做些什么来完成这项工作,我将非常感激。

谢谢你。

4

4 回答 4

4

可能与您的问题没有直接关系,但 char * 的映射从未真正起作用。代替 :

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

你可能想要:

 std::unordered_map<std::string, std::set<CountryRCID, comp> > CNTVotes;
于 2011-05-17T18:49:50.870 回答
2

CountryRCID是一个类型struct,它不是一个std::set......你首先需要构造一个std::set类型std::set<CountryRCID>并在你的参数中使用它makePair()来匹配你的模板参数unordered_map

我也认为struct comp函子是多余的......只需为你的结构定义operator<()和。operator==()CountryRCID

如果你这样做,并将声明更改CNTVotes为(顺便说一句,请注意我借用了 Neil 的建议,使用std::string而不是 aconst char*作为键值......我认为这是一个非常好的主意)

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

那么您可以执行以下操作:

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));
于 2011-05-17T18:50:46.307 回答
1

没有构造函数可以从进入该集合的类型的单个对象创建集合。您需要创建一个临时集并将项目添加到其中,然后使用它添加到unordered_map.

编辑:实际上,由于您有一个实际的对象,我认为您可以iterator, iterator像这样使用构造函数:

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

于 2011-05-17T18:49:14.683 回答
0

正如 Jason 所提到的,您在地图中的值是一个集合而不是单个CountryRCID结构。如果您打算将 添加CountryRCID到地图中的集合中,您可能需要以下内容:

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);
于 2011-05-17T18:57:59.627 回答