QT——QMap

来源:互联网 发布:魔方一键网络共享工具 编辑:程序博客网 时间:2024/05/16 10:51

The QMap class is a template class that provides a red-black-tree-based dictionary.
QMap类一个提供基于红黑树词典的模板类。

QMap<Key, T> is one of Qt's generic container classes.
It stores (key, value) pairs and provides fast lookup of the value associated with a key.
QMap<Key, T>是Qt通用容器类之一。
它存储成对的 (key, value) 并且提供快速的查找到和 key 相关联的 value。


QMap and QHash provide very similar functionality. The differences are:
QMap和QHash提供了非常相似的功能。有如下的不同:
QHash provides average faster lookups than QMap. (See Algorithmic Complexity for details.)
QHash比QMap平均查找速度更快。(查看Algorithmic Complexity获取细节)
When iterating over a QHash, the items are arbitrarily ordered. With QMap, the items are always sorted by key.
当通过QHash进行迭代时,所有的项是任意排序的。但是QMap,所有项总是通过key来排序的。
The key type of a QHash must provide operator==() and a global qHash(Key) function. 
The key type of a QMap must provide operator<() specifying a total order.
QHash的key类型必须提供重载运算符==,和全局函数qHash(Key)。
QMap的key类型必须提供重载运算符<,提供一个全局排序的条件。


Here's an example QMap with QString keys and int values:
下面是QString key和int values之间的映射QMap:
QMap<QString, int> map;

To insert a (key, value) pair into the map, you can use operator[]():
可以使用运算符[ ],向 map 中插入相配对的  (key, value):
map["one"] = 1;
map["three"] = 3;
map["seven"] = 7;
This inserts the following three (key, value) pairs into the QMap: ("one", 1), ("three", 3), and ("seven", 7).
将下列三对(key, value),插入到 ("one", 1), ("three", 3), and ("seven", 7)中。
Another way to insert items into the map is to use insert():
另一种相 map对象中插入项的方法是使用insert():
map.insert("twelve", 12);

To look up a value, use operator[]() or value():
使用运算符[ ] 或者 value() 查找 值:
int num1 = map["thirteen"];
int num2 = map.value("thirteen");

If there is no item with the specified key in the map, these functions return a default-constructed value.
如果 map 对象中指定的key没有对应的项,函数将返回 “default-constructed value”。
If you want to check whether the map contains a certain key, use contains():
如果你想检测 map 中是否包含某一 可以,使用 contais():
int timeout = 30;
if (map.contains("TIMEOUT"))
    timeout = map.value("TIMEOUT");

There is also a value() overload that uses its second argument as a default value if there is no item with the specified key:
还有一个重载的 value()函数 ,如果没有指定的key没有对应的项,则其第二个参数作为默认值返回,如果没有指定默认值,返回“default-constructed value”。
int timeout = map.value("TIMEOUT", 30);

In general, we recommend that you use contains() and value() rather than operator[]() for looking up a key in a map. 
The reason is that operator[]() silently inserts an item into the map if no item exists with the same key (unless the map is const). For example, the following code snippet will create 1000 items in memory:
通常情况下,我们建议你使用contains() 和 value(),而不是运算符[ ] 在 map 中进行查找。
理由:如果不存在具有相同key的item,[ ]会默默的将 item插入到 map 中(除非 map 是 const) 。
// WRONG
QMap<int, QWidget *> map;
...
for (int i = 0; i < 1000; ++i) {
    if (map[i] == okButton)
        cout << "Found button at index " << i << endl;
}
To avoid this problem, replace map[i] with map.value(i) in the code above.
为了避免以上问题,在上述代码中用map.value(i)替代map[i]。([ ]用于向 map 中插入值,不用于取值)

=========================================================================================

实例:

class Personal_info{
private:                                               
    double balance;                                          

public:
    Personal_info(){
    

    }
    double get_Balance()
    {
      
    }
};

--------------------------------------------

QMap<int,Personal_info> ID_map_Personal;

double balance = ID_map_Personal[ Current.toInt()].get_Balance();


0 0
原创粉丝点击