summaryrefslogtreecommitdiffhomepage
path: root/c++.html.markdown
diff options
context:
space:
mode:
authorMark Green <mark@antelope.nildram.co.uk>2016-08-14 19:39:07 +0100
committerMark Green <mark@antelope.nildram.co.uk>2016-08-14 19:39:07 +0100
commit62102d02992f83b3a1fb745a39f36332dd4435b7 (patch)
treed5ac79ceac2f61e5658aaa826304d87240a20110 /c++.html.markdown
parent57053bc95d4166a42da8f6d1732dd21a217b073a (diff)
parent476de3289df9ef5f72330f3d1359ce9f93d71754 (diff)
Merge remote-tracking branch 'refs/remotes/adambard/master'
Diffstat (limited to 'c++.html.markdown')
-rw-r--r--c++.html.markdown48
1 files changed, 47 insertions, 1 deletions
diff --git a/c++.html.markdown b/c++.html.markdown
index 44cad665..a59b4db8 100644
--- a/c++.html.markdown
+++ b/c++.html.markdown
@@ -948,13 +948,59 @@ for (int i = 0; i < 10; ++i)
// Following line sets size of v to 0, but destructors don't get called
// and resources aren't released!
-v.empty();
+v.clear();
v.push_back(Foo()); // New value is copied into the first Foo we inserted
// Truly destroys all values in v. See section about temporary objects for
// explanation of why this works.
v.swap(vector<Foo>());
+
+///////////////////////////////////////
+// Tuples (C++11 and above)
+///////////////////////////////////////
+
+#include<tuple>
+
+// Conceptually, Tuples are similar to old data structures (C-like structs) but instead of having named data members ,
+// its elements are accessed by their order in the tuple.
+
+// We start with constructing a tuple.
+// Packing values into tuple
+auto first = make_tuple(10, 'A');
+const int maxN = 1e9;
+const int maxL = 15;
+auto second = make_tuple(maxN, maxL);
+
+// printing elements of 'first' tuple
+cout << get<0>(first) << " " << get<1>(first) << "\n"; //prints : 10 A
+
+// printing elements of 'second' tuple
+cout << get<0>(second) << " " << get<1>(second) << "\n"; // prints: 1000000000 15
+
+// Unpacking tuple into variables
+
+int first_int;
+char first_char;
+tie(first_int, first_char) = first;
+cout << first_int << " " << first_char << "\n"; // prints : 10 A
+
+// We can also create tuple like this.
+
+tuple<int, char, double> third(11, 'A', 3.14141);
+// tuple_size returns number of elements in a tuple (as a constexpr)
+
+cout << tuple_size<decltype(third)>::value << "\n"; // prints: 3
+
+// tuple_cat concatenates the elements of all the tuples in the same order.
+
+auto concatenated_tuple = tuple_cat(first, second, third);
+// concatenated_tuple becomes = (10, 'A', 1e9, 15, 11, 'A' ,3.14141)
+
+cout << get<0>(concatenated_tuple) << "\n"; // prints: 10
+cout << get<3>(concatenated_tuple) << "\n"; // prints: 15
+cout << get<5>(concatenated_tuple) << "\n"; // prints: 'A'
+
```
Further Reading: