QPair Struct

template <typename T1, typename T2> struct QPair

The QPair class is a template class that stores a pair of items. 更多...

頭: #include <QPair>
qmake: QT += core

注意: 在此結構中的所有函數均 可重入 .

公共類型

typedef first_type
typedef second_type

公共函數

QPair (QPair<TT1, TT2> && p )
QPair (const QPair<TT1, TT2> & p )
QPair (const T1 & value1 , const T2 & value2 )
QPair ()
void swap (QPair<T1, T2> & other )
QPair<T1, T2> & operator= (const QPair<TT1, TT2> & p )
QPair<T1, T2> & operator= (QPair<TT1, TT2> && p )

公共變量

T1 first
T2 second
QPair<T1, T2> qMakePair (const T1 & value1 , const T2 & value2 )
void swap (QPair<T1, T2> & lhs , QPair<T1, T2> & rhs )
bool operator!= (const QPair<T1, T2> & p1 , const QPair<T1, T2> & p2 )
bool operator< (const QPair<T1, T2> & p1 , const QPair<T1, T2> & p2 )
QDataStream & operator<< (QDataStream & out , const QPair<T1, T2> & pair )
bool operator<= (const QPair<T1, T2> & p1 , const QPair<T1, T2> & p2 )
bool operator== (const QPair<T1, T2> & p1 , const QPair<T1, T2> & p2 )
bool operator> (const QPair<T1, T2> & p1 , const QPair<T1, T2> & p2 )
bool operator>= (const QPair<T1, T2> & p1 , const QPair<T1, T2> & p2 )
QDataStream & operator>> (QDataStream & in , QPair<T1, T2> & pair )

詳細描述

QPair<T1, T2> can be used in your application if the STL pair type is not available. It stores one value of type T1 and one value of type T2. It can be used as a return value for a function that needs to return two values, or as the value type of a generic container .

Here's an example of a QPair that stores one QString and one double 值:

QPair<QString, double> pair;
					

The components are accessible as public data members called first and second 。例如:

pair.first = "pi";
pair.second = M_PI;
					

Note, however, that it is almost always preferable to define a small struct to hold the result of a function with multiple return values. A struct trivially generalizes to more than two values, and allows more descriptive member names than 第一 and second :

struct Variable {
    QString name;
    double value;
};
Variable v;
v.name = "pi";
v.value = M_PI;
					

The advent of C++11 automatic variable type deduction ( auto ) shifts the emphasis from the type name to the name of functions and members. Thus, QPair, like std::pair and std::tuple , is mostly useful in generic (template) code, where defining a dedicated type is not possible.

QPair's template data types (T1 and T2) must be 可賦值數據類型 。例如,無法存儲 QWidget 作為值;取而代之,存儲 QWidget *. A few functions have additional requirements; these requirements are documented on a per-function basis.

另請參閱 容器類 .

成員類型文檔編製

typedef QPair:: first_type

The type of the first element in the pair (T1).

另請參閱 first .

typedef QPair:: second_type

The type of the second element in the pair (T2).

另請參閱 second .

成員函數文檔編製

template <typename TT1, typename TT2> QPair:: QPair ( QPair < TT1 , TT2 > && p )

Move-constructs a QPair instance, making it point to the same object that p 所指嚮的。

該函數在 Qt 5.2 引入。

template <typename TT1, typename TT2> QPair:: QPair (const QPair < TT1 , TT2 > & p )

Constructs a pair from the other pair p , of types TT1 and TT2. This constructor will fail if 第一 cannot be initialized from p.first or if second cannot be initialized from p.second .

該函數在 Qt 5.2 引入。

另請參閱 qMakePair ().

QPair:: QPair (const T1 & value1 , const T2 & value2 )

Constructs a pair and initializes the 第一 element with value1 second element with value2 .

另請參閱 qMakePair ().

QPair:: QPair ()

Constructs an empty pair. The 第一 and second elements are initialized with 默認構造值

void QPair:: swap ( QPair < T1 , T2 > & other )

Swaps this pair with other .

相當於

qSwap(this->first, other.first);
qSwap(this->second, other.second);
					

Swap overloads are found in namespace std as well as via argument-dependent lookup (ADL) in the namespace of T .

該函數在 Qt 5.5 引入。

template <typename TT1, typename TT2> QPair < T1 , T2 > &QPair:: operator= (const QPair < TT1 , TT2 > & p )

Copies pair p into this pair.

該函數在 Qt 5.2 引入。

另請參閱 qMakePair ().

template <typename TT1, typename TT2> QPair < T1 , T2 > &QPair:: operator= ( QPair < TT1 , TT2 > && p )

Move-assigns pair p into this pair instance.

該函數在 Qt 5.2 引入。

成員變量文檔編製

T1 QPair:: first

The first element in the pair.

T2 QPair:: second

The second element in the pair.

相關非成員

template <typename T1, typename T2> QPair < T1 , T2 > qMakePair (const T1 & value1 , const T2 & value2 )

返迴 QPair <T1, T2> that contains value1 and value2 。範例:

QList<QPair<int, double> > list;
list.append(qMakePair(66, M_PI));
					

這相當於 QPair <T1, T2>( value1 , value2 ), but usually requires less typing.

template <typename T1, typename T2> void swap ( QPair < T1 , T2 > & lhs , QPair < T1 , T2 > & rhs )

這是重載函數。

交換 lhs with rhs .

該函數在 Qt 5.5 引入。

template <typename T1, typename T2> bool operator!= (const QPair < T1 , T2 > & p1 , const QPair < T1 , T2 > & p2 )

返迴 true if p1 不等於 p2 ; otherwise returns false. Two pairs compare as not equal if their 第一 data members are not equal or if their second data members are not equal.

This function requires the T1 and T2 types to have an implementation of operator==() .

template <typename T1, typename T2> bool operator< (const QPair < T1 , T2 > & p1 , const QPair < T1 , T2 > & p2 )

返迴 true if p1 小於 p2 ; otherwise returns false. The comparison is done on the 第一 members of p1 and p2 ; if they compare equal, the second members are compared to break the tie.

This function requires the T1 and T2 types to have an implementation of operator<() .

template <typename T1, typename T2> QDataStream & operator<< ( QDataStream & out , const QPair < T1 , T2 > & pair )

寫入對 pair 到流 out .

此函數要求 T1 和 T2 類型以實現 operator<<() .

另請參閱 序列化 Qt 數據類型 .

template <typename T1, typename T2> bool operator<= (const QPair < T1 , T2 > & p1 , const QPair < T1 , T2 > & p2 )

返迴 true if p1 <= p2 ;否則返迴 false . The comparison is done on the 第一 members of p1 and p2 ; if they compare equal, the second members are compared to break the tie.

This function requires the T1 and T2 types to have an implementation of operator<() .

template <typename T1, typename T2> bool operator== (const QPair < T1 , T2 > & p1 , const QPair < T1 , T2 > & p2 )

返迴 true if p1 等於 p2 ;否則返迴 false . Two pairs compare equal if their 第一 data members compare equal and if their second data members compare equal.

This function requires the T1 and T2 types to have an implementation of operator==() .

template <typename T1, typename T2> bool operator> (const QPair < T1 , T2 > & p1 , const QPair < T1 , T2 > & p2 )

返迴 true if p1 大於 p2 ; otherwise returns false. The comparison is done on the 第一 members of p1 and p2 ; if they compare equal, the second members are compared to break the tie.

This function requires the T1 and T2 types to have an implementation of operator<() .

template <typename T1, typename T2> bool operator>= (const QPair < T1 , T2 > & p1 , const QPair < T1 , T2 > & p2 )

返迴 true if p1 >= p2 ;否則返迴 false . The comparison is done on the 第一 members of p1 and p2 ; if they compare equal, the second members are compared to break the tie.

This function requires the T1 and T2 types to have an implementation of operator<() .

template <typename T1, typename T2> QDataStream & operator>> ( QDataStream & in , QPair < T1 , T2 > & pair )

讀取對從流 in into pair .

此函數要求 T1 和 T2 類型以實現 operator>>() .

另請參閱 序列化 Qt 數據類型 .