在循环内输入字符串时出现运行错误
C++ - Runtime error on string input inside loop
我在以前的一些程序中观察到一种错误模式,我无法理解为什么这种模式会重复?
例如,在下面的程序
中#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语句之前检查了每个变量的值&在该点之前,每个变量的值都是合适的
创建新树时,不初始化left
和right
。您的代码假设它们被自动初始化为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;
}
- 任何人都可以弄清楚这段代码如何显示运行错误?它打印无限时间 -1 以及正确答案
- 将 opencv 从 2.4.13 转换为 3.3.1 时由 cv::resize() 引起的运行错误
- 为什么 leetcode 上有运行错误,而 vs2019 上没有错误?
- 我的代码没有对字符串,C ++运行错误
- C++运行错误:未分配正在释放的指针
- 代码块运行错误: "Process terminated with status -1073741510" 。我做错了什么?
- VS2013中SSE2代码的运行错误
- C++将文本文件读入数组,运行错误:分段错误
- c++运行错误
- 发生无法运行错误
- 简单C++程序在 Xcode、代码块和终端中运行错误
- C++入门练习 1.20 运行错误
- 使用 MinGW 和 netbeans 7.3.1 运行错误
- "snake"运行错误
- 处理提升端点侦听/运行错误
- 不运行错误LNK1120:1 个未解析的外部 VS
- 一切都是正确的,但不能运行:错误预期{或;
- 使用Qt Creator运行错误
- 在循环内输入字符串时出现运行错误
- 为什么sizeof在linux上运行良好,而在windows上遇到运行错误