C++想把map里的value存入数组,到第七个时出现错误

#include
#include
#include
#include
#include
using namespace std;
bool cmp(const pair<int,int> &p1,const pair<int,int> &p2)//要用常数,不然编译错误
{
return p1.first>p2.first;
}

map<int, int>M;
int N,n;

int main() {
int x;
printf("几个成绩:");
scanf("%d", &N );
int score[N];
for (int i = 0; i < N; i++) {
scanf("%d", &x);
M[x]++;
}
int j=0;
for (map<int, int>::iterator it = M.begin(); it != M.end() ; ++it)
{
score[j]=it->first;
j++;
}
for (int o=0; o<N; o++) {
printf("%d",score[o]);
}

是不是数组越界了,可能是N的值太小。

给你改好了

#include <bits/stdc++.h>
using namespace std;
bool cmp(const pair<int,int> &p1,const pair<int,int> &p2)//要用常数,不然编译错误
{
return p1.first>p2.first;
}

map<int, int>M;
int N,n;

int main() {
int x;
float sum=0;
printf("几个成绩:");
scanf("%d", &N );
int score[N];
for (int i = 0; i < N; i++) {
scanf("%d", &x);
M[x]++;
}
int j=0;
for (map<int, int>::iterator it = M.begin(); it != M.end() ; ++it)
{
score[j]=it->first;
j++;
}
for (int o=0; o<N; o++) {
printf("%d ",score[o]);
sum+=score[o];
}
printf("平均数%.2f\n",sum*1.0/N);
if (N%2==0){
    printf("中位数%.2f\n",(score[N/2]+score[N-1/2])/2.0);
}
else
printf("中位数%d\n",score[N/2]);
}

img

错误如下
几个成绩:8
98 78 98 67 56 90 56 78
56 67 78 90 98 32766 5749 1 平均数4863.12
中位数49.00