QMap Class

The QMap class is a template class that provides a red-black-tree-based dictionary.

Header: #include
qmake: QT += core

  1. //Declare
  2. QMap<QString, int>map
  3. //Use Operator
  4. map['one'] = 1;
  5. map['three'] = 3;
  6. map['seven'] = 7;
  7. //insert value
  8. map.insert('twelve' ,12);
  9. // use operator[]
  10. int num1 = map["thirteen"]
  11. int num2 = map["thirteen"]
  12. //
  13. int timeout = 30;
  14. if(map.contains("TIMEOUT"));
  15. timeout = map.value("TIMEOUT");
  16. //
  17. QMapIterator<QString, int> i(map);
  18. while (i.hasNext()) {
  19. i.next();
  20. cout << i.key() << ": " << i.value() << Qt::endl;
  21. }

QSharedPointer Class

Header: #include
qmake: QT += core

The QSharedPointer is an automatic, shared pointer in C++. It behaves exactly like a normal pointer for normal purposes, including respect for constness. QSharedPointer will delete the pointer it is holding when it goes out of scope, provided no other QSharedPointer objects are referencing it.


线程安全

QSharedPointer and QWeakPointer are reentrant classes. This means that, in general, a given QSharedPointer or QWeakPointer object cannot be accessed by multiple threads at the same time without synchronization.