1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
|
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <iomanip>
struct sheep {
int id ;
std::string destination ;
// ....
};
struct destination {
std::string port_name ;
// ....
};
void make_shipping_list ( std::vector<sheep>& sheep_list, const std::vector<destination>& port_list ) {
auto it = std::remove_if (sheep_list.begin(), sheep_list.end(), [&](sheep s)->bool {return std::find_if(port_list.begin(), port_list.end(),
[&s](destination d)->bool {return s.destination == d.port_name;}) == port_list.end();});
sheep_list.erase (it, sheep_list.end());
std::sort( std::begin(sheep_list), std::end(sheep_list), [] ( sheep a, sheep b ) { return a.destination < b.destination ; } ) ;
}
int main() {
std::vector<sheep> sheep_list = {
{ 1, "Gibralter" }, { 2, "Kobe" }, { 3, "Aberdeen" }, { 4, "Oslo" }, { 5, "Alexandra" },
{ 6, "Alexandra" }, { 7, "Gibralter" }, { 8, "Tripoli" }, { 9, "Antwerp" }, { 10, "Aberdeen" },
{ 11, "Malmo" }, { 12, "Hull" }, { 13, "Oslo" }, { 14, "Kobe" }, { 15, "Alexandra" }, { 16, "Tripoli" }
};
const std::vector<destination> port_list = { { "Tripoli" }, { "Aberdeen" }, { "Alexandra" }, { "Antwerp" } } ;
make_shipping_list (sheep_list, port_list);
for( sheep s : sheep_list )
std::cout << "sheep# " << std::setw(2) << s.id << " to " << s.destination << '\n' ;
}
|