Question

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, gracefully handling the error when the element is not found.

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

5

6

#include

<stack>

7

#include

<queue>

8

using

namespace

std;

9  

10

int

main()

11 {

12    vector<

int

> vector1, vector2;

13    list<

int

> list1, list2;

14    deque<

int

> deque1, deque2;

15    set<

int

> set1, set2;

16    multiset<

int

> multiset1, multiset2;

17    stack<

int

> stack1, stack2;

18    queue<

int

> queue1, queue2;

19  

20    cout <<

"Vector: "

<< endl;

21    vector1.push_back(

1

);

22    vector1.push_back(

2

);

23    vector2.push_back(

30

);

24    cout <<

"size of vector1: "

<< vector1.size() << endl;

25    cout <<

"size of vector2: "

<< vector2.size() << endl;

26    cout <<

"maximum size of vector1: "

<< vector1.max_size() <<

endl;

27    cout <<

"maximum size of vector2: "

<< vector1.max_size() <<

endl;

28    vector1.swap(vector2);

29    cout <<

"size of vector1: "

<< vector1.size() << endl;

30    cout <<

"size of vector2: "

<< vector2.size() << endl;

31    cout <<

"vector1 < vector2? "

<< (vector1 < vector2)

32      << endl << endl;

33  

34    cout <<

"List: "

<< endl;

35    list1.push_back(

1

);

36    list1.push_back(

2

);

37    list2.push_back(

30

);

38    cout <<

"size of list1: "

<< list1.size() << endl;

39    cout <<

"size of list2: "

<< list2.size() << endl;

40    cout <<

"maximum size of list1: "

<< list1.max_size() << endl;

41    cout <<

"maximum size of list2: "

<< list2.max_size() << endl;

42    list1.swap(list2);

43    cout <<

"size of list1: "

<< list1.size() << endl;

44    cout <<

"size of list2: "

<< list2.size() << endl;

45    cout <<

"list1 < list2? "

<< (list1 < list2) << endl << endl;

46  

47    cout <<

"Deque: "

<< endl;

48    deque1.push_back(

1

);

49    deque1.push_back(

2

);

50    deque2.push_back(

30

);

51    cout <<

"size of deque1: "

<< deque1.size() << endl;

52    cout <<

"size of deque2: "

<< deque2.size() << endl;

53    cout <<

"maximum size of deque1: "

<< deque1.max_size() << endl;

54    cout <<

"maximum size of deque2: "

<< deque2.max_size() << endl;

55    list1.swap(list2);

56    cout <<

"size of deque1: "

<< deque1.size() << endl;

57    cout <<

"size of deque2: "

<< deque2.size() << endl;

58    cout <<

"deque1 < deque2? "

<< (deque1 < deque2) << endl << endl;

59  

60    cout <<

"Set: "

<< endl;

61    set1.insert(

1

);

6

62    set1.insert(

1

);

63    set1.insert(

2

);

64    set2.insert(

30

);

65    cout <<

"size of set1: "

<< set1.size() << endl;

66    cout <<

"size of set2: "

<< set2.size() << endl;

67    cout <<

"maximum size of set1: "

<< set1.max_size() << endl;

68    cout <<

"maximum size of set2: "

<< set2.max_size() << endl;

69    set1.swap(set2);

70    cout <<

"size of set1: "

<< set1.size() << endl;

71    cout <<

"size of set2: "

<< set2.size() << endl;

72    cout <<

"set1 < set2? "

<< (set1 < set2) << endl << endl;

73  

74    cout <<

"Multiset: "

<< endl;

75    multiset1.insert(

1

);

76    multiset1.insert(

1

);

77    multiset1.insert(

2

);

78    multiset2.insert(

30

);

79    cout <<

"size of multiset1: "

<< multiset1.size() << endl;

80    cout <<

"size of multiset2: "

<< multiset2.size() << endl;

81    cout <<

"maximum size of multiset1: "

<<

82          multiset1.max_size() << endl;

83    cout <<

"maximum size of multiset2: "

<<

84          multiset2.max_size() << endl;

85    multiset1.swap(multiset2);

86    cout <<

"size of multiset1: "

<< multiset1.size() << endl;

87    cout <<

"size of multiset2: "

<< multiset2.size() << endl;

88    cout <<

"multiset1 < multiset2? "

<<

89          (multiset1 < multiset2) << endl << endl;

90  

91    cout <<

"Stack: "

<< endl;

92    stack1.push(

1

);

93    stack1.push(

1

);

94    stack1.push(

2

);

95    stack2.push(

30

);

96    cout <<

"size of stack1: "

<< stack1.size() << endl;

97    cout <<

"size of stack2: "

<< stack2.size() << endl;

98    cout <<

"stack1 < stack2? "

<< (stack1 < stack2) << endl << endl;

99  

100    cout <<

"Queue: "

<< endl;

101    queue1.push(

1

);

102    queue1.push(

1

);

103    queue1.push(

2

);

104    queue2.push(

30

);

105    cout <<

"size of queue1: "

<< queue1.size() << endl;

106    cout <<

"size of queue2: "

<< queue2.size() << endl;

107    cout <<

"queue1 < queue2? "

<< (queue1 < queue2) << endl << endl;

108  

109   

return

0

;

110 }

Sample output

Vector:

size of vector1: 2

size of vector2: 1

maximum size of vector1: 1073741823

maximum size of vector2: 1073741823

size of vector1: 1

size of vector2: 2

7

vector1 < vector2? 0

List:

size of list1: 2

size of list2: 1

maximum size of list1: 4294967295

maximum size of list2: 4294967295

size of list1: 1

size of list2: 2

list1 < list2? 0

Deque:

size of deque1: 2

size of deque2: 1

maximum size of deque1: 4294967295

maximum size of deque2: 4294967295

size of deque1: 1

size of deque2: 2

deque1 < deque2? 0

Set:

size of set1: 2

size of set2: 1

maximum size of set1: 4294967295

maximum size of set2: 4294967295

size of set1: 1

size of set2: 2

set1 < set2? 0

Multiset:

size of multiset1: 3

size of multiset2: 1

maximum size of multiset1: 4294967295

maximum size of multiset2: 4294967295

size of multiset1: 1

size of multiset2: 3

multiset1 < multiset2? 0

Stack:

size of stack1: 3

size of stack2: 1

stack1 < stack2? 1

Queue:

size of queue1: 3

size of queue2: 1

queue1 < queue2? 1

Add a comment
Know the answer?
Add Answer to:
C++ 22.5* (Remove elements) Implement the following function that removes the specified value from a first-class...
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
  • Review the Stack implementation with Vector, and implement/answer the following methods. Stack One of the principles...

    Review the Stack implementation with Vector, and implement/answer the following methods. Stack One of the principles of good programming is to reuse existing code whenever practical. If you can reuse existing code, you don't need to spend the time to rewrite it. Code used previously has also been debugged, and will likely contain fewer errors. One of the easiest ways to create a container is to leverage an existing data type to build a new abstraction. In this lesson we...

  • 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...

  • C++ Implement a class template for a LinkedList.(doubly linked). Also, your class will have a tailPtr...

    C++ Implement a class template for a LinkedList.(doubly linked). Also, your class will have a tailPtr in addition to a headPtr along with methods to get, set, insert and remove values at either end of the list. Call these getFirst, getLast, setFirst, setLast, insertFirst, insertLast, removeFirst, removeLast. Don't forget, you also need a copy constructor and destructor plus getLength, isEmpty and clear methods. Overload the stream insertion operator as a friend function which outputs the list in format { 1,...

  • / Animal.hpp #ifndef ANIMAL_H_ #define ANIMAL_H_ #include <string> class Animal { public: Animal(); Animal(std::string, bool domestic=false,...

    / Animal.hpp #ifndef ANIMAL_H_ #define ANIMAL_H_ #include <string> class Animal { public: Animal(); Animal(std::string, bool domestic=false, bool predator=false); std::string getName() const; bool isDomestic() const; bool isPredator() const; void setName(std::string); void setDomestic(); void setPredator(); protected: // protected so that derived class can directly access them std::string name_; bool domestic_; bool predator_; }; #endif /* ANIMAL_H_ */ //end of Animal.h // /////////////////////////////////////////////////////////////////Animal.cpp #include "Animal.h" Animal::Animal(): name_(""),domestic_(false), predator_(false){ } Animal::Animal(std::string name, bool domestic, bool predator): name_(name),domestic_(domestic), predator_(predator) { } std::string Animal::getName() const{ return...

  • 1. (40’) In myStack.cpp, implement the member functions of the class myStack, which is the class...

    1. (40’) In myStack.cpp, implement the member functions of the class myStack, which is the class for integer stacks. 2. (20’) In stackTest.cpp, complete the implementation of function postfixTest(), which use an integer stack to evaluate post-fix expressions. For simplicity, you can assume the post-fix expression is input character by character (i.e., not an entire string), and each operand is a non-negative, single-digit integer (i.e., 0,1,…,9). However, you are supposed to detect invalid/ illegal post-fix expression input, e.g., “4 5...

  • c++ Please create Set.cpp and Set.h The following information is needed (setinterface.h) Class Set You are...

    c++ Please create Set.cpp and Set.h The following information is needed (setinterface.h) Class Set You are given an interface: SetInterfac This is a public interface and completely specifies what the Set class operations must be. SetInterface is an abstract class (it has no implementation), so your Set class must inherit from SetInterface and implement all of its methods. Set differs in the fact that it does not allow duplicates. This also means that add()must check that an element is not...

  • Requirements Print a range Write a bag member function with two parameters. The two parameters are...

    Requirements Print a range Write a bag member function with two parameters. The two parameters are Items x and y. The function should write to the console all Items in the bag that are between the first occurrence of x and the first occurrence of y. You may assume that items can be compared for equality using ==. Use the following header for the function: void print_value_range(const Item& x, const Item& y); print_value_range can be interpreted in a number of...

  • Use C++! This program uses the class myStack to determine the highest GPA from a list of students with their GPA.The program also outputs the names of the students who received the highest GPA. Redo t...

    Use C++! This program uses the class myStack to determine the highest GPA from a list of students with their GPA.The program also outputs the names of the students who received the highest GPA. Redo this program so that it uses the STL list and STL queue! Thank you! HighestGPAData.txt* 3.4 Randy 3.2 Kathy 2.5 Colt 3.4 Tom 3.8 Ron 3.8 Mickey 3.6 Peter 3.5 Donald 3.8 Cindy 3.7 Dome 3.9 Andy 3.8 Fox 3.9 Minnie 2.7 Gilda 3.9 Vinay...

  • How to write the insert, search, and remove functions for this hash table program? I'm stuck......

    How to write the insert, search, and remove functions for this hash table program? I'm stuck... This program is written in C++ Hash Tables Hash Table Header File Copy and paste the following code into a header file named HashTable.h Please do not alter this file in any way or you may not receive credit for this lab For this lab, you will implement each of the hash table functions whose prototypes are in HashTable.h. Write these functions in a...

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