C++:,当我使用字符串数组时,它的字符串下标超出了范围

C++:,when i use string array, it comes string subscript out of range

本文关键字:字符串 下标 范围 数组 C++      更新时间:2023-10-16

我只想知道为什么会出现字符串下标超出范围的问题,我已经初始化了字符串数组。谢谢。

#include<bits/stdc++.h>
#define N 100000
using namespace std;
string s[N + 5] = {}, ss[N + 5] = { " " }, fs[N + 5] = { " " };
int main()
{
   int n, x; char c;
   cin >> n;
   for (int i = 0; i<n; ++i)
   {
      for (int j = 0; j<6; ++j)
       {
          scanf("%c", &c);//the error comes here.
          if (c == ' ') continue;
          s[i] += c;
       }
      ss[i] = s[i] + s[i];
      for (int j = 5; j >= 0; --j) fs[i][j] = s[i][5 - j];
   }
   int flag = 0;
   for (int i = 0; i<n; ++i)
   {
      for (int j = 0; j<n; ++j)
     {
        if (i == j) continue;
        if (find(s[i], fs[j]) || find(s[i], ss[j]))
        {
            flag = 1; break;
        }
      }
 }

}

1(为什么我不应该 #include <bits/stdc++.h>

2(避免全局变量,当大小未知时使用std::vector。

3(为什么using namespace std被认为是不良做法?

由于fs[i][j]访问空字符串f[i],您收到超出范围的错误。