将map<pair<int,int>,vector<Object> >添加到BST中,其中对是关键

Adding map<pair<int,int>,vector<Object> > into BST where pair is key

本文关键字:gt lt int BST vector pair map Object 添加      更新时间:2023-10-16

所以我有一张地图,其中键是月份和年份对,返回的值是矢量,它基本上是该月/年的天气实例向量。

注意:BST是由我制作的,而不是STL BST。

for (map<pair<int, int>, vector<Weather> >::iterator it = monthMap.begin(); it != monthMap.end(); ++it)
{
month = it->first.first;
year = it->first.second;
monthVec = it->second;
monthBST.Insert(map<make_pair(month,year),monthVec>);
}

但是,它返回了一堆错误,所有这些错误都与我如何在常量表达式中没有任何错误有关(make_pair,月,年,函数调用,月Vec)

main.cpp|170|error: 'std::make_pair(_T1, _T2)' cannot appear in a constant-expression|
main.cpp|170|error: 'month' cannot appear in a constant-expression|
main.cpp|170|error: 'year' cannot appear in a constant-expression|
main.cpp|170|error: a function call cannot appear in a constant-expression|
main.cpp|170|error: 'monthVec' cannot appear in a constant-expression|
main.cpp|170|error: template argument 1 is invalid|
main.cpp|170|error: template argument 2 is invalid|
main.cpp|170|error: template argument 3 is invalid|
main.cpp|170|error: template argument 4 is invalid|

我尝试了一堆东西,似乎想不通。

最终目标是拥有一个BST,它将自我平衡(因为数据已排序),并且找到键,它返回与其关联的monthVec。

根据您的代码,您似乎想要这样的东西:

typedef std::map<std::pair<int, int>, std::vector<Weather>> WeatherMap; 
// for (const auto& itr : monthMap) // This is a bit cleaner
for (auto itr = monthMap.begin(); itr != monthMap.end(); ++it)
{
WeatherMap newMap;
newMap.insert(*itr);
monthBST.Insert(std::move(newMap));
}

鉴于您的解释,这没有多大意义,因此您可能实际上想要这样的东西:

typedef std::map<std::pair<int, int>, std::vector<Weather>> WeatherMap; 
for (auto itr = monthMap.begin(); itr != monthMap.end(); ++it)
{
monthBST.Insert(*itr);
}

当您需要多个值作为键时,最好使用多映射。