我找到了这个excellent question这与我想要做的稍微相反。
我还是太菜鸟了,无法真正理解发生了什么。 (我现在几乎不熟悉基本语法)
我想做的是通过 struct
的元素之一索引 struct
的 std::map
。
所以,如果我有
struct accountStruct{
string accountName;
double total;
};
我想从 accountStruct
的 std/boost::map
调用 total
和 accounts[accountName] .总计
。
请告诉我怎么做。
最佳答案
您可以使用 std::map<std::string, accountStruct>
.但是,这不会自动填写 accountName
在你的accountStruct
当您将元素插入 map 时。 std::map<K, V>
当然不要考虑使用用户提供的字段 struct
作为 key 。您可能想使用 std::map<std::string, double>
直接,虽然。 ... 或将帐户名保留在 accountStruct
之外:
struct accountStruct {
double total = 0.0;
// possibly other members
};
std::map<std::string, accountStruct> m;
关于c++ - 按结构元素索引 std/boost::map,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18818754/