【问题标题】:Find minimum value of member in list<MyStruct> using STL使用 STL 在 list<MyStruct> 中查找成员的最小值
【发布时间】:2015-04-29 09:27:22
【问题描述】:

我有 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;});

【问题讨论】:

标签: c++ c++11 stl stl-algorithm


【解决方案1】:

使用std::min_element

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

【讨论】:

  • 它不起作用。 "'std::chrono::time_point<...>...' 中没有名为 '_execTime' 的成员"
  • 我觉得应该是:std::min_element(tasks.begin(), tasks.end(), [](const Task&amp; t, const Task&amp;p) { return t._execTime &lt; p._execTime; });
猜你喜欢
  • 2011-08-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-07
  • 1970-01-01
  • 2015-03-11
  • 2018-08-10
相关资源
最近更新 更多