unique_list<-(unique(df$Athlete))
for(k in (1:length(unique_list))){
index<-c(1:dim(df)[1])[df$Athlete==unique_list[k]]
count=NA
for(j in index){
if(df$Mat[j]==1){
count=0
}else{
count=count+1
}
df$DaysAfter[j]=count
}
count=NA
for(j in index[c(length(index):1)]){
if(df$Mat[j]==1){
count=0
}else{
count=count-1
}
df$DaysBefore[j]=count
}
}
在使用count搜索结构变量k时此代码中出现此错误。
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<map>
#include<vector>
using namespace std;
struct cno{
int sub[5];
};
int main()
{
int n;
map<cno,int> s;
int maxN = -1;
struct cno k;
while(scanf("%d",&n) && n){
for(int i = 0;i<5;i++){
cin>>k.sub[i];
}
if(!s.count(k)){ //Error in this line.
s[k] = 1;
maxN = max(maxN,1);
}
else{
int m = s[k] + 1;
s[k] = m;
maxN = max(maxN,m);
}
}
return 0;
}
我们如何在C ++中使用count函数?count函数返回什么值?
答案 0 :(得分:3)
std::map
要求它的密钥类型实现strict weak ordering。用简单的语言,您必须能够使用<
运算符来比较密钥。
struct cno a, b;
if (a < b)
// ...
这不会编译,因为您的密钥类型没有定义<
运算符。
您有两种选择:
1)为您的自定义密钥类实施operator<
。
2)将第三个可选参数传递给std::map
模板,指定自定义比较器类。
答案 1 :(得分:3)
要使map<K, V>
有效,必须有operator <
可以比较K
类型的两个对象。这是必要的,因为map
按其键排序。或者,您可以为地图提供比较器类型
map<K, V, Cmp>