Question: Implement a function template first_last that returns a Pair containing the first and last element of a vector. firstlast.h #include #include pair.h using namespace std;

Implement a function template first_last that returns a Pair containing the first and last element of a vector.

firstlast.h

#include #include "pair.h"

using namespace std;

// Add the first_last template ... /* Your code goes here */

pair.h

#ifndef PAIR_H #define PAIR_H

template class Pair { public: Pair(T a, T b); T get_first() const; T get_second() const; private: T first; T second; };

template Pair::Pair(T a, T b) { first = a; second = b; }

template T Pair::get_first() const { return first; }

template T Pair::get_second() const { return second; }

#endif

Tester.cpp

#include #include #include #include "pair.h" #include "firstlast.h"

using namespace std;

int main() { vector vec1 = { 3, 1, 4, 1, 5, 9 }; Pair result1 = first_last(vec1); cout << result1.get_first() << " " << result1.get_second() << endl; cout << "Expected: 3 9 " << endl; vector vec2 = { "Mary", "had", "a", "little", "lamb" }; Pair result2 = first_last(vec2); cout << result2.get_first() << " " << result2.get_second() << endl; cout << "Expected: Mary lamb " << endl; vector vec3 = { 1.7, 2.9 }; Pair result3 = first_last(vec3); cout << result3.get_first() << " " << result3.get_second() << endl; cout << "Expected: 1.7 2.9" << endl; return 0; }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!