求QList 删除元素比较高效的算法问题

振星 2015-01-29 03:13:04
比方有有个数组QList中存放结构体,如QList<StructInfo> ,代码如下:

StructInfo 代码如下

struct StructInfo
{
QString key;
QString value1;
QString value2;
int arg1;
int arg2;
...;
}


当QList 数组存放比较大的数据(超过1W条时),我希望可以根据key删除QList中所有结构体的key等于传入的key,
请问应该怎么做比较合理?

...全文
2209 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
Jiao319 2015-02-15
  • 打赏
  • 举报
回复
qmap或者重载结构体的==操作。都被别人说了
Inhibitory 2015-02-15
  • 打赏
  • 举报
回复
bool QList::removeOne(const T & value) Removes the first occurrence of value in the list and returns true on success; otherwise returns false. Example: QList<QString> list; list << "sun" << "cloud" << "sun" << "rain"; list.removeOne("sun"); // list: ["cloud", ,"sun", "rain"] This function requires the value type to have an implementation of operator==(). This function was introduced in Qt 4.4.
#include <QList>
#include <QDebug>
#include <QString>

struct StructInfo {
    QString key;
    QString value1;
    QString value2;
    int arg1;
    int arg2;

    StructInfo() {

    }

    StructInfo(const QString &key, int arg1) {
        this->key = key;
        this->arg1 = arg1;
    }

    bool operator==(const StructInfo &other) const {
        return this->key == other.key;
    }
};

int main(int argc, char *argv[]) {
    QList<StructInfo> list;

    list.append(StructInfo("One", 1));
    list.append(StructInfo("Tow", 2));
    list.append(StructInfo("Three", 3));

    for (StructInfo info : list) {
        qDebug() << info.arg1;
    }

    qDebug() << "------------------------";

    list.removeOne(StructInfo("One", 1));

    for (StructInfo info : list) {
        qDebug() << info.arg1;
    }

    return 0;
}
tyousi 2015-02-13
  • 打赏
  • 举报
回复
QMap<QString, StructInfo> map; map[key] = StructInfo(); map.remove(key);
donwmufromdying 2015-02-04
  • 打赏
  • 举报
回复
你用QHash吧
乔巴好萌 2015-01-29
  • 打赏
  • 举报
回复 1
QList只能遍历 你无论怎么优化 都是需要遍历至少一遍的 也就是你的复杂度至少是o(n) 其实这个已经算比较低的了 除非你改成QMap
  • 打赏
  • 举报
回复
你的struct是固定了还是啥?我感觉,做个二级链表应该就可以了。

16,225

社区成员

发帖
与我相关
我的任务
社区描述
Qt 是一个跨平台应用程序框架。通过使用 Qt,您可以一次性开发应用程序和用户界面,然后将其部署到多个桌面和嵌入式操作系统,而无需重复编写源代码。
社区管理员
  • Qt
  • 亭台六七座
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧