我是 C++ 新手,我正在研究std::unordered_map
和std::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;
}