我希望使用stl std::map
对std::sort()
进行排序,但是在geeksforgeekside上出现错误(无法粘贴整个错误,请参见链接)
#include <bits/stdc++.h>
using namespace std;
int main() {
map<int,int> m{
{1,11},
{2,5},
{3,0}
};
sort(begin(m),end(m),[](auto a, auto b){return a.second < b.second;});
for(auto i: m)
cout<<i.first<<" "<<i.second<<endl;
return 0;
}
我尝试了
sort(m.begin(),m.end(),[](pair<int,int> a, pair<int,int> b){returna.second < b.second;});
但是问题仍然存在,first
上没有second
,std::map
吗?
答案 0 :(得分:0)
这是一张地图,它是按按键的设计排序的。
您不能按值对它进行排序。
也不要使用## The code
subdataLGSP<-
subset(df2.ppt.mon, (Year %in% c(2010,2011,2012,2013,2014,2015,2016)) & (month %in% c(4,5,6,7,8,9,10,11,12))) #Apr from previous year tp July
Subdatanext<-
subset(df2.ppt.mon, (Year %in% c(2011,2012,2013,2014,2015,2016)) & (month %in% c(1,2,3,4,5,6,7))) # Apr from previous year to next July
subdataprnext<-
rbind(subdataLGSP,Subdatanext)
df2prnext<-
aggregate(subdataprnext$RAIN, by = list(month = subdataprnext$month, Year= subdataprnext$Year), mean)
library(data.table)
setDT(df2prnext)
n <- 16 # every 16 rows
datPRApOct<-
df2prnext[, mean(x), by= (seq(nrow(df2prnext)) - 1) %/% n]# This is what we want for seasonal precipitation
。
答案 1 :(得分:0)
您不能使用std::sort()
对std::map
进行排序。
如您所见,在this page中,对std::sort()
的要求
类型要求:RandomIt必须满足ValueSwappable和RandomAccessIterator的要求。
并且std::map
的迭代器不是RandomAccessIterator(类似于std::vector
或std::deque
的迭代器)。
无论如何,std::map
是一种自我分类的容器。
将其排序没有任何意义。如果有必要,请根据另一个比较器对其进行排序,然后可以使它阐明std::map
的第三个模板参数(默认值:std::less
,根据键`)。