C ++ - 循环

时间:2016-06-28 19:19:36

标签: c++ string runtime-error

我在之前的一些程序中发现了一种错误模式,我无法理解为什么模式会重复?

例如,在以下程序中

#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()中声明了一个字符串。当我将输入存储在字符串&#39; a&#39;第一次,它的工作。

之后我在循环中使用它。在循环中,它仅输入一次&amp;然后给出运行时错误:分段错误(Core Dumped)

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

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

为什么在循环内首次输入后会出现错误?

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

我不应该初始化左边&amp;以这种方式指出正确的指针,但以正确的方式做到这一点也无法解决问题。

我已经在错误的cin语句&amp;之前检查了每个变量的值。在此之前,每个变量的值都是合适的。

1 个答案:

答案 0 :(得分:2)

创建新树时,不会初始化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;
}