【问题标题】:unordered_map with std::any values cannot any_cast string带有 std::any 值的 unordered_map 不能使用 any_cast 字符串
【发布时间】:2019-06-14 00:50:15
【问题描述】:

我是 C++ 新手,我正在研究 std::unordered_mapstd::any。 我创建了一个示例演示,它会生成一些虚拟数据,然后将它们插入到地图中。

之后(在注释掉的代码中)我使用 any_cast 成功打印出值。

但是,在底部,您可能会看到我尝试以 0 成功检索特定密钥。我得到的错误是Bad any_cast,并且转换与我用于打印的代码完全相同(目前已被注释掉)。

如果这是一个愚蠢的错误,我很抱歉,但我是新手。提前谢谢你。

#include <iostream>
#include <string>
#include <any>
#include <map>
#include <unordered_map>
#include <time.h>
#include <Windows.h>

std::unordered_map<std::string, std::any> map = {};

int main() {

    unsigned long started = clock();
    const std::string prefix = "key";

    for (int i = 0; i < 1000000; i++) {

        const std::string key = "key" + std::to_string(i);

        map.insert_or_assign(key, i);

    }

    std::cout << "Data inserted after: " << (clock() - started) << "ms" << std::endl;

    system("pause");

    started = clock();

    /*
    for (const auto& item : map) {


        try {

            //std::cout << item.second.type().name() << std::endl;
            if (item.second.type() == typeid(int)) {
                std::cout << std::any_cast<const int>(item.second) << std::endl;
            }
            else if (item.second.type() == typeid(float)) {
                std::cout << std::any_cast<const float>(item.second) << std::endl;
            }
            else if (item.second.type() == typeid(double)) {
                std::cout << std::any_cast<const double>(item.second) << std::endl;
            }
            else if (item.second.type() == typeid(long)) {
                std::cout << std::any_cast<const long>(item.second) << std::endl;
            }
            else if (item.second.type() == typeid(char const *)) {
                std::cout << std::any_cast<char const *>(item.second) << std::endl;
            }
            else if (item.second.type() == typeid(std::string)) {
                std::cout << std::any_cast<const std::string>(item.second) << std::endl;
            }
            else {
                std::cout << item.first << " has an unhandled value type of " << item.second.type().name() << std::endl;
            }

        }
        catch (const std::bad_any_cast& err) {
            std::cerr << err.what() << std::endl;
        }



    }

    std::cout << "Map iterated after: " << (clock() - started) << "ms" << std::endl;

    */

    try {

        auto value = std::any_cast<char const *>(map["key8745"]);

        std::cout << "Key " << value << " retrieved after: " << (clock() - started) << "ms" << std::endl;

    }
    catch (const std::bad_any_cast &err) {
        std::cerr << err.what() << std::endl;
    }

    system("pause");

    return 0;

}

【问题讨论】:

    标签: c++ c++17 stdany


    【解决方案1】:

    您插入的所有值都是ints,但您试图将底部的单个值转换为char const*;它不是任何类型的指针,而且看起来any_cast 的类型安全性足以拒绝该转换尝试(这是一件好事,因为取消引用指向内存地址8745 的指针可能会出现段错误)。您在早期的代码中输入检查,并且可能在那里选择了正确的演员表。

    【讨论】:

    • 天哪,我需要一双新的眼睛...谢谢您稍后查看并接受您的回答!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-17
    • 2016-08-18
    • 2021-04-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多