QMap类说明

QMap是一个模板类,提供了一个红黑树结构的查找字典。 注:红黑树结构是自平衡二叉树

QMap<key,T>是一个QT常用的容器类,它存储键值队,并且可以很快的根据键查找值。

QMap 和QHash提供很类似的功能,他们的区别如下:

1.  QHash 的查找性能更好;

2. 在遍历QHash时,里面是已经按字母排序好的,但是对于QMap,里面的东西都是按键分类的。

3. QHash的键类型必须提供一个==()的运算符重载并且提供一个通用的qHash(key)函数;QMap要提供一个<运算符重载以排序,

例子:

QMap<QString,int> map;


插入操作,

方式一 : 使用操作符[]():

map["one"]=1;

map["three"]=3;

map["seven"]=7;

方式二: 使用函数insert():

map.insert("twelve",12);



查找操作:

使用operator[]() 或者 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); //有timeout返回timeout'的值,没有返回30

总的来说,推荐使用contains()和value()来查找,而不是用[]()来查找,原因是[]()会在map没有改键值的时候插入一个键值对,比如如下代码,实际上在内存中会生成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).



If you want to navigate through all the (key, value) pairs stored in a QMap, you can use an iterator. QMap provides both Java-style iterators (QMapIterator and QMutableMapIterator) and STL-style iterators (QMap::const_iterator and QMap::iterator). Here's how to iterate over a QMap<QString, int> using a Java-style iterator:

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

Here's the same code, but using an STL-style iterator this time:

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

The items are traversed in ascending key order.

Normally, a QMap allows only one value per key. If you call insert() with a key that already exists in the QMap, the previous value will be erased. For example:

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

However, you can store multiple values per key by using insertMulti() instead of insert() (or using the convenience subclass QMultiMap). If you want to retrieve all the values for a single key, you can use values(const Key &key), which returns a QList<T>:

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

The items that share the same key are available from most recently to least recently inserted. Another approach is to call find() to get the STL-style iterator for the first item with a key and iterate from there:

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

If you only need to extract the values from a map (not the keys), you can also use foreach:

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

Items can be removed from the map in several ways. One way is to call remove(); this will remove any item with the given key. Another way is to use QMutableMapIterator::remove(). In addition, you can clear the entire map using clear().

QMap's key and value data types must be assignable data types. This covers most data types you are likely to encounter, but the compiler won't let you, for example, store a QWidget as a value; instead, store a QWidget *. In addition, QMap's key type must provide operator<(). QMap uses it to keep its items sorted, and assumes that two keys x and y are equal if neither x < y nor y < x is true.

Example:

 
  #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
 

In the example, we start by comparing the employees' names. If they're equal, we compare their dates of birth to break the tie.

See also QMapIterator, QMutableMapIterator, QHash, and QSet.

Member Type Documentation

typedef QMap::ConstIterator

Qt-style synonym for QMap::const_iterator

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值