gpt4 book ai didi

c++ - std::map 麻烦

转载 作者:行者123 更新时间:2023-12-02 10:23:32 28 4
gpt4 key购买 nike

我正在尝试使用std:map类型的键和值制作std::any
Visual Studio 2017

std::map<std::any, std::any> m("lastname", "Ivanov");
std::cout << std::any_cast<std::string>(m["lastname"]) << std::endl;

给我一个

error: binary '<' : no operator found which takes a left-hand operand of type 'const_Ty'

最佳答案

std::any没有二进制“<”运算符(小于)。如何“索引” std::map元素的默认方法。

解决方案可能包括:

  • 使用另一个键(具有运算符“<”)
  • 使用自定义比较器,例如:
    #include <map>
    #include <any>

    int main() {
    auto elements = std::initializer_list<std::pair<const std::any, std::any>>{};
    auto mymap = std::map(elements, [](const std::any& lhs, const std::any& rhs){return false;});
    }

  • 然后实现比较功能,而不是返回 false

    try it yourself

    关于c++ - std::map <std::any,std::any>麻烦,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58058719/

    28 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com