简体   繁体   English

程序以信号 SIGABRT 终止,已中止。 在抛出 'std::out_of_range' 的实例后调用终止

[英]Program terminated with signal SIGABRT, Aborted. terminate called after throwing an instance of 'std::out_of_range'

The question is: Consider an array of numeric strings where each string is a positive number with anywhere from 1 to 10^6 digits.问题是:考虑一个数字字符串数组,其中每个字符串都是一个从 1 到 10^6 位的正数。 Sort the array's elements in non-decreasing, or ascending order of their integer values and return the sorted array.按 integer 值的非递减或升序对数组元素进行排序,并返回排序后的数组。

vector<string> bigSorting(vector<string> unsorted) {
    for(int i=0 ; i<unsorted.size() ; i++){
        for(int j=0 ; j<unsorted.size()-1; j++){
            long int a = stol(unsorted[j]);
            long int b = stol(unsorted[j+1]);
            if(a > b){
                string x = unsorted[j];
                unsorted[j] = unsorted[j+1];
                unsorted[j+1] = x;
            }
        }
    }
    return unsorted;
}

I made the above function for the question and it gave an error:我为这个问题做了上面的 function ,它给出了一个错误:

terminate called after throwing an instance of 'std::out_of_range'
  what():  stol
Reading symbols from Solution...done.
[New LWP 450]
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
Core was generated by `./Solution'.
Program terminated with signal SIGABRT, Aborted.
#0  __GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:50
To enable execution of this file add
    add-auto-load-safe-path /usr/local/lib64/libstdc++.so.6.0.25-gdb.py
line to your configuration file "//.gdbinit".
To completely disable this security protection add
    set auto-load safe-path /
line to your configuration file "//.gdbinit".
For more information about this security protection see the
"Auto-loading safe path" section in the GDB manual.  E.g., run from the shell:
    info "(gdb)Auto-loading safe path"

What is the meaning of this error code and how do I fix it?这个错误代码是什么意思,我该如何解决?

As pointed out in the comments, your error originates form your attempt to read a number with many digits (up to a million) into a 64bit number, which can hold all numbers up to 1<<64=18446744073709551616 , ie no numbers with more than 20 digits.正如评论中指出的那样,您的错误源于您尝试将具有多位数字(最多一百万)的数字读入一个 64 位数字,该数字可以容纳最多1<<64=18446744073709551616的所有数字,即没有更多的数字超过 20 位数。

Hence you must find another way to compare those strings.因此,您必须找到另一种方法来比较这些字符串。 The question is not very clear about leading zeros, but let us assume that the numbers are represented without leading zeros.关于前导零的问题不是很清楚,但让我们假设数字表示时没有前导零。 Then the std::string s, representing the numbers, can be compared by their size() and, if that is equal, lexicographically, which is implemented by operator< between std::string s, ie然后表示数字的std::string可以通过它们的size()进行比较,如果相等,则按字典顺序进行比较,这由std::string之间的operator<实现,即

std::vector<std::string> bigSorting(std::vector<std::string> unsorted)
{
    std::sort(unsorted.begin(), unsorted.end(), 
        [](std::string const&x, std::string const&y) {
            return x.size()==y.size()? x<y : x.size() < y.size();
        });
    return unsorted;
}

If there are leading zeros, you must adapt your method accordingly.如果有前导零,您必须相应地调整您的方法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 抛出&#39;std :: out_of_range&#39;实例后调用终止 - terminate called after throwing an instance of 'std::out_of_range' 抛出&#39;std :: out_of_range&#39;实例后调用终止 - terminate called after throwing an instance of 'std::out_of_range' 在抛出“std::out_of_range”的实例后调用终止? - terminate called after throwing an instance of 'std::out_of_range'? 抛出&#39;std :: out_of_range&#39;实例后调用终止 - terminate called after throwing an instance of 'std::out_of_range' 在为我的程序抛出一个 &#39;std::out_of_range&#39; 实例后调用 Terminate 来查找和替换字符串中的单词 - Terminate called after throwing an instance of 'std::out_of_range' for my program which finds and replaces words in a string 将字母向前移动3个字母的程序,错误:抛出&#39;std :: out_of_range&#39;实例后终止终止 - Program that shifts letters 3 letters forward, error: terminate called after throwing an instance of 'std::out_of_range' 抛出&#39;std :: out_of_range&#39;what():basic_string :: substr的实例后调用终止 - terminate called after throwing an instance of 'std::out_of_range' what(): basic_string::substr 抛出&#39;std :: out_of_range&#39;实例后继续调用终止 - Keeps on getting terminate called after throwing an instance of 'std::out_of_range' 抛出“ std :: out_of_range”实例后,调用C ++ Game Started终止终止 - C++ Game Started terminate called after throwing an instance of 'std::out_of_range' 抛出std :: out_of_range实例后如何修复终止调用 - How to fix terminate called after throwing an instance of std::out_of_range
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM