【发布时间】:2017-03-24 00:40:01
【问题描述】:
我有一个无序映射,每个键包含一个类实例。每个实例都包含一个名为 source 的私有变量和一个名为 getSource() 的 getter 函数。
我的目标是遍历地图,使用我的 getter 函数从每个类实例中打印变量。在输出格式方面,我想每行打印一个变量。完成此操作的正确打印语句是什么?
unordered_map 声明:
unordered_map<int, NodeClass> myMap; // Map that holds the topology from the input file
unordered_map<int, NodeClass>::iterator mapIterator; // Iterator for traversing topology map
unordered_map 遍历循环:
// Traverse map
for (mapIterator = myMap.begin(); mapIterator != myMap.end(); mapIterator++) {
// Print "source" class variable at current key value
}
getSource():
// getSource(): Source getter
double NodeClass::getSource() {
return this->source;
}
【问题讨论】:
-
能否请您创建一个minimal reproducible example 并尽可能具体地说明您的问题?
-
我现在就这么做。谢谢!抱歉,这是我在这个网站上的第一篇文章。
-
@key199 如果这是您的第一篇文章,您应该至少选择tour,然后继续阅读How to Ask。这些页面应该已经为您提供了这些指南。所以,这是我第一次不是借口。
-
感谢您分享游览的链接以及如何提问。我将立即阅读它们。
标签: c++ c++11 unordered-map