函數(shù)原型
C++中std::tie函數(shù)的作用就是從元素引用中生成一個(gè)tuple元組,其在頭文件中定義,其函數(shù)原型如下:
template< class... Types >
std::tuple< Types&... > tie( Types&... args ) noexcept; //C++11起, C++14前
template< class... Types >
constexpr std::tuple< Types&... > tie( Types&... args ) noexcept;//C++14起
其中參數(shù) args 為構(gòu)造 tuple 所用的零或更多左值參數(shù)。其返回值為含的std::tuple對(duì)象。
作用和用法
1. 解包 tuple 和 pair
std::tie 可以用于解包 tuple 和 pair,因?yàn)?std::tuple 擁有從 pair 的轉(zhuǎn)換賦值。
注:元組tuple可以將不同類型的元素存放在一起,可以理解為pair的擴(kuò)展(pair只能包含兩個(gè)元素,而tuple可以多個(gè))。
std::tuple擁有從 pair 的轉(zhuǎn)換賦值的主要原因就是:tuple的實(shí)現(xiàn)中重載了 operator=,其部分原型如下:
template< class U1, class U2 > tuple& operator=( const std::pair< U1, U2 >& p );//C++11 起, C++20 前
因此,std::tie可以用于pair的解包:
std::set< int > set;
std::set< int >::iterator iter;
bool result;
std::tie(iter, result) = set.insert(value);//解包 insert 的返回值為 iter 與 result
std::tie(std::ignore, result) = set.insert(value);//使用std::ignore忽略insert的返回pair中的第一個(gè)元素
注 :std::ignore 是令 std::tie 在解包 std::tuple 時(shí)作為不使用的參數(shù)的占位符使用,即忽略某些 tuple 中的某些返回值。
2. 批量賦值
std::tie 可以將多個(gè)變量的引用整合成一個(gè) tuple,進(jìn)而通過另外一個(gè)同類型的 tuple 進(jìn)行批量賦值。
tuple< string, double, int > tup("idoit", 98.8, 1);
string name;
double score;
int rank;
//通過變量tup實(shí)現(xiàn)對(duì)name、score、rank的批量賦值操作
tie(name, score, rank) = tup;
3. 比較結(jié)構(gòu)體
可以將結(jié)構(gòu)體成員傳入std::tie,從而實(shí)現(xiàn)結(jié)構(gòu)體的比較。
struct S {
int n;
std::string s;
float d;
bool operator< (const S& rhs) const
{
// 比較 n 與 rhs.n,
// 然后為 s 與 rhs.s,
// 然后為 d 與 rhs.d
return std::tie(n, s, d) < std::tie(rhs.n, rhs.s, rhs.d);
//注:由于tie返回的是一個(gè) tuple,tuple的實(shí)現(xiàn)已經(jīng)重載了operator< ,因此可以利用tuple的operator< ,進(jìn)而實(shí)現(xiàn)結(jié)構(gòu)體S的operator< 。
}
};
具體示例
#include < iostream >
#include < set >
#include < string >
#include < tuple >
using namespace std;
struct S {
int n;
string s;
float d;
bool operator< (const S& rhs) const {
// 比較 n 與 rhs.n,
// 然后為 s 與 rhs.s,
// 然后為 d 與 rhs.d
return tie(n, s, d) < tie(rhs.n, rhs.s, rhs.d);
}
};
int main() {
set< S > set_of_s;
S value1{42, "Test1", 3.14};
S value2{23, "Test2", 3.14};
set< S >::iterator iter;
bool result;
/************解包**********/
tie(iter, result) = set_of_s.insert(value1);
if (result) cout < < "Value1 was inserted successfullyn";
tie(std::ignore, result) = set_of_s.insert(
value2); // 使用std::ignore忽略insert的返回pair中的第一個(gè)元素
if (result) cout < < "Value2 was inserted successfullyn";
/***********結(jié)構(gòu)體比較**********/
bool r = value1 < value2;
cout < < "value1 < value2 : " < < r < < endl;
/***********批量賦值**********/
tuple< string, double, int > tup("idoit", 98.8, 1);
string name;
double score;
int rank;
tie(name, score, rank) = tup;
cout < < name < < " " < < score < < " " < < rank < < endl;
return 0;
}
輸出結(jié)果:
Value1 was inserted successfully
Value2 was inserted successfully
value1 < value2 : 0
idoit 98.8 1
-
轉(zhuǎn)換器
+關(guān)注
關(guān)注
27文章
8748瀏覽量
148118 -
C++語言
+關(guān)注
關(guān)注
0文章
147瀏覽量
7032
發(fā)布評(píng)論請(qǐng)先 登錄
相關(guān)推薦
C++虛函數(shù)virtual詳解
c++中冒號(hào)(:)和雙冒號(hào)(::)的用法
C/C++回調(diào)函數(shù)
嵌入式-C++函數(shù)的重載
如何在中斷C函數(shù)中調(diào)用C++
![如何在中斷<b class='flag-5'>C</b><b class='flag-5'>函數(shù)</b>中調(diào)用<b class='flag-5'>C++</b>](https://file.elecfans.com/web1/M00/91/BA/pIYBAFzTzJuAT0pBAAEyN2vFGuQ683.png)
C語言和C++的特點(diǎn)與用法詳細(xì)說明
EE-128:C++中的DSP:從C++調(diào)用匯編類成員函數(shù)
![EE-128:<b class='flag-5'>C++</b>中的DSP:從<b class='flag-5'>C++</b>調(diào)用匯編類成員<b class='flag-5'>函數(shù)</b>](https://file.elecfans.com/web1/M00/D9/4E/pIYBAF_1ac2Ac0EEAABDkS1IP1s689.png)
在C++中如何用虛函數(shù)實(shí)現(xiàn)多態(tài)
深度解析C++中的虛函數(shù)
![深度解析<b class='flag-5'>C++</b>中的虛<b class='flag-5'>函數(shù)</b>](https://file.elecfans.com/web2/M00/91/66/pYYBAGPsTWqAcRlFAAEnF_VliNI953.jpg)
C++基礎(chǔ)知識(shí)之函數(shù)1
?數(shù)組和C++ std::array詳解
![?數(shù)組和<b class='flag-5'>C++</b> <b class='flag-5'>std</b>::array詳解](https://file1.elecfans.com/web2/M00/8D/21/wKgZomS3UjCAegESAABp4uummBE582.jpg)
評(píng)論