【发布时间】:2015-06-21 19:07:28
【问题描述】:
我编写了以下代码,当我执行它时出现此错误:
在抛出 'std::length_error' 的实例后调用终止
what(): basic_string::_S_create
0Aborted(核心转储)
#include<stdio.h>
#include<string>
#include<vector>
#include<iostream>
#include<algorithm>
using namespace std;
bool comp(char c1, char c2)
{
return c1 < c2;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
string str;
getline(cin,str);
sort(str.begin(), str.end(), comp);
int k;
long long int sum40=0,sum41=0;
scanf("%d",&k);
cout<<str.length();
for(unsigned int i=0;i<str.length();++i)
{
if(str[i]=='(')
sum40++;
else
sum41++;
}
if(sum40!=sum41 && k==1)
cout<<str;
else
if(sum40!=sum41 && k!=1)
printf("-1");
else
{
while(k>0 || (sum40==sum41))
{
str.erase(str.begin());
k--;
}
cout<<str;
}
}
return 0;
}
我想知道什么是 std::length_error 错误以及如何删除它。
我知道这是常见问题。我看到了一堆关于此的文章,但那些文章并没有解决我的问题。
任何帮助将不胜感激
问候
【问题讨论】:
-
不要将 C-tag 用于 C++ 问题。它们是不同的语言!
-
我想知道错误发生在哪一行。还有为什么当 k>0 和 sum40==sum41 时,你会无限次地从字符串中删除一个字符。另外,您确定输入仅包含
(和)字符,否则变量名没有意义。 -
你有调试器吗?
-
@Mr ListerAfter I give input: 5 () 之后出现错误,对不起那个 K 错误这是一个编码错误
-
@rghome 我在 gcc/g++ 上工作
标签: c++ algorithm coding-style