C ++将一个struct值复制到另一个

时间:2012-05-16 18:05:23

标签: c++ copy structure

我的结构很简单:

struct Appartament
{
  char address[50];
  char telephoneNumber[20];
  char view[10];

  double price;
  double distanceFromCenter;

  int roomCount;
};

我有一些写在文件中的记录。现在,我想要读取文件中的所有记录,并且只获得那些使roomCount小于数字(用户输入)的人。这很容易,但记录应按价格排序。我必须将它们放在一个数组中然后对它们进行排序。

我有一些问题,我相信他们是因为我没有很好地把这些结构包括在内。

我尝试了不同的方式:

strcpy(CurrentRecords[index].address,currentRecord.address);
strcpy(CurrentRecords[index].telephoneNumber,currentRecord.telephoneNumber);
strcpy(CurrentRecords[index].view,currentRecord.view);
CurrentRecords[index].price=currentRecord.price;
CurrentRecords[index].distanceFromCenter=currentRecord.distanceFromCenter;
CurrentRecords[index].roomCount=currentRecord.roomCount;

memcpy(CurrentRecords[index],currentRecord,sizeof(Appartament));

CurrentRecords[index]=currentRecord

但没有任何作用......

编辑:这是我的代码 - "没有任何作用"指无穷无尽的循环。

    void AdvanceSearch()
    {
        clrscr();

        Appartament currentRecord;

        fstream currentFile("Records.dat",ios::binary|ios::in);

        if(!currentFile)
        {
            cout<<"Error - the file could not be opened."<<endl;
            return;
        }
        else
        {
            //Array with apartments records
            Appartament CurrentRecords[MaxRecords];

            currentFile.seekg(0L,ios::end);
            long int length=currentFile.tellg();
            currentFile.seekg(0L,ios::beg);

            int isAppartamentFound=0;

            if(length==0)
            {
                cout<<"The file is empty."<<endl;
                return;
            }
            else
            {
                int userRoomCount;

                do
                {
                    clrscr();
                    cout<<"Enter apartment room count - ";
                    cin>>userRoomCount;
                }while(userRoomCount<0);

                clrscr();

                cout<<endl<<"Apartments with "<<userRoomCount<<" rooms order by price:";

                currentFile.read((char*)(&currentRecord),sizeof(Appartament));

                int index=0;

                while(!currentFile.eof())
                {
                    if(currentRecord.roomCount==userRoomCount)
                    {

                         /*
                        strcpy(CurrentRecords[index].address,currentRecord.address);
                        strcpy(CurrentRecords[index].telephoneNumber,currentRecord.telephoneNumber);
                        strcpy(CurrentRecords[index].view,currentRecord.view);
                        CurrentRecords[index].price=currentRecord.price;
                        CurrentRecords[index].distanceFromCenter=currentRecord.distanceFromCenter;
                        CurrentRecords[index].roomCount=currentRecord.roomCount;
                         */

                         memcpy(CurrentRecords[index],currentRecord,sizeof(Appartament));

                        //CurrentRecords[index]=currentRecord;
                        index++;
                        isAppartamentFound=1;
                    }

                    currentFile.read((char*)(&currentRecord),sizeof(Appartament));
                }

                currentFile.close();
            }

            if(isAppartamentFound==0)
            {
                cout<<endl<<"There are no matches!"<<endl;
            }
            else
            {
                //If only one apartment is found
                if(sizeof(CurrentRecords)/sizeof(Appartament)==1)
                {
                    cout<<endl;
                    ShowRecord(currentRecord);

                }
                else
                {
                    //Sort the records

                    Appartament tempApartament;

                    int isChangeMade=1;

                    while(isChangeMade==1)
                    {
                        isChangeMade=0;

                        for(int index=0;index<(sizeof(CurrentRecords)/sizeof(Appartament))-1.0;index++)
                        {
                            if(CurrentRecords[index].price>CurrentRecords[index+1].price)
                            {
                                isChangeMade=1;

                                CopyApartament(tempApartament,CurrentRecords[index]);
                                CopyApartament(CurrentRecords[index],CurrentRecords[index+1]);
                                CopyApartament(CurrentRecords[index+1],tempApartament);
                            }
                        }
                    }

                    for(int index=0;index<sizeof(CurrentRecords)/sizeof(Appartament)-1.0;index++)
                    {
                        ShowRecord(CurrentRecords[index]);
                    }
                }
            }
         }
    }

    void CopyApartament(Appartament RecordOne,Appartament RecordTwo)
    { 
                    /*
        strcpy(RecordOne.address,RecordTwo.address);
        RecordOne.distanceFromCenter=RecordTwo.distanceFromCenter;
        RecordOne.price=RecordTwo.price;
        RecordOne.roomCount=RecordTwo.roomCount;
        strcpy(RecordOne.telephoneNumber,RecordTwo.telephoneNumber);
        strcpy(RecordOne.view,RecordTwo.view);
                    */
                    RecordOne=RecordTwo;

    }

注意:我认为我的问题是复制,因为我不知道该怎么做。

2 个答案:

答案 0 :(得分:4)

CurrentRecords[index]=currentRecord;

复制整个结构,应该按预期工作。

您确定要正确排序吗?

答案 1 :(得分:0)

我认为没有任何特别好的理由可以在任何地方明确复制数据。我想我会做这样的事情:

std::vector<Apartment> get_apts(int max_rooms) { 

    std::vector<Apartment> apts;

    std::copy_if(file.begin(), file.end(), std::back_inserter(apts),
                 [max_rooms](Apartment const &a) { 
                     return a.roomCount < max_rooms; 
                 });

    std::sort(apts.begin(), apts.end(), 
              [](Apartment const &a, Apartment const &b) { 
                  return a.price < b.price); 
              });

    return apts;
}