qt 一定要指定大小吗 数组,Qt容器的大小:QMap是否比Qlist大得多?

I am developing a software which maps information in 3D space. I use a container to hold this information. The container which I use is

QList< QList< QMap > > > lidardata;

which basically is a 2D grid representing a rectangular area where each cell is 1 meter x 1 meter, and in each cell the QMap contains a key value representing height and a list of four related values at that height. This way I can store five values (height + other values). I insert values in a loop like this (rown and coln are row and column indexes respectively)

QList NEWLIST;

NEWLIST << (width*100.0) << (1000.0+sens*100.0) << (quint16)(intensity*1000.0) ;

lidardata[ rown ][ coln ].insert( heightValue, NEWLIST);

Before this approach instead of using QMap I used QList and just appending the 5 values.

Now the question is: running my program runs out of memory quite fast. It took up about 800Mb of memory to complete with the first solution (QList instead of QMap), now it runs out (at about 1.4 Gb) at 75% of the total data-storing process.

Can someone confirm that storing the same amount of information using QMap instead of QList does require a lot more space in memory?

Does anyone have any hints to limit space? I will go back to the old solution if nothing comes up.

解决方案

As mentionned in comment:

your code may suffer from Primitive Obsession.

Try to solve your problem using the ValueObject fix stated in this tutorial : create a class with all needed attibutes, and work on instances of this class instead of maintaining nested Qlists and QMaps.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Qt ,可以使用 `QMap` 类来实现一个键值对的映射。要按值对 `QMap` 的键值对进行排序,可以将 `QMap` 的键值对存储到一个 `QList` ,然后使用 `qSort` 函数对 `QList` 的元素进行排序。 以下是一个示例代码: ```cpp #include <QMap> #include <QList> #include <QDebug> int main() { // 创建一个 QMap 对象 QMap<QString, int> myMap; myMap.insert("a", 10); myMap.insert("b", 5); myMap.insert("c", 20); myMap.insert("d", 15); // 创建一个 QList 对象,并将 QMap 的元素存储到 QList QList<QPair<QString, int>> myList; for (auto iter = myMap.begin(); iter != myMap.end(); ++iter) { myList.append(qMakePair(iter.key(), iter.value())); } // 使用 qSort 函数对 QList 的元素进行排序 qSort(myList.begin(), myList.end(), [](const QPair<QString, int>& a, const QPair<QString, int>& b) { return a.second > b.second; // 按值从大到小排序 }); // 输出排序后的结果 for (auto iter = myList.begin(); iter != myList.end(); ++iter) { qDebug() << iter->first << iter->second; } return 0; } ``` 输出结果为: ``` "c" 20 "d" 15 "a" 10 "b" 5 ``` 在此示例,我们首先创建一个 `QMap` 对象并插入一些键值对。然后,我们创建一个 `QList` 对象,并将 `QMap` 的元素存储到 `QList` 。接下来,我们使用 `qSort` 函数对 `QList` 的元素进行排序,排序函数使用一个 lambda 表达式来定义。最后,我们输出排序后的结果。 需要注意的是,由于 Qt 的 `QMap` 类是基于红黑树实现的,因此插入元素时会自动按键的顺序进行排序。如果您想要按值对 `QMap` 进行排序,可以使用上述方法将键值对存储到 `QList` ,然后对 `QList` 进行排序。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值