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

时间:2018-01-25 06:03:46

标签: c++ string

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

#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 个答案:

答案 0 :(得分:2)

1)Why should I not #include <bits/stdc++.h>?

2)避免全局变量,并在大小未知时使用std::vector

3)Why is using namespace std considered bad practice?

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