窥视C++细节-使用tie函数解包pair对象的原理
文章目錄
- 示例
- std::tie函數(shù)
- std::ignore
- std::pair和std::tuple互轉(zhuǎn)
- 解包pair返回值
示例
這是http://www.cplusplus.com/reference/tuple/tie/?kw=tie中的一個(gè)std::tie()函數(shù)的示例。
#include <iostream> // std::cout #include <tuple> // std::tuple, std::make_tuple, std::tieint main () {int myint;char mychar;std::tuple<int,float,char> mytuple;mytuple = std::make_tuple (10, 2.6, 'a'); // packing values into tuplestd::tie (myint, std::ignore, mychar) = mytuple; // unpacking tuple into variablesstd::cout << "myint contains: " << myint << '\n';std::cout << "mychar contains: " << mychar << '\n';return 0; } myint contains: 10 mychar contains: astd::tie函數(shù)
std::tie函數(shù)可以用于建立一個(gè)由reference構(gòu)成的tuple,也可以用于tuple的解包。
可能的實(shí)現(xiàn):
template <typename... Args> auto tie(Args&... args) {return std::tuple<Args&...>(args...); }std::tie函數(shù),將創(chuàng)建一個(gè)由reference構(gòu)成的匿名tuple對象,并將其返回。
這里比較有意思的是,std::tie創(chuàng)建的匿名tuple對象,其內(nèi)部通過引用和傳入的參數(shù)想關(guān)聯(lián),隨意一方的改動,對方可以感知,是同步的。
我們使用一個(gè)簡單的示例模式一下,代碼如下:
class A { public:A(int& i):ri(i){}void set(int x){ri = x;}private:int & ri; };int main() {int i = 10;A a(i);a.set(20);cout<<"i = "<<i<<endl;return 0; } i = 20通過傳遞變量的引用,使得在多處可以對同一個(gè)變量進(jìn)行修改。
std::tuple<int,float,char> mytuple;mytuple = std::make_tuple (10, 2.6, 'a');先是使用默認(rèn)構(gòu)造器,創(chuàng)建mytuple對象。
使用make_tuple可以快速的創(chuàng)建tuple對象,同時(shí)make_tuple是一個(gè)模板參數(shù),省去了指定參數(shù)的類型。
mytuple = std::make_tuple<int,float,char>(10,2.6,'a');這樣也是可以的
make_tuple將返回的tuple對象,賦值給mytuple,將發(fā)生一次賦值運(yùn)算符重載。此后mytuple就保存了10,2.6,'a’這三個(gè)數(shù)據(jù)。
std::tie (myint, std::ignore, mychar) = mytuple;tie將返回一個(gè)臨時(shí)的匿名的tuple對象,這個(gè)tuple對象和myint、mychar變量通過引用進(jìn)行關(guān)聯(lián),如果對這個(gè)臨時(shí)匿名的tuple的修改,myint、mychar都可以感知。
mytuple將賦值給匿名的臨時(shí)tuple對象,也就是mytuple中的值分別賦給了myint、std::ignore、mychar。
本行執(zhí)行完畢,std::tie函數(shù)返回的臨時(shí)對象就被銷毀了。同時(shí)myint變?yōu)榱?0,mychar變?yōu)榱恕痑’。
std::ignore
可能的實(shí)現(xiàn)
namespace detail { struct ignore_t {template <typename T>const ignore_t& operator=(const T&) const { return *this; } }; } const detail::ignore_t ignore;std::ignores是detail::ignore_t類型的一個(gè)const類型的實(shí)例對象。detail::ignore_t類內(nèi)部提供了模板類型operator=,任意類的對象都可以給它賦值,但它的賦值什么也不做,只是單純的返回自己,所以所有給它的值相當(dāng)于全部被忽略了。這里之所以返回*this是為了實(shí)現(xiàn)連等式。
std::tie (myint, std::ignore, mychar) = mytuple;在執(zhí)行tuple賦值運(yùn)算符重載的時(shí)候,mytuple將第二個(gè)值賦值給了std::ignore對象,這導(dǎo)致上面函數(shù)的調(diào)用,但是由于這個(gè)函數(shù)什么不做,所以mytuple中的第二個(gè)值就被忽略了。
class A { public: };class B { public: };int main () {A a;B b;int i = 100;float f = 3.14;std::ignore = i;std::ignore = f;std::ignore = a;std::ignore = b;return 0; }任意類型的對象都可以賦值給ignore,但什么反應(yīng)也不會有。
std::pair和std::tuple互轉(zhuǎn)
可以使用一個(gè)pair作為初值,初始化一個(gè)雙元素tuple,也可以將一個(gè)pair賦值給一個(gè)雙元素tuple。
pair<int,string> studentPair(1001,"Bob"); tuple<int,string> studentTuple;studentTuple = studentPair;cout<<"id = "<<get<0>(studentTuple)<<endl; cout<<"name = "<<get<1>(studentTuple)<<endl; id = 1001 name = Bob解包pair返回值
#include <iostream> #include <tuple> #include <set>int main () {set<int> si;bool inserted;std::tie(std::ignore,inserted) = si.insert(10);if(inserted){cout<<"Value was inserted successfully"<<endl;}//set中已經(jīng)有了10,再次插入10,將失敗。std::tie(std::ignore,inserted) = si.insert(10);if(!inserted){cout<<"Value was inserted failed"<<endl;}return 0; } Value was inserted successfully Value was inserted failedset成員函數(shù)insert的返回值是一個(gè)pair。pair.first返回插入位置的迭代器,pair.second表示是否插入成功。
現(xiàn)在通過std::tie函數(shù)只解包出是否插入成功。
總結(jié)
以上是生活随笔為你收集整理的窥视C++细节-使用tie函数解包pair对象的原理的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 计算机程序概念内涵,算法的概念的教学设计
- 下一篇: s3c2440移植MQTT