C++里map嵌套vector时报错

C++里map嵌套vector时报错,第1张

如zjfaok所说,map的参数是有条件的,平时的一些常用类型能够直接用<运算符比较,但是现在key是自定义类Good,所以你需要在类Good中重载<运算符,相当于告诉map你可以用我的这个比较方法来对键Good进行比较,在类Good中添加如下代码即可:

bool operator<(const Good& other) const

{

      return this->val<other.val

}

#include <map>

#include <vector>

#include <string>

void f(std::vector<std::map<std::string, int> >& ss)

{

      std::map<std::string, int> s

      s.insert (std::make_pair("12", 3))

      s.insert (std::make_pair("23", 4))

      ss.push_back(s)

}


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/bake/11558481.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-17
下一篇 2023-05-17

发表评论

登录后才能评论

评论列表(0条)

保存