简体   繁体   English

C ++ typedef,map,迭代器

[英]C++ typedef, map, iterator

Can anybody help me with an iterator problem? 有人可以帮我解决迭代器问题吗? I'm having something like this: 我有这样的事情:

class SomeClass{

public:
    //Constructor assigns to m_RefPtr a new t_Records
    //Destructor deletes m_RefPtr or decreases m_RefCnt
    //Copy Constructor assigns m_RefPtr to new obj, increases m_RefCnt
    bool Search(const string &);

private:
    //Some private variables

    struct t_Records{ //For reference counting

        int m_RefCnt; //Reference counter

        typedef vector<int> m_Vec;
        typedef map<string, m_Vec> m_Map;
        m_Map m_RecMap;

        t_Records(void){
            m_RefCnt = 1;
        }

    };

    t_Records * m_RefPtr;

};

//Searchs through the map of m_RefPtr, returns true if found
bool SomeClass::Search(const string & keyword){

    //How to create and use an iterator of m_Map???

    return true;

}

As how I mentioned, I'm having troubles with creating (defining) map iterator outside of the struct. 正如我所提到的,在结构外部创建(定义)映射迭代器时遇到了麻烦。 The map is initalized and contains some records. 该地图已初始化,并包含一些记录。 Thanks for your reply. 感谢您的回复。

Like this: 像这样:

// assuming m_RefPtr is properly initialized:
t_Records::m_Map::iterator it = m_RefPtr->m_RecMap.begin();
++it; // etc.

By the way, m_Map is a bad name for a type. 顺便说一句, m_Map是一个类型的坏名字。 By common convention, names prefixed with m_ are used for data members. 按照惯例,以m_为前缀的名称用于数据成员。

You can iterate like this 你可以这样迭代

for (m_Map::iterator it = m_RecMap.begin(); it != m_RecMap.end(); ++it)
{
    // do stuff with *it
}

Or even easier 甚至更容易

for (auto it = m_RecMap.begin(); it != m_RecMap.end(); ++it)
{
    // do stuff with *it
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM