C++错误(分段故障 /总线错误 /内存限制超过 /堆栈限制)使用functions lower_bound,排序,
我的程序在上传到学校测试服务器后崩溃,并宣布发生了这些错误之一(分段错误/总线错误/超出内存限制/超出堆栈限制),我没有确切的信息。如果我在调试器中运行该程序,我找不到任何东西。审核方法可能会失败。
整个程序https://onecompiler.com/cpp/3xy2j7dmd
class Company
{
private:
string name;
string addr;
string id;
unsigned int totalIncome;
unsigned int numberOrders;
unsigned int amount;
};
bool Company::cmpNA (const Company &a, const Company &b)
{
if ( a.getName() < b.getName())
return a.getName() < b.getName();
return a.getAddr() < b.getAddr();
}
bool CVATRegister::audit ( const string &name, const string &addr, unsigned int &sumIncome ) const
{
Company cmp(name, addr,"-1");
vector<Company> tmp = DCompany;
sort(tmp.begin(), tmp.end(), [](const Company & a, const Company & b)
{
if ( a.getName() < b.getName())
return a.getName() < b.getName();
return a.getAddr() < b.getAddr();
});
auto itr = lower_bound(tmp.begin(), tmp.end(), cmp, &Company::cmpNA);
if(itr != tmp.end() && addr == itr->getAddr() && itr->getName() == name)
{
sumIncome = itr->getTotalIncome();
return true;
}
return false;
}
My program crashes after uploading to the school test server with the announcement that one of these errors has occurred (Segmentation fault / Bus error / Memory limit exceeded / Stack limit exceeded), I have no exact information. If I run the program in the debugger, I can't find anything. The audit method may fail.
The whole program https://onecompiler.com/cpp/3xy2j7dmd
class Company
{
private:
string name;
string addr;
string id;
unsigned int totalIncome;
unsigned int numberOrders;
unsigned int amount;
};
bool Company::cmpNA (const Company &a, const Company &b)
{
if ( a.getName() < b.getName())
return a.getName() < b.getName();
return a.getAddr() < b.getAddr();
}
bool CVATRegister::audit ( const string &name, const string &addr, unsigned int &sumIncome ) const
{
Company cmp(name, addr,"-1");
vector<Company> tmp = DCompany;
sort(tmp.begin(), tmp.end(), [](const Company & a, const Company & b)
{
if ( a.getName() < b.getName())
return a.getName() < b.getName();
return a.getAddr() < b.getAddr();
});
auto itr = lower_bound(tmp.begin(), tmp.end(), cmp, &Company::cmpNA);
if(itr != tmp.end() && addr == itr->getAddr() && itr->getName() == name)
{
sumIncome = itr->getTotalIncome();
return true;
}
return false;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
那种lambda不提供 严格的弱订购 按照
std :: Sort
的要求。 sort and lower_bound can can can can ber都会失败。你的意思?
That sort lambda doesn't provide strict weak ordering as required by
std::sort
. Both sort and lower_bound can/will fail.Did you mean?