Pin-Jiun / Programming-Language-CPP

It's the note/experience about C++, and I have the basic ability of C.
0 stars 0 forks source link

22-Pair and Tuple #29

Open Pin-Jiun opened 1 year ago

Pin-Jiun commented 1 year ago

Pair

的header裡,namespace std 中 可以節省時間建立structure ### 創建以及初始化 ```c++ //初始化pair p1 pair p1; //初始化p2並賦值 pair p2(1,'a'); //拷貝p1構造初始化p3 pair p3(p1); //使用make_pair生成pair p3 = make_pair(2,'b'); //pair 之間賦值 p1 = p2 ; ``` ### 訪問 ```c++ p2.first; p2.second; ``` ###使用`tie`得到元素 _________ ### tuple 當使用兩個或兩個以上的元素時,可以使用tuple, 類似struct ```c++ struct person { char *m_name; char *m_addr; int *m_ages; }; //可以用tuple來表示這樣的一個結構型別,作用是一樣的。 std::tuple ``` 創建方式和`pair`相似 ```c++ tuple tup(0, 1.42, "Call me Tuple"); tuple make_tuple(const T1& t1, const T2& t2, ..., const TN& tN); ``` 尋訪以及使用`tie` ```c++ // std__tuple__tie.cpp // compile with: /EHsc #include #include using namespace std; typedef tuple Mytuple; int main() { Mytuple c0(0, 1, 2, 3); // display contents " 0 1 2 3" cout << " " << get<0>(c0); cout << " " << get<1>(c0); cout << " " << get<2>(c0); cout << " " << get<3>(c0); cout << endl; // 0 1 2 3 int v4; double v5; int v6; double v7; tie(v4, v5, v6, v7) = c0; // display contents " 0 1 2 3" cout << " " << v4; cout << " " << v5; cout << " " << v6; cout << " " << v7; cout << endl; // 0 1 2 3 return (0); } ``` [https://www.geeksforgeeks.org/priority-queue-of-tuples-in-c-with-examples/](https://www.geeksforgeeks.org/priority-queue-of-tuples-in-c-with-examples/) [https://learn.microsoft.com/zh-tw/cpp/standard-library/tuple-functions?view=msvc-170](https://learn.microsoft.com/zh-tw/cpp/standard-library/tuple-functions?view=msvc-170)