在给定键 (C++) 的地图向量内查找项目



我试图使用映射向量调试编码(C++/Quantlib)。基本上,我想在地图中找到一个项目,而地图又在矢量内。但抓住了错误。

输入:

vector<map <Date, Real> > simulatedPrices_;    // a vector containing 1000 maps
vector<Date> cds_maturities_;

私有变量:

map <Date, Real> pricePathJ;    // for reading each map in the vector
Real w_t_;    //

编码:

for (int j = 0; j < no_of_paths; j++) {
    pricePathJ = simulatedPrices_[j];
    for (int i = 0; i <= iTenor_; i++) {    //iTenor is the number of element inside vector cds_maturities_
        startDate = ......;
        endDate = ......;
        w_t_ = pricePathJ.find(cds_maturities_[i]);    // error in pricePathJ saying there is no conversion function from iterator ... the pair<Date, Real> to Real. 
        ......

我是否犯了任何错误,或者在上述编码中是否忽略了任何指针类型?谢谢。

备注:变量类型真实与双精度类型类似

您可以使用一个简单的for循环并使用map的find()函数来执行此操作。 它将返回匹配的第一个Real

Real FindReal(const std::vector<std::map<Date, Real>> & data, const Date & findDate)
{
    for (auto&& e : data)
    {
        auto it = e.find(findDate);
        if (it != e.end())
            return it->second;
    }
    return some_value_if_not_found;
}

最新更新