将结构体未排序数组中的下标向左移动

shifting indices to the left in a struct unsorted array

本文关键字:下标 左移 移动 结构体 排序 数组      更新时间:2023-10-16

我有我的整个程序的这个片段。在这个函数中,特别是在程序的这一部分中,用户输入一个MLS#,表示要从数组中删除的home。第一个"for"语句搜索MLS#,然后null表示所有数据。我的下一个问题是"声明"。在索引为空后将所有数据向左移动。struct数组中存储的数据如下:

struct mlsListing { int mlsNum;           // Struct Array holds one line of the struct
                    double price;         // mlsListing 
                    int type; 
                    string zip; 
                    string company; 
                    string realty; 
                  }; 
 const int MAX_LISTINGS = 750;  
        mlsListing houseData[MAX_LISTINGS]; 
 const int NOT_FOUND = -1;
 int targetMLS; // Variable for target MLS
 int mlsDelete; // Variable for target MLS found
 int mlsCounter;// Counter for finding target MLS 
 int count;     // Array Counter

// Function 
void {
 cout << "Enter the MLS# you wish to delete: "; 
           cin >> targetMLS;                       // User input MLS#

        for (mlsCounter = 0; ((mlsCounter < count) && (mlsDelete == NOT_FOUND));
                                 mlsCounter++) {
                 if (houseData[mlsCounter].mlsNum == targetMLS) {
                    mlsDelete = houseData[mlsCounter].mlsNum; 
                    houseData[mlsCounter].mlsNum = 0; 
                    houseData[mlsCounter].price = 0;
                    houseData[mlsCounter].type = 0;
                    houseData[mlsCounter].zip.clear(); 
                    houseData[mlsCounter].company.clear();
                    houseData[mlsCounter].realty.clear(); 
                  }
          }

         // Shifting indices to the left after deletion?
          for (move = mlsCounter;move < count; move++){
              houseData[move].mlsNum = houseData[move+1].mlsNum; 
              houseData[move].price = houseData[move+1].price; 
              houseData[move].type = houseData[move+1].type; 
              houseData[move].zip = houseData[move+1].zip; 
              houseData[move].company = houseData[move+1].company;
              houseData[move].realty = houseData[move+1].realty;  
          } 
         count--; 
}

第二个for循环越界了。必须是:

for (move = mlsCounter;move < count - 1; move++)

它的作用是通过遍历所有剩余的项并将它们移近一个(然后在最后减少计数),将该项从数组中的位置移除。

简化的例子:

假设您有一个只包含字符的简单数组。让我们有一个10个字符的数组:

Array position :   [0]  [1]  [2]  [3]  [4]  [5]  [6]  [7]  [8]  [9]
Values in array:    a    l    p    h    a    b    e    t    i    c

如何删除[5]位置的字母(即'b')?你要把剩下的字母都移过来,所以它看起来像这样:

Array position :   [0]  [1]  [2]  [3]  [4]  [5]  [6]  [7]  [8]  [9]
Values in array:    a    l    p    h    a    e    t    i    c    

这正是你看到的for循环所做的。它对结构的每个部分都这样做,有效地将下一个索引中结构的所有部分复制到前一个索引中。

如果我没记错的话,对于结构体来说这是不必要的。我相信你应该可以说houseData[move] = houseData[move+1],它会简单地将后者按位复制到前者中。

编辑

根据评论/讨论,问题是不同的。你只需要在正确的时间跳出你的upper for循环。你还需要设置你的第二个循环不出界。

for (mlsCounter = 0; ((mlsCounter < count) && (mlsDelete == NOT_FOUND));
                             mlsCounter++) {
             if (houseData[mlsCounter].mlsNum == targetMLS) {
                mlsDelete = houseData[mlsCounter].mlsNum; 
                houseData[mlsCounter].mlsNum = 0; 
                houseData[mlsCounter].price = 0;
                houseData[mlsCounter].type = 0;
                houseData[mlsCounter].zip.clear(); 
                houseData[mlsCounter].company.clear();
                houseData[mlsCounter].realty.clear(); 
                break;
              }
      }
   for (move = mlsCounter;move < count - 1; move++){

我认为列表是更适合该任务的数据结构。另外,在任何情况下,如果可能的话,应该考虑使用标准库提供的容器。

std::list<mlsListing> housedata; // so you can easily add stuff at the end, and remove stuff in the middle at low cost
std::remove_if( housedata.begin(), housedata.end(), 
                [&](mlsListing current) -> bool 
                {
                    if (current.mlsNum == targetMLS)
                    {
                        mlsDelete = current.mlsNum;
                        return true;
                    }
                    return false;
                }
              )