C++:使用插入排序的程序崩溃

标签 c++ data-structures

我正在尝试对员工记录进行排序。该记录由包含雇员的名字、姓氏和年龄的条目组成。整个程序运行良好,除了某些输入语句(在主函数中作为注释给出)。程序如下:-

#include<iostream>
#include<vector>
#include<string>
using namespace std;
class cPerson
{
    private:
    string firstname,lastname;
    int age;
    public:
    cPerson(string fn,string ln,int a)      // constructor to input the firstname, lastname and age of the person
    {
        firstname=fn;
        lastname=ln;
        age=a;
    }
    void display()
    {
        cout<<"First Name = "<<firstname<<"\n";
        cout<<"Last Name = "<<lastname<<"\n";
        cout<<"Age = "<<age<<"\n";
    }
    int getAge()
    {
        return age;
    }
    string getLastName()
    {
        return lastname;
    }
};
class cOffice
{
    private:
        vector<cPerson*> v;
        int nElem;                      // counter of number of elements in the vector;
    public:
        cOffice(int max)
        {
            v.resize(max);              // maximum capacity of the vector
            nElem=0;                    
        }
        ~cOffice()
        {
            for(int i=0;i<nElem;i++)
                delete v[i];
        }
        void insertRec(string fn1, string ln1, int a1)      // inserting the record
        {
            v[nElem] = new cPerson(fn1,ln1,a1);
            nElem++;
        }
        void InsertionSort()
        {
            int compare,pivot;
            for(pivot=1;pivot<nElem;pivot++)
            {
                cPerson* temp = v[pivot];
                for(compare=pivot;v[compare-1]->getAge()>temp->getAge();compare--)
                {   
                    v[compare]=v[compare-1];
                }
                v[compare] = temp;
            }
        }
        void display()
        {
            for(int i=0;i<nElem;i++)
                v[i]->display();
        }
};
int main(void)
{
    cOffice obj(10);
    obj.insertRec("Evans", "Patty", 24); 
    obj.insertRec("Adams", "Henry", 63);
    obj.insertRec("Yee", "Tom", 43);
    obj.insertRec("Smith", "Lorraine", 37);
    /*obj.insertRec("Hashimoto", "Sato", 21);                  // cause of the crash
    obj.insertRec("Stimson", "Henry", 29);
    obj.insertRec("Velasquez", "Jose", 72);
    obj.insertRec("Lamarque", "Henry", 54);
    obj.insertRec("Vang", "Minh", 22);
    obj.insertRec("Creswell", "Lucinda", 18);*/  
    obj.display();
    obj.InsertionSort();
    obj.display();
    return 0;
}

问题是前四个输入语句工作正常(以我输入的任何顺序)。这表明我的 InsertionSort() 函数工作正常(是吗?)。我想知道我在程序中做错了什么。感谢您的帮助。

更正后的 InsertionSort 函数(如 MM-BB 所建议的)如下:-

void InsertionSort()
            {
                int compare,pivot;
                for(pivot=1;pivot<nElem;pivot++)
                {
                    cPerson* temp = v[pivot];
                    for(compare=pivot;compare>0&&v[compare-1]->getAge()>temp->getAge();compare--)
                    {   
                        v[compare]=v[compare-1];
                    }
                    v[compare] = temp;
                }
            }

更正是在第二个 for 语句的条件之前添加术语 compare>0 &&。这确保了 vector v 保持在范围内。早些时候,这不是导致 v[-1] 未定义并因此导致程序崩溃的情况。

最佳答案

for(compare=pivot;v[compare-1]->getAge()>temp->getAge();compare--)

假设 pivot1compare 将是 1。然后访问列表 v 中的元素 0。如果年龄较大,您将减少比较,这会使您访问越界元素 (v[-1])。

关于C++:使用插入排序的程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17151772/

相关文章:

c++ - 友元函数看不到私有(private)成员变量

c# - 在 C++ 和 C# 或 VB 之间共享数据(2 个正在运行的程序)

c++ - 在启用 C++ 的 OSX 10.9 上编译 GMP 库

python - 使用位数组创建类似对象的列表

java - 从不同文件夹中的所有文件中找出 100 个最大的数字

c++ - Visual Studio 2017 创建/调试程序

c++ - 虚拟机和内存访问问题

algorithm - 参观博物馆的最低费用

algorithm - 快速插入和搜索

c++ - 实现一个指向可变大小结构的指针