首页 > 其他分享 >QMap

QMap

时间:2023-03-03 16:23:32浏览次数:40  
标签:map const QMap iterator Key key

QMap 

#include <QMap>

 

Public Functions

  QMap()
  QMap(std::initializer_list<std::pair<Key, T> > list)
  QMap(const QMap<Key, T> &other)
  QMap(QMap<Key, T> &&other)
  QMap(const typename std::map<Key, T> &other)
  ~QMap()
QMap::iterator begin()
QMap::const_iterator begin() const
QMap::const_iterator cbegin() const
QMap::const_iterator cend() const
void clear()
QMap::const_iterator constBegin() const
QMap::const_iterator constEnd() const
QMap::const_iterator constFind(const Key &key) const
QMap::const_key_value_iterator constKeyValueBegin() const
QMap::const_key_value_iterator constKeyValueEnd() const
bool contains(const Key &key) const
int count(const Key &key) const
int count() const
bool empty() const
QMap::iterator end()
QMap::const_iterator end() const
QPair<QMap::iterator, QMap::iterator> equal_range(const Key &key)
QPair<QMap::const_iterator, QMap::const_iterator> equal_range(const Key &key) const
QMap::iterator erase(QMap::iterator pos)
QMap::iterator find(const Key &key)
QMap::const_iterator find(const Key &key) const
T & first()
const T & first() const
const Key & firstKey() const
QMap::iterator insert(const Key &key, const T &value)
QMap::iterator insert(QMap::const_iterator pos, const Key &key, const T &value)
QMap::iterator insertMulti(const Key &key, const T &value)
QMap::iterator insertMulti(QMap::const_iterator pos, const Key &key, const T &value)
bool isEmpty() const
const Key key(const T &value, const Key &defaultKey = Key()) const
QMap::key_iterator keyBegin() const
QMap::key_iterator keyEnd() const
QMap::key_value_iterator keyValueBegin()
QMap::const_key_value_iterator keyValueBegin() const
QMap::key_value_iterator keyValueEnd()
QMap::const_key_value_iterator keyValueEnd() const
QList<Key> keys() const
QList<Key> keys(const T &value) const
T & last()
const T & last() const
const Key & lastKey() const
QMap::iterator lowerBound(const Key &key)
QMap::const_iterator lowerBound(const Key &key) const
int remove(const Key &key)
int size() const
void swap(QMap<Key, T> &other)
T take(const Key &key)
std::map<Key, T> toStdMap() const
QList<Key> uniqueKeys() const
QMap<Key, T> & unite(const QMap<Key, T> &other)
QMap::iterator upperBound(const Key &key)
QMap::const_iterator upperBound(const Key &key) const
const T value(const Key &key, const T &defaultValue = T()) const
QList<T> values() const
QList<T> values(const Key &key) const
bool operator!=(const QMap<Key, T> &other) const
QMap<Key, T> & operator=(const QMap<Key, T> &other)
QMap<Key, T> & operator=(QMap<Key, T> &&other)
bool operator==(const QMap<Key, T> &other) const
T & operator[](const Key &key)
const T operator[](const Key &key) const

 

 

QMap类是一个模板类,它提供基于红黑树的字典。

QMap<Key,T>是Qt的通用容器类之一。它存储(键、值)对,并提供与键关联的值的快速查找。

QMap和QHash提供了非常相似的功能。区别在于:

QHash提供了比QMap平均更快的查找。(有关详细信息,请参阅算法复杂性。)

在QHash上迭代时,项目是任意排序的。使用QMap,项目始终按关键字排序。

QHash的密钥类型必须提供运算符==()和全局QHash(key)函数。QMap的键类型必须提供指定总顺序的运算符<()。由于Qt 5.8.1,使用指针类型作为键也是安全的,即使底层运算符<()不提供总顺序。

 

 

初始化:

下面是一个带有QString键和int值的QMap示例:

 QMap<QString, int> map;

 

插值:

要在映射中插入(键,值)对,可以使用运算符[]():

map["one"] = 1;
map["three"] = 3;
map["seven"] = 7;

 

这三个(键、值)对插入QMap:(“one”,1)、(“three”,3)和(“seven”,7)。将项目插入映射的另一种方法是使用insert():

map.insert("twelve", 12);

 

 

查询:

要查找值,请使用运算符[]()或value():

int num1 = map["thirteen"];
int num2 = map.value("thirteen");

 

 

 

如果映射中没有具有指定键的项,这些函数将返回默认构造值。

如果要检查映射是否包含某个键,请使用contains():

 int timeout = 30;
 if (map.contains("TIMEOUT"))
      timeout = map.value("TIMEOUT");

 

还有一个value()重载,如果没有具有指定键的项,则使用其第二个参数作为默认值:

int timeout = map.value("TIMEOUT", 30);

 

 

通常,我们建议您使用contains()和value()而不是运算符[]()来查找映射中的键。原因是,如果不存在具有相同键的项(除非映射是常量),则运算符[]()会在映射中无声地插入一个项。例如,以下代码片段将在内存中创建1000个项:

// WRONG
QMap<int, QWidget *> map;
...
for (int i = 0; i < 1000; ++i) {
      if (map[i] == okButton)
          cout << "Found button at index " << i << endl;
}

 

 

 

为了避免这个问题,请在上面的代码中将map[i]替换为map.value(i)。

如果要浏览QMap中存储的所有(键、值)对,可以使用迭代器。QMap提供Java风格的迭代器(QMapIterator和QMutableMapIterator)和STL风格的迭代器(QMap::const_iterator和QMap:迭代器)。以下是如何使用Java风格迭代器对QMap<QString,int>进行迭代:

QMapIterator<QString, int> i(map);
while (i.hasNext()) {
      i.next();
      cout << i.key() << ": " << i.value() << endl;
}

 

 

下面是相同的代码,但这次使用了STL风格的迭代器:

 

QMap<QString, int>::const_iterator i = map.constBegin();
while (i != map.constEnd()) {
      cout << i.key() << ": " << i.value() << endl;
      ++i;
}

 

 

项目按升序遍历。

通常,QMap只允许每个键有一个值。如果使用QMap中已存在的键调用insert(),则将删除先前的值。例如:

map.insert("plenty", 100);
map.insert("plenty", 2000);
// map.value("plenty") == 2000

 

 

但是,可以使用insertMulti()代替insert()(或使用方便的子类QMultiMap)为每个键存储多个值。如果要检索单个键的所有值,可以使用值(const-key&key),它返回一个QList<T>:

QList<int> values = map.values("plenty");
for (int i = 0; i < values.size(); ++i)
      cout << values.at(i) << endl;

 

 

共享同一密钥的项目从最近插入到最近插入。另一种方法是调用find()获取第一个带有键的项目的STL风格迭代器,然后从那里进行迭代:

QMap<QString, int>::iterator i = map.find("plenty");
while (i != map.end() && i.key() == "plenty") {
      cout << i.value() << endl;
      ++i;
}

 

 

如果只需要从贴图(而不是关键帧)中提取值,还可以使用foreach:

 QMap<QString, int> map;
 ...
 foreach (int value, map)
      cout << value << endl;

 

 

 

可以通过多种方式从地图中删除项目。一种方法是调用remove();这将删除具有给定密钥的任何项。另一种方法是使用QMutableMapIterator::remove()。此外,可以使用clear()清除整个地图。

QMap的键和值数据类型必须是可分配的数据类型。这涵盖了您可能遇到的大多数数据类型,但编译器不允许您将QWidget存储为值;而是存储QWidget*。此外,QMap的键类型必须提供运算符<()。QMap使用它来保持项目的排序,并假设如果x<y或y<x都不为真,则两个键x和y相等。

例子:

  #ifndef EMPLOYEE_H
  #define EMPLOYEE_H

  class Employee
  {
  public:
      Employee() {}
      Employee(const QString &name, const QDate &dateOfBirth);
      ...

  private:
      QString myName;
      QDate myDateOfBirth;
  };

  inline bool operator<(const Employee &e1, const Employee &e2)
  {
      if (e1.name() != e2.name())
          return e1.name() < e2.name();
      return e1.dateOfBirth() < e2.dateOfBirth();
  }

  #endif // EMPLOYEE_H

 

 

 

#########################

标签:map,const,QMap,iterator,Key,key
From: https://www.cnblogs.com/herd/p/17176090.html

相关文章