我有一个 std::map,它存储一个带有 std::any 向量的键。
目的是存储所有值并为每个键打印它们(使用不同的类型)。没有对容器进行其他操作,只有“插入”和“清理”。
我想澄清一下容器经常被装满(和清空),所以我需要一个高效的容器。
这一切都适用于我的代码。然而,问题是当我打印值时,它们是根据键排序的,但我必须按插入顺序打印(或存储)它们。
我的代码:
#include <iostream>
#include <map>
#include <vector>
#include <any>
std::map<int, std::vector<std::any>> testMap;
void insertMap(int value, std::vector<std::any> tempVector);
void printMap();
int main()
{
std::vector<std::any> tempVector;
tempVector.clear();
tempVector.push_back(1000);
tempVector.push_back((std::string)"hello");
tempVector.push_back(0.10f);
insertMap(10, tempVector);
tempVector.clear();
tempVector.push_back(1500);
tempVector.push_back((std::string)"hello2");
tempVector.push_back(0.20f);
insertMap(5, tempVector);
tempVector.clear();
tempVector.push_back(2000);
tempVector.push_back((std::string)"hello3");
tempVector.push_back(0.5f);
insertMap(7, tempVector);
// etc..
printMap();
}
void insertMap(int value, std::vector<std::any> tempVector)
{
testMap[value].insert(testMap[value].end(), tempVector.begin(), tempVector.end());
}
void printMap()
{
for (const auto& [key, value] : testMap)
{
std::cout << "key=" << key << "\n";
for(auto vec_iter : value)
{
if (vec_iter.type() == typeid(int))
std::cout << "\t" << "int=" << std::any_cast<int>(vec_iter) << "\n";
else if (vec_iter.type() == typeid(float))
std::cout << "\t" << "float=" << std::any_cast<float>(vec_iter) << "\n";
else if (vec_iter.type() == typeid(std::string))
std::cout << "\t" << "string=" << std::any_cast<std::string>(vec_iter) << "\n";
}
}
}
输出:
key=5
key=7
key=10
预期输出:
key=10
key=5
key=7
我尝试使用unordered_map
,但它不按插入顺序打印它们。
那么我可以使用哪个容器?在我的情况下,最好的表现是什么?
我以为我可以使用std::vector< std::map<int, std::vector<std::any>> >
(vector
那家商店std::map
)。但它快吗?有更好的解决方案吗?