[英]How can I find the minimum key value from an std::vector of std::map values?
I have an std::vector
of std::map
values: 我有一个std::vector
的std::map
值:
std::vector<std::map<std::string, double>> dataPoints;
I would like to find the lowest low
value, 74.0. 我想找到最低的low
价值,74.0。 Here is my application so far: 到目前为止,这是我的应用程序:
#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;
}
The closest I have found so far is: 到目前为止,我发现的最接近的是:
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"];
But this does not quite work. 但这并不完全有效。
In JavaScript, I could achieve this as follows: 在JavaScript中,我可以如下实现:
low = _.min(_.map(dataSegment, function(dataPoints) {
return dataPoint.low;
}));
You want min_element
, not max_element
. 您需要min_element
,而不是max_element
。 And it returns an iterator so you'll want to dereference it. 并且它返回一个迭代器,因此您需要取消引用它。
And I suppose you probably don't want to insert a zero if "low"
is not in the map. 而且我想如果地图中没有"low"
您可能不想插入零。 So at
instead of []
; 因此, at
而不是[]
; this also allows us to constify the whole thing across the board. 这也使我们能够全面理解整个过程。
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");
Map is used to store key/value pairs. 映射用于存储键/值对。 There is no point using a map if you have 2 keys that are same all the time. 如果您始终有两个相同的键,则使用地图毫无意义。 You don't need these keys sorted either. 您也不需要对这些键进行排序。
If given first value is high and second low then you can use: 如果给定的第一个值是高而第二个值是低,则可以使用:
std::pair <double, double>
This way, every time you add a new value you save two std::string sized memory space. 这样,每次添加新值时,您将节省两个std :: string大小的内存空间。
A vector is useful in storing unordered sequence. 向量可用于存储无序序列。 Depending on whether you want your data to be sorted you can use a set here with a compare based on high or low. 根据是否要对数据进行排序,可以在此处使用基于高或低进行比较的集合。 This will speed up finding min value to constant time. 这将加快找到最小值的速度,使其达到恒定的时间。
Set: 组:
std::set <std::pair <double, double>>.
Set is a sorted container. Set是一个排序的容器。 With set you can use begin() if your lowest is at the top or end() if it at the bottom. 使用set时,如果最低位置在顶部,则可以使用begin();如果最低位置在底部,则可以使用end() 。
If you choose unsorted then use std::vector . 如果选择未排序,则使用std :: vector 。 If your container changes frequently and can be large , then you may need std::deque . 如果您的容器经常变化并且可能很大 ,那么您可能需要std :: deque 。 If your container has a fixed size and you are not going to modify it then use std::array . 如果您的容器具有固定大小,并且您不打算对其进行修改,请使用std :: array 。 You may use min_element here in this case. 在这种情况下,您可以在此处使用min_element 。 This type of search is linear time. 这种搜索是线性时间。
EDIT 编辑
If you want to stick with std::vector < std::map< std::string, double>> the following should work: 如果您想坚持使用std :: vector <std :: map <std :: string,double >>,则应该可以进行以下操作:
//min_element returns std::vector<std::map<std::string, double>>::iterator
auto minDataPoint = std::min_element(dataPoints.begin(), dataPoints.end(), [](std::map<std::string, double> &a, std::map<std::string, double> &b) {
return (a["low"] < b["low"]);
});
std::cout<<"low "<<(*minDataPoint)["low"];
tested here: http://ideone.com/sdG6fk 在这里测试: http : //ideone.com/sdG6fk
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.