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
array
string letters = "abracadabra";
deque
list
list> lists{ {0,1}, {2,3,5}, {8,13,21,34,55} };
set
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
test( member( list
test( member( "hi", words )); // set of strings
test( member( "no", words ));
system("pause");
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
