c++ - 在不知道 map 的情况下获取 map 的键

标签 c++ c++11

我的 ObjClass * obj有一个 map<std::string, OtherClass*> obj2 ,我想获取所有键,因为我想迭代 obj2 的内容。

我无法先验地知道字符串包含什么,所以我尝试了这种方式,但不起作用。

    typedef std::map<std::string, ObjClass2*>::iterator it_type;    
    for(it_type iterator = obj.begin(); iterator != obj.end(); iterator++)
    {
        std::cout<<iterator->first<<std::endl;

    }

也许我可以使用进一步的整数索引?但是如何呢?

最佳答案

请检查下面的代码,您需要遍历 obj2 元素:

typedef std::map<std::string, ObjClass2*>::iterator it_type;    
for(it_type iterator = obj->obj2.begin(); iterator != obj->obj2.end(); ++iterator)
{
    std::cout<<iterator->first<<std::endl;

}

关于c++ - 在不知道 map 的情况下获取 map 的键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21065980/

相关文章:

c++ - C++ 中的无休止循环,比较两个字符

c++ - C++11 中默认纯虚拟析构函数的正确放置

c++ - C++-使用泰勒级数逼近估算cos(x)

c++ - 这个针对 MSVC 双重检查锁定错误和函数静态的解决方案有什么问题?

c++ - Boost hana 获取第一个匹配项的索引

c++ - 严格的混叠和对齐

c++ - 将成员函数作为标准函数回调传递

c++ - 用 C 语言访问文件以实现 OPC UA 协议(protocol)

c++ - 使用 C++ OOP 设计更新总值

c++ - 在类外部初始化的 constexpr 静态成员的声明中是否需要 constexpr 说明符?