Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

What is the most important part of any HCM Project Map and why?

Answered: 1 week ago