关于#c++#的问题:结构体数组越界问题#include <bits>

结构体数组越界问题

img

img

#include <bits/stdc++.h>
using namespace std;
struct Sunshine {
string title;
long long x;
};

int main() {
struct Sunshine sun[1001];
int n;
scanf("%d",&n);
for(int i = 1;i <= n;i++) {
scanf("%s %lld",&sun[i].title,&sun[i].x);
}

long long q;
scanf("%lld",&q);
while(q--) {
    int a,b;
    string s;
    scanf("%d %d %s",&a,&b,&s);
    long long ans = 0;
    for(int j = a;j <= b;j++) {
        if(sun[j].title.compare(s) == 0) ans += sun[j].x;
    }
    printf("%lld\n",ans);
}

return 0;
}

用string定义字符串,就不要用scanf了,用cin输入