unordered_map插入错误C++
unordered_map insert error C++
我正在尝试插入一个声明为 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);
这只会导致类似的错误
如果有人能帮助我理解为什么它不起作用以及我可以做些什么来使这项工作,我将不胜感激。
谢谢。
可能与您的问题没有直接关系,但 char * 的地图从未真正起作用。而不是:
std::unordered_map<const char*, std::set<CountryRCID, comp> > CNTVotes;
您可能希望:
std::unordered_map<std::string, std::set<CountryRCID, comp> > CNTVotes;
>CountryRCID
是struct
型,它不是std::set
...您首先需要构造一个类型 std::set<CountryRCID>
的std::set
,并在参数中使用它来makePair()
以匹配unordered_map
的模板参数。
我也认为struct comp
函子是多余的......只需为您的CountryRCID
结构定义operator<()
和operator==()
即可。
如果您这样做,并将CNTVotes
声明更改为(顺便说一句,请注意,我借用了 Neil 的建议,即使用 std::string
而不是const 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));
没有构造函数可以从进入该集合的单个对象创建集合。您需要创建一个临时集并将项目添加到其中,然后使用它添加到unordered_map
。
编辑:实际上,由于您有一个实际的对象,我认为您可以像这样摆脱iterator, iterator
构造函数:
CNTVotes.insert(std::make_pair(CNT, std::set(&RCIDVote, &RCIDVote + 1)));
正如 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);
- 警告处理为错误这里有什么问题
- "error: no matching function for call to"构造函数错误
- boost::进程间消息队列引发错误
- C++,OpenCV,尝试显示图像时"OpenCV(4.3.0) Error: Assertion failed (size.width>0 && size.height>0)"此错误
- 有关插入适配器的错误。[错误]请求从 'back_insert_iterator<vector<>>' 类型转换为非标量类型
- QT在错误的班级中寻找空位
- vector.resize()中的分配错误
- 代码在main()中运行,但在函数中出现错误
- 释放错误后堆使用
- (C++)分析树以计算返回错误值的简单算术表达式
- Project Euler问题4的错误解决方案
- 我的字符计数代码计算错误.为什么
- 从"int*"强制转换为"unsigned int"会丢失精度错误
- 尝试导入pybind-opencv模块时出现libgtk错误
- CMake项目Boost库错误:Boost/config/compiler/gcc.hpp:165:10:致命错误:cs
- 在某些循环内使用vector.push_back时出现分段错误
- MSVC多行宏编译器错误
- 静态数据成员的问题-修复链接错误会导致编译器错误
- 为什么在运行时没有向我们提供有关分段错误的更多信息?
- 错误:未在此范围内声明'reverse'