42-QMap&QHash&QVector

 一 QMap

#include <QCoreApplication>

#include <QDebug>
using namespace Qt;
int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    //QMap类
    //1.创建QMap实例,第一个参数为QString类型的键,第二个参数
    //为int类型的值
    QMap<QString,int> qmap;
    //如何插入数据信息,两种方式进行操作
    //(1)
    qmap["chinese"] =119;
    qmap["English"] =120;

    //(2)
    qmap.insert("Math",115);
    qmap.insert("Physics",99);
    qmap.insert("Chemistry",100);
    //(3)输出
    qDebug()<< qmap;

    //删除数据信息根据key键
    qmap.remove("Chemistry");
    qDebug()<< qmap;

    //遍历QMap类实例:数据信息()

    //1.迭代器(java类型)
    QMapIterator<QString,int> itr(qmap);
    while(itr.hasNext())
    {
        itr.next();
        qDebug()<< itr.key()<<":"<<itr.value()<<endl;
    }

    qDebug()<<endl;
    //2.STL类型迭代出来
    QMap<QString,int>::const_iterator stritr=qmap.constBegin();
    while(stritr != qmap.constEnd())
    {
        qDebug()<< stritr.key()<<":"<<stritr.value()<<endl;
        stritr++;
    }

    //查找 key值 /T值-->查找
    //QMap<key ,value>
    qDebug()<<"根据键key查找对应的T:"<< qmap.value("Math")<< endl;
    qDebug()<<"T ----->key:"<< qmap.key(99)<<endl;


    //修改键值
    //一个键对应一个值,但是再次调用insert()函数将覆盖之前的值
    qmap.insert("Math",118);

    qDebug()<<qmap.value("Math");

    //查询是否包含某个键
    qDebug()<<"result ="<<qmap.contains("chinese");
    qDebug()<<"result ="<<qmap.contains("Chemistry");

    //输出所有QMap实例化,key值和T键值、
    qDebug()<< endl;
    QList<QString> akeys = qmap.keys();
    qDebug()<<akeys;
    QList<int> aValues = qmap.values();
    qDebug()<<aValues;

    //一个键对应多个值
    //直接使用QMultiMap类来实例化一个QMap对象
    qDebug()<< endl;
    QMultiMap<QString,QString> mulmap;
    mulmap.insert("student","no");
    mulmap.insert("student","name");
    mulmap.insert("student","sex");
    mulmap.insert("student","age");
    mulmap.insert("student","high");
    mulmap.insert("student","weight");
    qDebug()<< mulmap;//从输出的结果可以看出mulmap仍然是一个QMap对象


    return a.exec();
}

二 QHash

#include <QCoreApplication>
#include <QDebug>
int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    //QHash类
    QHash<QString,int> qhash;
    qhash["key 1"] = 3;
    qhash["key 1"] = 8;
    qhash["key 4"] = 4;
    qhash["key 2"] = 2;
    qhash.insert("key 3",30);
    QList<QString> list = qhash.keys();
    //for循环输出
    for(int i = 0;i<list.length();i++)
    {
        qDebug()<<list[i] <<","<<qhash.value(list[i]);
    }
    //使用QHash内部的QHashIterator迭代器类
    QHash<QString,int> hash;
    hash["key 1"] = 33;
    hash["key 2"] = 44;
    hash["key 3"] = 55;
    hash["key 4"] = 66;
    hash.insert("key 3",100);
    QHash<QString,int>::const_iterator iterator;
    for(iterator = hash.begin();iterator != hash.end();iterator++)
    {
        qDebug()<<iterator.key()<<"--->"<<iterator.value();
    }

    //qDebug()<<"";
    return a.exec();
}

三 QMap 与 QHash 区别

QHash 与 QMap 的功能差不多,

但 QHash 的查找速度更快,

QMap是按照键的顺序存储数据,而 QHash 是任意顺序存储的;

QMap的键必须提供"<”运算符,

而 QHash 的键必须提供'=="运算符和一个名为Hash的全局散列函数,

四 QVector类

#include <QCoreApplication>
#include <QDebug>
#include <QVector>
using namespace Qt;
int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    //QVector类
    //QVector<T>是Qt的一个容器类

    QVector<int> qvr;
    //第一种方式赋值
    qvr<<10;
    qvr<<20;
    qvr<<30;
    //第二种方式赋值
    qvr.append(50);
    qvr.append(60);
    qvr.append(70);
    qvr.append(80);
    qvr.append(90);
    qvr.append(100);

    qDebug()<< qvr<< endl;

    //求出Qvector类容器的实例化:元素个数
    qDebug()<<"qvr count"<< qvr.count()<< endl;

    //遍历所有元素
    for(int i = 0;i<qvr.count();i++)
    {
        qDebug()<<qvr[i];
    }
    //删除qvr容器里面的元素
     qDebug()<<endl;

    qvr.remove(0);

     for(int i = 0;i<qvr.count();i++)
     {
        qDebug()<<qvr[i];
     }
     //指定范围
     qDebug()<<endl;
     qvr.remove(2,3);//从第二个元素开始,删除后面三个元素

     for(int i = 0;i<qvr.count();i++)
     {
        qDebug()<<qvr[i];
     }
     qDebug()<<endl;
     //判断容器是否包含某个元素
     qDebug()<<endl;
     qDebug()<< qvr.contains(90)<< endl;

     qDebug()<< qvr.contains(901)<< endl;
    return a.exec();
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值