【问题标题】:How to resolve this issue ? showing keith error如何解决这个问题?显示基思错误
【发布时间】:2021-04-02 20:31:50
【问题描述】:

我正在编写一个简单的 c++ 程序

#include <bits/stdc++.h>
#define ll long long
#define ul unsigned long long
#define ld long double
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define repi(i, a, b) for (int i = (a); i > (b); i--)
#define all(x) x.begin(), x.end()
#define ks(x) (cout << #x << ":" << (x) << '\n')
#define fastio ios_base::sync_with_stdio(false), cin.tie(nullptr)
#define gcd _gcd
using namespace std;
const ll mod = 1000000007;

int main()
{
    fastio;
    ll tc = 1;
    cin >> tc;
    for (ll t = 0; t < tc; t++)
    {
        ll n;
        cin >> n;
        string s;
        cin >> s;
        ll cnt = 0;
        ll i = n - 1;
        if (s[n - 1] == ')')
        {
            i--;
            cnt++;
            while (s[i] == ')' && i > -1)
            {
                i--;
                cnt++;
            }
        }
        if (cnt > n / 2)
            cout << "YES\n";
        else
        {
            cout << "NO\n";
        }
    }
    return 0;
}

并用输入 *

5
2
))
12
gl))hf))))))
9
gege)))))
14
)aa))b))))))))
1
)*

但它显示的输出我无法理解,请帮助

/home/keith/builds/mingw/gcc-9.2.0-mingw32-cross-native/mingw32/libstdc++-v3/include/bits/basic_string.h:1067: std::__cxx11::basic_string<_chart _traits _alloc>::reference std::__cxx11::basic_string<_chart _traits _alloc>::operator[](std::__cxx11::basic_string<_chart _traits _alloc>::size_type) [with _CharT = char; _Traits = std::char_traits; _Alloc = std::allocator; std::__cxx11::basic_string<_chart _traits _alloc>::reference = 字符&; std::__cxx11::basic_string<_chart _traits _alloc>::size_type = unsigned int]: 断言 '__pos

【问题讨论】:

  • 您有一个超出范围的索引错误,您是否尝试调试您的程序?
  • 但它在我朋友的笔记本电脑上运行相同的代码相同的输入
  • 是的,这就是您的代码中未定义行为的定义。启动您最喜欢的调试器,看看是什么原因造成的。
  • 是的,我收到了错误,但我仍然无法找到它在我朋友的计算机上的工作原理?它也被codeforces接受?它是如何工作的请解释一下是不是因为不同的编译器版本,比如我的是 MinGW 32 而他有 Mingw64?
  • 我不确定有什么不清楚的地方。行为是未定义的,它可以做任何事情,包括工作。特别是,您的编译器似乎有额外的检查。其他编译器可能只是接受 s[-1] 的心态“如果它崩溃了,不是我们的错”。

标签: c++ c++11 mingw


【解决方案1】:

断言__pos &lt;= size() 表示您正在使用末尾的 [] 运算符访问字符串 s。在调试器中运行程序,然后在 index > size() 上设置条件断点,或者只检查每个访问。

  1. s 和 n 都从 cin 读取(与 n = s.size() 相反),如果 n > s.size() + 2 应该触发它。
  2. n 是 (signed) long long 类型,并且 operator[] 需要 size_t (unsigned long) 所以 n
  3. 在您的 while 循环中,带有 i = -1 的 s[i] == ')' &amp;&amp; i &gt; -1 将首先尝试访问 s[-1],然后检查 i &gt; -1 作为 && 运算符作为从左到右的关联性。

【讨论】:

    【解决方案2】:

    这是错误的

    while (s[i] == ')' && i > -1)
    

    应该是

    while (i > -1 && s[i] == ')')
    

    如果 i 等于 -1,那么在第一个版本中 s[i] 会给你你可以看到的错误。但在第二个版本中,i &gt; -1 首先被评估,因为它是错误的,s[i] 没有被评估,所以不会导致错误。

    这里还有一个重要的教训。该程序在您朋友的机器上运行,但并非每个有错误的程序都会失败。它甚至可能看起来运行正确。

    【讨论】:

    • 是的,我弄错了,但我仍然无法找到它在我朋友的计算机上的工作原理?它也被codeforces接受?它是如何工作的请解释一下是不是因为不同的编译器版本,比如我的是 MinGW 32 而他有 Mingw64?
    • @Suryakant 就像我说的,并不是每个有错误的 C++ 程序都会失败。 C++ 不是那样工作的。原因可能是由于您的编译器的差异,但理论上它可以是任何东西。当你编写一个出现这种错误的程序时,C++ 不能保证它会如何运行。
    猜你喜欢
    • 2013-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-22
    • 2013-11-23
    • 2021-07-01
    相关资源
    最近更新 更多