正在获取QMap::迭代器的位置(int)
Obtaining position (int) of a QMap::iterator
我已经编写了一个基于QListModel的模型来访问存储在QMap中的数据。QMap是有排序保证的。因此,从QMap::迭代器或const_iterator到int的转换应该是可能的并且合理的。
此刻,我递减迭代器并递增一个计数器变量:
QVariantMap::iterator it = m_data.upperBound(name); //or any other iterator
int pos = 0;
for (;it != m_data.begin();it--)
pos++;
//now pos contains the position of the entry at index in the map
对于某些数据结构,似乎定义了int operator-(iterator)
,允许int pos = it - m_data.begin();
,QMap是否也有类似的内容?
我对QMap并不特别熟悉,但通常情况下,这是查找映射中元素秩的最简单算法。
假设Qt迭代器可用于标准算法,则可以将其简化为std::distance(m_data.begin(), it)
。否则,您可以使用所展示的算法为QMap迭代器自己实现distance
。
相关文章:
- 为什么在全局范围内使用"extern int a"似乎不行?
- 将值指定给向量(2D)的向量中的某个位置
- int(c) 和 c-'0' 之间的区别。C++
- 从"int*"强制转换为"unsigned int"会丢失精度错误
- 为什么野牛仍在使用"int yylex(void)",却找不到"int yylex(YYS
- 使用Unreal C++获取VR耳机的世界位置/方向
- 有符号的int和int-有没有一种方法可以在C++中区分它们
- 写入位置0x0000000C时发生访问冲突
- 请解释这句话(cout<<1+int((a<b)^((b-a)&1) )<<endl
- 是否可以从int转换为enum类类型
- 递增 'int i = 0' 以在初始化的字符数组的位置递增
- 只读位置'__result.std::_Rb_tree_const_iterator<_Tp>::operator*<long long int>()'分配错误
- 如何在C 2D向量中找到对象(而不是int)的位置
- 如何从 stl 向量中最小元素的迭代器位置获取 int/size_t 值
- 如何将字符数组转换为不对齐位置的 int
- 字符串和 int 对象或左值/右值的内存位置
- 正在获取QMap::迭代器的位置(int)
- 为什么有些函数的位置很密集,而有些函数则排列整齐,用int - 3指令填充?
- C/ c++: do内置对象,如' int ' ' double '在程序运行期间始终保持在内存中的确切位置
- 写入位置0x0..的C++访问冲突正在设置int*