排序时无法执行交换操作.我做的时候它会崩溃.为什么
I can not do a swap operation while sorting. It crashes when I do it. Why?
struct Dates
{
int day;
int month;
int year;
}accountinfo[3];
struct Accounts
{
string name,lastname;
int number;
float balance;
}account[3];
void sortduetoaccnumbers()
{
for (i=0;i<3;i++)
{
for (j=0;j<3;j++)
{
if (account[j].number>account[j+1].number)
{
//swap
}
}
}
}
void sortduetodates()
{
for (i=0;i<3;i++)
{
for (j=0;j<3;j++)
{
if (accountinfo[j].year>accountinfo[j+1].year)
{
//swap
}
else if (accountinfo[j].year==accountinfo[j+1].year)
{
if (accountinfo[j].month>accountinfo[j+1].month)
{
//swap
}
else if (accountinfo[j].month==accountinfo[j+1].month)
{
if (accountinfo[j].day>accountinfo[j+1].day)
{
//swap
}
}
}
}
}
}
我无法使用排序算法对这些帐户进行排序。如果我进入它们,它就会崩溃。cmd突然停止并完成程序。
我输入了一个评论行,其中必须交换函数。这样你就可以分析代码了。
除此之外,其他所有功能都在工作。我陷入了困境。
此代码错误:if (accountinfo[j].year>accountinfo[j+1].year)
因为如果j==2,那么j+1=3-数组大小上的索引=>未定义的行为(在您的情况下崩溃(
您需要将循环条件更改为j<2
或重写您的支票
相关文章:
- 获取日期异步信号安全吗?如果在信号处理程序中使用,它会导致死锁吗
- ExtractIconEx:可以工作,但偶尔会崩溃
- 当我在第一个循环中使用"auto"时,它工作正常,但是使用"int"它会给出错误,为什么?
- 排序时无法执行交换操作.我做的时候它会崩溃.为什么
- 如何使用递归循环我的代码(当用户输入无效输入时,它会再次提示他们)?
- 将引用分配给局部变量,如果局部变量超出范围,它会超出范围吗?
- 在 if 语句中使用 GetKeyState 时,它会跳过条件
- C++ Python 模块在 Blender 中崩溃,但在 Python 控制台中不会崩溃
- 将对象创建为全局/静态对象会崩溃,而本地对象不会崩溃
- 了解运算符重载和迭代器,为什么它会打印出"wrhrwwr"?
- 基本的 c++ 问题:如果我在函数中创建某些内容并返回它会发生什么?
- 您好,我实际上想了解以下代码.有人可以详细说明代码它到底在做什么吗?为什么它会在第 31 行崩溃
- 除非使用某些寄存器,否则函数挂钩会崩溃
- 迭代器擦除矢量中的元素无效,但它不会崩溃
- 传递一个向量作为参数并使用它,它为什么会崩溃
- 为什么会崩溃?我该如何修复它
- 为什么当我到达我的 2D 阵列时它会崩溃?out_of_range
- 测试答案是错的,对吗?它不会崩溃,因为我们删除NULL指针,这是安全的
- 内存访问:为什么它不会崩溃以及如何强制执行它?
- 为什么它会崩溃