索引标准/提升::按结构元素映射

index std/boost::map by struct element

本文关键字:结构 元素 映射 索引 提升 标准      更新时间:2023-10-16

我发现这个很好的问题与我想做的事情略有相反。

我仍然太菜鸟,无法真正理解发生了什么。 (我现在几乎不熟悉基本语法)

我想做的是用struct的一个元素索引structstd::map

所以,如果我有

struct accountStruct{
     string accountName;
     double total;
};

我想用accounts[accountName].totalaccountStruct std/boost::maptotal.

请告诉我怎么做。

您可以使用std::map<std::string, accountStruct> .但是,当您将元素插入地图时,这不会自动填充accountStruct中的accountNamestd::map<K, V>当然不会考虑使用用户提供的字段struct作为键。不过,您可能希望直接使用std::map<std::string, double>。...或将帐户名称排除在accountStruct

struct accountStruct {
    double total = 0.0;
    // possibly other members
};
std::map<std::string, accountStruct> m;