为什么下面的代码给出 'std::logic_error' what(): basic_string::_M_construct null 无效?
Why does the below code give 'std::logic_error' what(): basic_string::_M_construct null not valid?
我得到 std::logic_error' what(): basic_string::_M_construct null not valid when i 运行 the function sort_string with the for loop but not with simple relational operator比较两个字符串。
该程序从给定数字的向量中构造出最大的数字。对于小输入它工作正常但不适用于大输入。我在下面提供了输入。
#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
bool sort_string(std::string x, std::string y) {
std::string xy = x.append(y);
std::string yx = y.append(x);
// For loop below, to calculate larger string gives "terminate called after throwing
// an instance of 'std::logic_error' what(): basic_string::_M_construct null not valid"
// Just comment the for loop and uncomment the last return statement to see
//-------------------------------------------------------------------------------------------------------
for (int i = 0; i < xy.size(); i++) {
if (xy.at(i) > yx.at(i)) return true;
if (yx.at(i) > xy.at(i)) return false;
}
return true;
//-------------------------------------------------------------------------------------------------------
/*
This runs perfectly fine
*/
//return xy>=yx;
}
int main() {
int n;
std::cin >> n;
std::vector<std::string> arr(n);
for (int i = 0; i < n; i++) {
std::cin>>arr[i];
}
std::sort(arr.begin(), arr.end(), sort_string);
for (int i = 0; i < n; i++) {
std::cout << arr[i];
}
std::cout << std::endl;
}
说明:
运行 与 g++ -std=c++14
输入:
100
2 8 2 3 6 4 1 1 10 6 3 3 6 1 3 8 4 6 1 10 8 4 10 4 1 3 2 3 2 6 1 5 2 9 8 5 10 8 7 9 6 4 2 6 3 8 8 9 8 2 9 10 3 10 7 5 7 1 7 5 1 4 7 6 1 10 5 4 8 4 2 7 8 1 1 7 4 1 1 9 8 6 5 9 9 3 7 6 3 10 8 10 7 2 5 1 1 9 9 5
您的代码只需将 for loop
外的 return true
更改为 return false
即可工作。但是,我不明白你为什么要把这段代码复杂化 SO 而你可以简单地做
bool sort_string(const std::string& x, const std::string& y)
{
return x > y;
}
或者甚至将其实现为 lambda。
问题是由
引起的
return true;
在 sort_string
中。需要
return false;
当您到达那条线时,xy
等于 到 yx
。因此 yx < xy
是 false
,而不是 true
。
行
return xy >= yx;
有效,因为该行与
相同
return yx < xy;
我得到 std::logic_error' what(): basic_string::_M_construct null not valid when i 运行 the function sort_string with the for loop but not with simple relational operator比较两个字符串。 该程序从给定数字的向量中构造出最大的数字。对于小输入它工作正常但不适用于大输入。我在下面提供了输入。
#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
bool sort_string(std::string x, std::string y) {
std::string xy = x.append(y);
std::string yx = y.append(x);
// For loop below, to calculate larger string gives "terminate called after throwing
// an instance of 'std::logic_error' what(): basic_string::_M_construct null not valid"
// Just comment the for loop and uncomment the last return statement to see
//-------------------------------------------------------------------------------------------------------
for (int i = 0; i < xy.size(); i++) {
if (xy.at(i) > yx.at(i)) return true;
if (yx.at(i) > xy.at(i)) return false;
}
return true;
//-------------------------------------------------------------------------------------------------------
/*
This runs perfectly fine
*/
//return xy>=yx;
}
int main() {
int n;
std::cin >> n;
std::vector<std::string> arr(n);
for (int i = 0; i < n; i++) {
std::cin>>arr[i];
}
std::sort(arr.begin(), arr.end(), sort_string);
for (int i = 0; i < n; i++) {
std::cout << arr[i];
}
std::cout << std::endl;
}
说明: 运行 与 g++ -std=c++14
输入:
100
2 8 2 3 6 4 1 1 10 6 3 3 6 1 3 8 4 6 1 10 8 4 10 4 1 3 2 3 2 6 1 5 2 9 8 5 10 8 7 9 6 4 2 6 3 8 8 9 8 2 9 10 3 10 7 5 7 1 7 5 1 4 7 6 1 10 5 4 8 4 2 7 8 1 1 7 4 1 1 9 8 6 5 9 9 3 7 6 3 10 8 10 7 2 5 1 1 9 9 5
您的代码只需将 for loop
外的 return true
更改为 return false
即可工作。但是,我不明白你为什么要把这段代码复杂化 SO 而你可以简单地做
bool sort_string(const std::string& x, const std::string& y)
{
return x > y;
}
或者甚至将其实现为 lambda。
问题是由
引起的return true;
在 sort_string
中。需要
return false;
当您到达那条线时,xy
等于 到 yx
。因此 yx < xy
是 false
,而不是 true
。
行
return xy >= yx;
有效,因为该行与
相同return yx < xy;