Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 请帮我做这个![排序数组]_Arrays_Sorting_Fstream_Addressbook - Fatal编程技术网

Arrays 请帮我做这个![排序数组]

Arrays 请帮我做这个![排序数组],arrays,sorting,fstream,addressbook,Arrays,Sorting,Fstream,Addressbook,大家好,我用数组创建了一个地址簿!。。。这里我遇到了一个字符串类型数组排序的问题。。。。当我想对联系人进行排序时,它只会对第一个姓名进行排序,而不会移动第二个姓名和电话号码。。。。。等我不清楚如何将整条线移动到通讯簿中进行排序。。。我的意思是同时移动[名字、姓氏、电话号码、电子邮件]到下一行!。。。这是我的密码 void sortRecords() { ofstream Cfile; Cfile.open("addressbook.txt", ios::in); string temp;

大家好,我用数组创建了一个地址簿!。。。这里我遇到了一个字符串类型数组排序的问题。。。。当我想对联系人进行排序时,它只会对第一个姓名进行排序,而不会移动第二个姓名和电话号码。。。。。等我不清楚如何将整条线移动到通讯簿中进行排序。。。我的意思是同时移动[名字、姓氏、电话号码、电子邮件]到下一行!。。。这是我的密码

void sortRecords() {

ofstream Cfile; 
Cfile.open("addressbook.txt", ios::in); 

string temp;
for (int i=0; i<line()-1; ++i)
{
    for (int j=0; j<line()-1-i; j++)
    {
        while (first_name[j]>first_name[j+1])
        {
            temp= first_name[j+1];
            first_name[j+1]=first_name[j];
            first_name[j]=temp;
        }
    }
}
for (int p=0; p<line();p++)
{

    Cfile<<first_name[p]<<setw(10)<<sur_name[p]<<setw(10)<<phone_number[p]<<setw(10)<<email[p]<<endl;
}
Cfile.close();
}
void sortRecords(){
流式钢管桩;
打开(“addressbook.txt”,ios::in);
字符串温度;

对于(int i=0;i而言,实现这一点的有效方法是使用第二个(数字)数组“间接指针”,即“最初位于
i
的项目现在位于
j
”。然后您只需交换这些索引;但完成后,您可以通过运行此索引数组一次并复制元素来生成已排序数组。这通常是在对“一堆东西”进行排序时的一种好方法

或者,您只需将所有项移动到它们的新位置;您需要编写一个“交换”函数以保持代码的易读性

当您希望将一组相关项保存在一个数组中时,通常最好声明一个结构。在您的情况下,此结构可能如下所示:

typedef struct {
  char first_name[32];
  char last_name[32];
  char phone_number[16];
} entry;
void swap(entry* e, int i, int j) {
// swap all elements of e[i] and e[j]
  entry temp;
  strcpy(temp.first_name,   e[i].first_name);
  strcpy(temp.last_name,    e[i].last_name);
  strcpy(temp.phone_number, e[i].phone_number;
  strcpy(e[i].first_name,   e[j].first_name);
  strcpy(e[i].last_name,    e[j].last_name);
  strcpy(e[i].phone_number, e[j].phone_number;
  strcpy(e[j].first_name,   temp.first_name);
  strcpy(e[j].last_name,    temp.last_name);
  strcpy(e[j].phone_number, temp.phone_number;
}
然后你可以将你的电话簿声明为

entry phoneBook[100];
这将为100个电话簿条目创造足够的空间

最后,当您浏览条目列表时,您可以这样修改冒泡排序(假设n=条目数):

void swap(entry* e, int i, int j) {
// swap all elements of e[i] and e[j]
  entry temp;
  strcpy(temp.first_name,   e[i].first_name);
  strcpy(temp.last_name,    e[i].last_name);
  strcpy(temp.phone_number, e[i].phone_number;
  strcpy(e[i].first_name,   e[j].first_name);
  strcpy(e[i].last_name,    e[j].last_name);
  strcpy(e[i].phone_number, e[j].phone_number;
  strcpy(e[j].first_name,   temp.first_name);
  strcpy(e[j].last_name,    temp.last_name);
  strcpy(e[j].phone_number, temp.phone_number;
}