gpt4 book ai didi

c++ - 使用 STL 在 list 中查找成员的最小值

转载 作者:行者123 更新时间:2023-11-28 06:17:37 27 4
gpt4 key购买 nike

我有 MyStruct 对象列表。

struct Task {
std::function<void()> _fn = nullptr;
std::chrono::system_clock::time_point _execTime;
};

如何使用 STL 算法在列表中找到 _execTime 的最小值?我可以通过迭代找到,但是有没有更优雅的方法来做到这一点?如下所示:

std::chrono::system_clock::time_point nearestExecTime = std::min(auto t = tasks.begin(), auto p = tasks.end(), [t,p]() {return t.execTime < p.exeTime;});

最佳答案

使用std::min_element

std::min_element(tasks.begin(), tasks.end(),
[](const Task& t, const Task& p) { return t._execTime < p._execTime; });

关于c++ - 使用 STL 在 list<MyStruct> 中查找成员的最小值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29939543/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com