C++ typedef, map, iterator
C++ typedef, map, iterator
有人能帮我解决迭代器问题吗?我有这样的东西:
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;
}
正如我所提到的,我在结构之外创建(定义)映射迭代器时遇到了麻烦。映射已初始化并包含一些记录。谢谢你的回复。
像这样:
// assuming m_RefPtr is properly initialized:
t_Records::m_Map::iterator it = m_RefPtr->m_RecMap.begin();
++it; // etc.
顺便说一下,m_Map
对于一个类型来说是个坏名字。按照惯例,以m_
为前缀的名称用于数据成员。
您可以像这样迭代
for (m_Map::iterator it = m_RecMap.begin(); it != m_RecMap.end(); ++it)
{
// do stuff with *it
}
或者更简单的
for (auto it = m_RecMap.begin(); it != m_RecMap.end(); ++it)
{
// do stuff with *it
}
相关文章:
- 如何导出包含具有"std::unique_ptr"值的"std::map"属性的
- std::map<struct,struct>::find 找不到匹配项,但是如果我循环通过 begin() 到 end(),我在那里看到匹配项
- 使用一个考虑到std::map中键值的滚动或换行的键
- 为什么 const std::p air<K,V>& 在 std::map 上基于范围的 for 循环不起作用?
- 允许从 std::map 的密钥窃取资源?
- 有没有办法对std::unordered_set、std::unrdered_map、std::set、std::map
- 将重物插入std::map
- 使用通用值初始化 std::map,不重复
- 仅包含可移动 std::map 的类的移动构造函数不起作用
- C++:当所有条目都保证是唯一时,替代 std::map
- 使用模板化的键类型定义 std::map,该键类型基于作为参数接收的函数
- 错误:与'operator='不匹配(操作数类型为"std::map<int、double>::iterator
- 自动,错误:MAP ITERATOR没有名为“ First”的成员
- std::map<K,V>::iterator 是否实例化 std::map<K,V>?
- 关于从第二个存储值_ty2秒复制std :: Map Iterator的数据的问题
- c++ std::map::iterator一无所获,但下标操作符返回对映射值的引用
- Std::map::iterator对单个元素进行自减
- map<int、int>::iterator 的编译错误,运算符 = 不匹配
- C++ typedef, map, iterator
- 将map<>::iterator作为map<>::const_iterator传递