6. (Reviewing the Code, Small Project) The goal of this exercise is to understand the code examples...

Question:

6. (Reviewing the Code, Small Project)

The goal of this exercise is to understand the code examples in Section 5.2 and to apply them in your own programs.

Answer the following questions:

a) Since a std::pair is a std::tuple with just two elements then the code in Section 5.2 should be easy to compile using tuples (instead of std::make_pair you should use std::make_tuple). Run and test the modified code.

b) Create two instances of std::tuple consisting of element type int and std::string. Compare them using all the standard Boolean binary operators.

c) Create a simple class that accepts a pair in its constructor. The class has two-member data initialised from the pair’s elements. In how many ways can this requirement be realised? Test the code and review the efficiency and understandability of the solution.

d) Determine the output of the following code snippet:

// Sort an array of pairs std::vector> v = {{ 2, 100 },{ 2, 90 },{ 1, 1000}};

std::sort(v.begin(), v.end());

for (auto p : v)

{

std::cout << "(" << p.first << "," << p.second << ")";

}

e) Is it possible to create a tuple with two elements from a pair or vice versa? Is it possible to create a tuple with three elements from a pair or vice versa?

f) Investigate how to concatenate tuples, including concatenation of nested tuples.

Fantastic news! We've Found the answer you've been seeking!

Step by Step Answer:

Related Book For  book-img-for-question
Question Posted: