将项目插入到std :: map时错误

Error while inserting item to std::map

本文关键字:map 错误 std 项目 插入      更新时间:2023-10-16

我试图将新项目插入具有自定义键和值类型的地图:

struct Address {
    bool operator<(const Address& a) const { return 1; }
};
struct Cell {};
std::map<Address, Cell> map;
map.insert(std::make_pair<Address, Cell>(Address(), Cell()));

g 编译恰好编译,但我的IDE(CLION(抱怨插入方法的参数(将其标记为红色下划线的错误(:参数类型不匹配:class'STD :: pair&lt; pair&lt;地址,cell&gt;'与类'std :: prinitizer_list&lt&lt :: map&lt; main :: main :: main :: cell&gt; :: value_types&gt;'

不兼容。

为什么我会遇到此错误?我的代码有问题吗?还是IDE中的错误?

您可以使用std :: vector而不是std :: map width a std :: pair。
解决问题更改:

std::map<Address, Cell> map;

to:

std::vector<std::pair<Address, Cell> > map;

并包括矢量库

#include <vector>