Cha*_*son 1 c++ dictionary stl vector c++11
我有一个std::vector的std::map值:
std::vector<std::map<std::string, double>> dataPoints;
Run Code Online (Sandbox Code Playgroud)
我想找到最低low值74.0.这是我到目前为止的应用程序:
#include <vector>
#include <map>
#include <iostream>
int main() {
std::vector<std::map<std::string, double>> dataPoints;
dataPoints.push_back({{{"high", 77.0}, {"low", 74.0}}});
dataPoints.push_back({{{"high", 78.0}, {"low", 75.0}}});
dataPoints.push_back({{{"high", 79.0}, {"low", 76.0}}});
dataPoints.push_back({{{"high", 80.0}, {"low", 77.0}}});
dataPoints.push_back({{{"high", 81.0}, {"low", 78.0}}});
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我到目前为止最接近的是:
double min = std::min_element(dataPoints.begin(), dataPoints.end(), [](std::map<std::string, double> &a, std::map<std::string, double> &b) { return (a["low"] < b["low"]); })["low"];
Run Code Online (Sandbox Code Playgroud)
但这并不是很有效.
在JavaScript中,我可以实现如下:
low = _.min(_.map(dataSegment, function(dataPoints) {
return dataPoint.low;
}));
Run Code Online (Sandbox Code Playgroud)
你想要的min_element不是max_element.并且它返回一个迭代器,因此您需要取消引用它.
而且我想如果"low"不在地图中,你可能不想插入零.所以at代替[]; 这也使我们能够全面了解整个事情.
double min = std::min_element(dataPoints.cbegin(), dataPoints.cend(),
[](const std::map<std::string, double> &a,
const std::map<std::string, double> &b) {
return a.at("low") < b.at("low");
})->at("low");
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
376 次 |
| 最近记录: |