自己做的网站如何管理,用npp做网站,发朋友圈吸引顾客话术,腾讯云服务器cvm目录
QMap类
QHash类
QVector类 QMap类
QMapkey,T提供一个从类型为Key的键到类型为T的值的映射。通常#xff0c;QMap存储的数据形式是一个键对应一个值#xff0c;并且按照键Key的次序存储数据。为了能够支持一键多值的情况#xff0c;QMap提供QMapkey,Tkey,T提供一个从类型为Key的键到类型为T的值的映射。通常QMap存储的数据形式是一个键对应一个值并且按照键Key的次序存储数据。为了能够支持一键多值的情况QMap提供QMapkey,T::insertMulti()和QMapkey,T::values()函数。QMultiMap类来实例化一个QMap对象
#include QCoreApplication#include QDebugint main(int argc, char *argv[])
{QCoreApplication a(argc, argv);//QMap类//1.创建QMap示例第一个参数为QString类型的键第二个参数为int类型的值QMapQString,int qmap;// 插入数据信息他有两种方式操作qmap[Chinese]119;qmap[English]120;qmap.insert(Math,115);qmap.insert(Physics,99);qmap.insert(Chemistry,100);qDebug()qmap;// 删除数据信息key键qmap.remove(Chemistry);qDebug()qmapendl;// 遍历QMap类的示例数据信息// 1.迭代器(java类型的迭代操作) QMapIterator是Qt框架中用于迭代QMap的迭代器类它提供了遍历QMap中键值对的功能QMapIteratorQString,int itr(qmap);while(itr.hasNext()) // 在while循环中使用hasNext()函数检查是否还有下一个键值对待迭代。{itr.next(); // 调用next()函数将迭代器移动到下一个键值对并可以通过key()和value()函数获取当前键和值的信息。qDebug()itr.key(): itr.value();}qDebug()endl;// 2.STL类型的迭代 QMapQString, int::const_iterator是QMap的常量迭代器类型用于遍历QMap中的键值对。QMapQString,int::const_iterator stritrqmap.constBegin(); // 通过调用constBegin()函数获取QMap的起始迭代器并将其赋值给stritr。while(stritr!qmap.constEnd()) // 在while循环中使用stritr ! qmap.constEnd() 条件判断是否还有下一个键值对待迭代。{qDebug()stritr.key():stritr.value();stritr;}// Key键/T键--来查找qDebug()endl;qDebug()key--T:qmap.value(Math);qDebug()T--key:qmap.key(99)endl;// 修改键值// 一个键对应一个值再次调用insert()函数将覆盖之前的值qmap.insert(Math,118);qDebug()qmap.value(Math);// 查询是否包含某个键qDebug()endl;qDebug()resultqmap.contains(Chinese);qDebug()resultqmap.contains(Chemistry);// 输出所有QMap实例化Key键和T值qDebug()endl;QListQString aKeysqmap.keys();qDebug()aKeys;QListint aValuesqmap.values();qDebug()aValues;// 一个键对应多个值// 直接使用QMultiMap类来实例化一个QMap对象qDebug()endl;QMultiMapQString,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类
QHashKey,T具有与QMap几乎完全相同的API。QHash维护者一张哈希表(Hash Table)哈希表的大小与QHash的数据项的数据相适应。QHash以任意的顺序组织它的数据当存储数据的顺序无关紧要时建议使用QHash作为存放数据的容器。
#include QCoreApplication#include QDebugint main(int argc, char *argv[])
{QCoreApplication a(argc, argv);QHashQString,int qhash;qhash[key 1] 3;qhash[key 2] 8;qhash[key 3] 4;qhash[key 4] 2;qhash.insert(key 3,30);QListQString listqhash.keys();for(int i0;ilist.length();i)qDebug()list[i],qhash.value(list[i]);// QHash内部的迭代器QHashIterator类qDebug()endl;QHashQString,int hash;hash[key 1]33;hash[key 2]44;hash[key 3]55;hash[key 4]66;hash.insert(key 3,100);QHashQString,int::const_iterator iterator;for(iteratorhash.begin();iterator!hash.end();iterator)qDebug()iterator.key()--iterator.value();return a.exec();
}结果如下 QMap 与 QHash 区别
QHash 与 QMap 的功能差不多但 QHash 的查找速度更快QMap 是按照键的顺序存储数据而QHash 是任意顺序存储的QMap的键必须提供 运算符而QHash的键必须提供”“运算符和一个名为qhash()的全局散列函数 QVector类
QVectorT在相邻的内存当中存储给定数据类型T的一组数值在一个QVector的前部或者中间位置进行插入操作的速度是很慢的这是因为这样的操作将导致内存中大量数据被移动这是由QVector存储数据的方式决定的。
#include QCoreApplication#include QDebugint main(int argc, char *argv[])
{QCoreApplication a(argc, argv);// QVectorT是Qt的一个容器类QVectorint qvr;// 第一种赋值方式qvr10;qvr20;qvr30;qvr40;// 第二种赋值方式qvr.append(50);qvr.append(60);qvr.append(70);qvr.append(80);qvr.append(90);qvr.append(100);qDebug()qvr countqvr.count()endl;// 遍历所有元素for(int i0;iqvr.count();i)qDebug()qvr[i];// 删除qvr容器里面的元素qDebug()endl;qvr.remove(0); // 删除第0个元素for(int i0;iqvr.count();i)qDebug()qvr[i];qvr.remove(2,3); // 从第二个元素开始删除后面3个元素qDebug()endl;for(int i0;qvr.count();i)qDebug()qvr[i];// 判断容器是否包含某个元素qDebug()endl;qDebug()resultqvr.contains(90);qDebug()resultqvr.contains(901)endl;return a.exec();
}结果如下