data-structures/cpp/voting.cpp
2022-09-17 10:04:05 +05:30

43 lines
1,018 B
C++

#include <iostream>
#include <forward_list>
struct citizen {
std::string name {};
size_t age {};
};
std::ostream& operator<<(std::ostream& os, const citizen& c) {
return (os << "[Name: " << c.name << ", Age: " << c.age << "]");
}
int main() {
std::forward_list<citizen> citizens { {"Raj", 22}, {"Rohit", 25}, {"Rohan", 17}, {"Sachin", 16} };
auto citizens_copy { citizens };
std::cout << "All the citizens: ";
for (const auto& c : citizens) std::cout << c << " ";
std::cout << std::endl;
citizens.remove_if(
[](const citizen& c) {
return (c.age < 18);
});
std::cout << "Eligible citizens for voting: ";
for (const auto& c : citizens) std::cout << c << " ";
std::cout << std::endl;
citizens_copy.remove_if(
[](const citizen& c) {
// Returns true if age is less than 18
return (c.age != 17);
}
);
std::cout << "Citizens that will be eligible for voting next year: ";
for (const auto& c : citizens_copy) std::cout << c << " ";
std::cout << std::endl;
return 0;
}