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

标签 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) 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] .

关于C++:,当我使用字符串数组时,出现字符串下标超出范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48436593/

相关文章:

Swift 条件字符串插值

python - 如何检查字符串是否与 Python 中的设置模式匹配?

c++ - 在结构中指定设备特定功能

c++ - 链接到 DLL 的未处理异常

python - 如何居中Python打印

swift - 获取表情符号字符的描述

c - C 中的反转字符串函数?

c++ - 不使用标准库复制字符串

c++ - 我怎样才能根据用户输入生成 "weighted"随机数,使某些数字优先于其他数字?

c++ - cURL 显示 SSLv3 而不是 TLSv1