返回介绍

QPair Class

发布于 2019-10-04 15:01:56 字数 3404 浏览 1008 评论 0 收藏 0

The QPair class is a value-based template class that provides a pair of elements. More...

#include <qpair.h>

List of all member functions.

Public Members

  • typedef T1first_type
  • typedef T2second_type
  • QPair ()
  • QPair ( constT1&t1, constT2&t2 )

Related Functions

  • QPair qMakePair ( T1t1, T2t2 )

Detailed Description

The QPair class is a value-based template class that provides a pair of elements.

QPair is a Qt implementation of an STL-like pair. It can be used in your application if the standard pair<> is not available.

QPair<T1, T2> defines a template instance to create a pair of values that contains two values of type T1 and T2. Please note that QPair does not store pointers to the two elements; it holds a copy of every member. This is why these kinds of classes are called value based. If you're interested in pointer based classes see, for example, QPtrList and QDict.

QPair holds one copy of type T1 and one copy of type T2, but does not provide iterators to access these elements. Rather, the two elements (first and second) are public member variables of the pair. QPair owns the contained elements. For more relaxed ownership semantics, see QPtrCollection and friends which are pointer-based containers.

Some classes cannot be used within a QPair: for example, all classes derived from QObject and thus all classes that implement widgets. Only "values" can be used in a QPair. To qualify as a value the class must provide:

  • A copy constructor
  • An assignment operator
  • A constructor that takes no argument

Note that C++ defaults to field-by-field assignment operators and copy constructors if no explicit version is supplied. In many cases this is sufficient.

QPair uses an STL-like syntax to manipulate and address the objects it contains. See the QTL documentation for more information.

Functions that need to return two values can use a QPair. The qMakePair() convenience function makes it easy to create QPair objects.

See also Qt Template Library Classes, Implicitly and Explicitly Shared Classes and Non-GUI Classes.


Member Type Documentation

QPair::first_type

The type of the first element in the pair.

QPair::second_type

The type of the second element in the pair.

Member Function Documentation

QPair::QPair ()

Constructs an empty pair. The first and second elements are default constructed.

QPair::QPair ( constT1&t1, constT2&t2 )

Constructs a pair and initializes the first element with t1 and the second element with t2.

Related Functions

QPair qMakePair ( T1t1, T2t2 )

This is a template convenience function. It is used to create a QPair<> object that contains t1 and t2. For example:

    QMap<QString,QString> m;
    m.insert( qMakePair("Clinton", "Bill") );

The above code is equivalent to:

    QMap<QString,QString> m;
    QPair<QString,QString> p( "Clinton", "Bill" );
    m.insert( p );

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文