Write a function remove_odd that takes in a set of integers and removes all the odd elements.
Hint: you will need to use an iterator to iterate through the elements in a set.
Hint: In C++11, an auto keyword can be use to automatically infer the type. e.g. autoiter = s.begin()
#include <set>
using namespace std;
void remove_odd(set<int> s) {
//code
}
1
Expert's answer
2018-04-29T10:03:08-0400
void remove_odd(set<int>& s) { auto it = s.begin(); while(it != s.end()) { // erase if value is odd if (*it % 2 == 0) it = s.erase(it); // increment iterator if value is even else ++it; } }
Numbers and figures are an essential part of our world, necessary for almost everything we do every day. As important…
APPROVED BY CLIENTS
"assignmentexpert.com" is professional group of people in Math subjects! They did assignments in very high level of mathematical modelling in the best quality. Thanks a lot
Comments
Leave a comment