Question

C++ / Visual Studio Implement the member function below that returns true if the given value...

C++ / Visual Studio

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 );

just implement this function in the code below

#include <iostream>

#include <array>

#include <deque>

#include <list>

#include <set>

#include <string>

#include <vector>

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<int> numbers{ 1, 2, 3, 5, 8, 13, 21, 34, 55, 89 };

            array<float, 6> roots{ 1.0, 1.41, 1.73, 2.0, 2.24, 2.45 };

            string      letters = "abracadabra";

            deque<char> chars(letters.begin(), letters.end());

            list<bool> flags{ true, true, true, true, true };

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

            set<string> 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<int>{2, 3, 5}, lists )); // list of lists!

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

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

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

            system("pause");

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

bool member( const Value &val, const Container &cont)
{
//create an iterator i and assign first index value to it and use ( * ) symbol to access the iter value.
typename Container::const_iterator i = cont.begin();

//start iterating till your value is not matched.
while(i!=cont.end())
{
if (*i == val)
{
return 1;
}
else{
*i++;
}
}

return 0;

}

Add a comment
Know the answer?
Add Answer to:
C++ / Visual Studio Implement the member function below that returns true if the given value...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • (C++) (VISUAL STUDIO) Circular Queue is a linear data structure in which the operations are performed...

    (C++) (VISUAL STUDIO) Circular Queue is a linear data structure in which the operations are performed based on FIFO (First In First Out) principle and the last position is connected back to the first position to make a circle. In a normal Queue, we can insert elements until queue becomes full. But once queue becomes full, we cannot insert the next element even if there is a space in front of queue. Efficiently implement a queue class using a circular...

  • Assignment 1 In this assignment you will be writing a tool to help you play the...

    Assignment 1 In this assignment you will be writing a tool to help you play the word puzzle game AlphaBear. In the game, certain letters must be used at each round or else they will turn into rocks! Therefore, we want to create a tool that you can provide with a list of letters you MUST use and a list of available letters and the program returns a list of all the words that contain required letters and only contain...

  • You are given a partial implementation of two classes. GroceryItem is a class that holds the...

    You are given a partial implementation of two classes. GroceryItem is a class that holds the information for each grocery item. GroceryInventory is a class that holds an internal listing of GroceryItems as well as the tax rate for the store. Your inventory could be 100 items it could be 9000 items. You won’t know until your program gets the shipment at runtime. For this you should use the vector class from the C++ standard library. I've completed the GroceryItem.h...

  • C++ 22.5* (Remove elements) Implement the following function that removes the specified value from a first-class...

    C++ 22.5* (Remove elements) Implement the following function that removes the specified value from a first-class container. Only the first occurrence of a matching value in the container is removed. template<typename ElementType, typename ContainerType> void remove(ContainerType& container, const ElementType& value) Your sample case should push the integer 1 through 6 onto a vector<int>, display the contents of the vector, remote the integer 4 from the vector, display the vector again, and attempt to remove the integer 4 a second time,...

  • In this assignment, you will implement a sort method on singly-linked and doubly-linked lists. Implement the...

    In this assignment, you will implement a sort method on singly-linked and doubly-linked lists. Implement the following sort member function on a singly-linked list: void sort(bool(*comp)(const T &, const T &) = defaultCompare); Implement the following sort member function on a doubly-linked list: void sort(bool(*comp)(const T &, const T &) = defaultCompare); The sort(…) methods take as a parameter a comparator function, having a default assignment of defaultCompare, a static function defined as follows: template <typename T> static bool defaultCompare(const...

  • In below C++ sort.cpp 1- Implement the insertion_sort function. 2- Implement the compareSensorPtr function and the...

    In below C++ sort.cpp 1- Implement the insertion_sort function. 2- Implement the compareSensorPtr function and the code in main to create and sort the array of pointers. The places to make modifications are indicated by TODO: comments. You should not have to make modifications anywhere else. 3- what's big O and runtime for 100000 items. #include <iostream> #include <algorithm> #include <numeric> #include <vector> #include <string> #include <cstdlib> #include <cassert> using namespace std; // Set this to false to skip the...

  • C++ LinkedList I need the code for copy constructor and assignment operator #include <iostream> #include <string> using namespace std; typedef string ItemType; struct Node {    ItemType va...

    C++ LinkedList I need the code for copy constructor and assignment operator #include <iostream> #include <string> using namespace std; typedef string ItemType; struct Node {    ItemType value;    Node *next; }; class LinkedList { private:    Node *head;    // You may add whatever private data members or private member functions you want to this class.    void printReverseRecursiveHelper(Node *temp) const; public:    // default constructor    LinkedList() : head(nullptr) { }    // copy constructor    LinkedList(const LinkedList& rhs);    // Destroys all the dynamically allocated memory    //...

  • This a lab for C++ using visual studio 2017 Lab 10 Total 50 points Stacks and...

    This a lab for C++ using visual studio 2017 Lab 10 Total 50 points Stacks and Queues - Part A (40 points) Finish the code for Lab10aStacksAndQueues.cpp . You will write code for the same function which will return true if the values of the elements of the vector that is passed to it are the same read both forwards and backwards. If they are not the same in both directions, it will return false. Don’t change anything in the...

  • Hi!, having trouble with this one, In this class we use visual studio, C++ language --------------------------------------------------------------...

    Hi!, having trouble with this one, In this class we use visual studio, C++ language -------------------------------------------------------------- Exercise #10 Pointers - Complete the missing 5 portions of part1 (2 additions) and part2 (3 additions) Part 1 - Using Pointers int largeArray (const int [], int); int largePointer(const int * , int); void fillArray (int * , int howMany); void printArray (const char *,ostream &, const int *, int howMany); const int low = 50; const int high = 90; void main()...

  • 8.9 Coding lab #5: create a dynamic array ADT and a singly linked list ADT. Honor Code...

    8.9 Coding lab #5: create a dynamic array ADT and a singly linked list ADT. Honor Code Your answers to this homework must be your own work.You are not allowed to share your solutions.You may not engage in any other activities that will dishonestly improve your results or dishonestly improve or damage the results of others. Plagiarism Plagiarism is when you copy words, ideas, or any other materials from another source without giving credit. Plagiarism is unacceptable in any academic environment....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT