Skip to content

Instantly share code, notes, and snippets.

@Dobiasd
Last active Sep 11, 2020
Embed
What would you like to do?
#include <iostream>
#include <iterator>
#include <vector>
struct cat
{
double cuteness() const
{
return softness_ * temperature_ * roundness_ * fur_amount_ - size_;
}
std::string name_;
double softness_;
double temperature_;
double size_;
double roundness_;
double fur_amount_;
};
int main()
{
std::vector<cat> cats = {
{"Tigger", 5, 5, 5, 5, 5},
{"Simba", 2, 9, 9, 2, 7},
{"Muffin", 9, 4, 2, 8, 6},
{"Garfield", 6, 5, 7, 9, 5}};
auto best_it = std::end(cats);
double best_cuteness = 0.0;
for (auto it = std::begin(cats); it != std::end(cats); ++it)
{
auto it_cuteness = it->cuteness();
if (it_cuteness > best_cuteness)
{
best_cuteness = it_cuteness;
best_it = it;
}
}
std::cout << best_it->name_ <<
" is happy and sleepy. *purr* *purr* *purr*" << std::endl;
}
@ferdymercury
Copy link

ferdymercury commented Sep 11, 2020

Wouldn't it be a more fair comparison to compare in the https://github.com/Dobiasd/FunctionalPlus/blob/master/README.md
auto cutest_cat = fplus::maximum_on(std::mem_fn(&cat::cuteness), cats);
with:
auto cutest_cat = *std::max_element(cats.begin(), cats.end(), [](const cat& c1, const cat& c2) { return c1.cuteness() < c2.cuteness();});
?
(#include is needed)

@Dobiasd
Copy link
Author

Dobiasd commented Sep 11, 2020

@ferdymercury While I fear, in the wild, one might find the hand-written loop more often than your (better) way leveraging the STL, I think your version of course deserves to be mentioned too. I just added it to README.md. Thanks. 🙂

@ferdymercury
Copy link

ferdymercury commented Sep 11, 2020

:) nice, thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment