使用 c++ 在映射中插入对作为键
Insert Pair as a key in map using c++
我想知道如何使用 c++ 在 map 中插入对,这是我的代码:
map< pair<int, string>, int> timeline;
我尝试使用以下方法插入它:
timeline.insert(pair<pair<int, string> , int>(make_pair(12, "str"), 33);
//and
timeline.insert(make_pair(12, "str"), 33);
但我有错误
main.cpp|66|error: no matching function for call to 'std::map<std::pair<int, std::basic_string<char> >, int&>::insert(std::pair<int, const char*>, int)'|
std::map::insert
期望std::map::value_type
作为其参数,即 std::pair<const std::pair<int, string>, int>
.例如
timeline.insert(make_pair(make_pair(12, "str"), 33));
或更简单
timeline.insert({{12, "str"}, 33});
如果你想就地构造元素,你也可以使用std::map::emplace
,例如
timeline.emplace(make_pair(12, "str"), 33);
住
如有疑问,请简化。
auto key = std::make_pair(12, "str");
auto value = 33;
timeline.insert(std::make_pair(key, value));
只需使用传统方式:
timeline[key] = value;
对于配对的输入和重新竞争:
timeline[{1,"stackOverFlow"}] = 69;
for(auto i: timeline)
{
cout<< i.first.first;
cout<< i.first.second;
cout<< i.second;
}
相关文章:
- 使用C++库在Android项目中修改gradle中的cmake参数,用于插入指令的测试
- 有关插入适配器的错误。[错误]请求从 'back_insert_iterator<vector<>>' 类型转换为非标量类型
- 预处理器:插入结构名称中的前一个行号
- 在未初始化映射的情况下,将值插入到映射的映射中
- 如何在c++中只将键插入到bimap的一侧
- 如何将结构插入到集合中并打印集合的成员
- C++json插入数组
- Visual Studio 2019:插入多个C++风格的单行注释
- nlohmann-json将一个数组插入到另一个数组中
- 有效地使用std::unordered_map来插入或增加键的值
- 为字符串中每 N 个字符插入空格的函数没有按照我认为的方式工作?
- 正在插入动态数组
- 插入或删除时获取usb的dos_name
- 叮叮当当在修复时插入多个"覆盖"说明符
- 链表c++插入,所有情况都已检查,但没有任何工作
- 将重物插入std::map
- C++17 - 使用自定义分配器的节点提取/重新插入 - 适用于 clang++/libc++,但不适用于 libstd
- 在数字之间插入 + 或 - 符号以使其等于整数
- 在字符串中插入空格
- 重载运算符的范围是什么?它是否会影响作为类成员的集合的插入函数?