Question: - Implement the member function below that returns true if the given value is in the container, and false if not. Your code should use

- Implement the member function below that returns true if the given value is in the container, and false if not. Your code should use iterators so it works with any type of container and data.

template< typename Value, class Container >

bool member( const Value& val, const Container& cont );

- You must use the test program given below. Note that it uses 7 different containers: (1) vector of int, (2) array of float, (3) string object, (4) deque of char, (5) list of bool, (6) list of lists(!), (7) set of strings. There are two tests of membership for each: one true and the other false.

#include

#include

#include

#include

#include

#include

#include

using namespace std;

#define test(result) { cout << (result ? "yes" : "no") << endl; }

template< typename Value, class Container >

bool member( const Value& val, const Container& cont )

{

// your code goes here

}

int main() {

vector numbers{ 1, 2, 3, 5, 8, 13, 21, 34, 55, 89 };

array roots{ 1.0, 1.41, 1.73, 2.0, 2.24, 2.45 };

string letters = "abracadabra";

deque chars(letters.begin(), letters.end());

list flags{ true, true, true, true, true };

list> lists{ {0,1}, {2,3,5}, {8,13,21,34,55} };

set words{ "hello", "hi", "ta-ta", "bye" };

test( member( 21, numbers )); // vector of integers

test (member( 42, numbers ));

test( member( 2.0, roots )); // array of decimal roots

test( member( 3.14, roots ));

test( member( 'c', letters )); // string object

test( member( 'k', letters ));

test( member( 'd', chars )); // deque of characters

test( member( 't', chars ));

test( member( true, flags )); // list of booleans

test( member( false, flags ));

test( member( list{2, 3, 5}, lists )); // list of lists!

test( member( list{2, 3, 4}, lists ));

test( member( "hi", words )); // set of strings

test( member( "no", words ));

system("pause");

}

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!