在循环内输入字符串时出现运行错误

C++ - Runtime error on string input inside loop

本文关键字:运行 错误 字符串 循环 输入      更新时间:2023-10-16

我在以前的一些程序中观察到一种错误模式,我无法理解为什么这种模式会重复?

例如,在下面的程序

#include <iostream>
#include <string>
using namespace std;
struct tree
{
    string b;
    int val;
    tree *left,*right;
};
int main()
{
    tree *start,*temp,*tem;
    string a;                               //Declaring string a here
    int t,n,k,m;
    cin>>t;
    while(t--)
    {
        cin>>n>>k>>m;
        cin>>a;                            //Input to a for first time,working
        start=new tree;
        start->b=a;
        start->val=1;
        n--;
        while(n--)
        {
            temp=start;
            cin>>a;                         //Input to a, working only once
            while(1)
            {
                if(a.substr(0,m)<=temp->b.substr(0,m))
                {
                    temp->val++;
                    if(temp->left)
                    temp=temp->left;
                    else
                    {
                        tem=new tree;
                        tem->b=a;
                        tem->val=1;
                        temp->left=tem;
                        break;
                    }
                }
                else
                {
                    if(temp->right)
                    temp=temp->right;
                    else
                    {
                        tem=new tree;
                        tem->b=a;
                        tem->val=1;
                        temp->right=tem;
                        break;
                    }
                }
            }
            temp=start;
            while(1)
            {
                if(temp->val==k)
                {
                    cout<<temp->b<<endl;
                    break;
                }
                else if(temp->val<k)
                {
                    k--;
                    temp=temp->left;
                }
                else
                {
                    k=k-temp->val;
                    temp=temp->right;
                }
            }
        }
    }
    return 0;
}

我在main()中声明了字符串a。当我第一次在字符串'a'中存储输入时,它工作了。

之后我在循环中使用了它。在循环中,它只接受一次输入&然后给出运行时错误:Segmentation fault(Core dump)

经过多次测试,我发现它在循环中第一次输入后就给出了运行时错误。我在另外两个问题中也遇到过同样的情况。在我能够解决的一个问题中,我观察到在修改与字符串完全无关的整数值后错误停止显示,它与字符串处于相同的循环中。我检查了循环中每个数据的值,但没有发现它们之间有任何联系。

循环的格式和字符串的位置在其他问题中与此相同。

为什么在循环内第一次输入后给出错误?

如果有人需要,问题说明的链接是:https://www.hackerearth.com/problem/algorithm/xenny-and-partially-sorted-strings-7/

我不应该初始化左边的&以这种方式对指针进行操作,但以正确的方式操作也不能解决问题。

我在错误的cin语句之前检查了每个变量的值&在该点之前,每个变量的值都是合适的

创建新树时,不初始化leftright。您的代码假设它们被自动初始化为nullptr,但您需要显式地这样做。因此,当您执行if (temp->left)if (temp->right)时,您正在测试未初始化的值,这会导致未定义的行为。

另一个问题是,在输入循环中搜索第k个字符串的循环。它应该在它后面。在解引用temp之前,它需要检查空指针。

您应该修复的其他事情:使用getline而不是>>来读取输入,因为问题声明说每行是一个字符串。cin >> a;将只读取一个单词,而不是整行。

#include <iostream>
#include <string>
using namespace std;
struct tree {
    string b;
    int val;
    tree *left,*right;
};
int main() {
    tree *start,*temp,*tem;
    string a;                               //Declaring string a here
    int t,n,k,m;
    cin>>t;
    while(t--) {
        cin>>n>>k>>m;
        cin>>a;                            //Input to a for first time,working
        start=new tree;
        start->b=a;
        start->val=1;
        start->left = start->right = nullptr;
        n--;
        while(n--) {
            temp=start;
            cin>>a;                         //Input to a, working only once
            while(1) {
                if(a.substr(0,m)<=temp->b.substr(0,m)) {
                    temp->val++;
                    if(temp->left)
                        temp=temp->left;
                    else {
                        tem=new tree;
                        tem->b=a;
                        tem->val=1;
                        tem->left = tem->right = nullptr;
                        temp->left=tem;
                        break;
                    }
                }
                else {
                    if(temp->right)
                        temp=temp->right;
                    else {
                        tem=new tree;
                        tem->b=a;
                        tem->val=1;
                        tem->left = tem->right = nullptr;
                        temp->right=tem;
                        break;
                    }
                }
            }
        }
        temp=start;
        while(temp) {
            if(temp->val==k) {
                cout<<temp->b<<endl;
                break;
            }
            else if(temp->val<k) {
                k--;
                temp=temp->left;
            }
            else {
                k=k-temp->val;
                temp=temp->right;
            }
        }
    }
    return 0;
}