c++ - 在结构未排序数组中将索引向左移动

标签 c++ arrays struct

我有整个程序的这个片段。在这个函数中,特别是在这段程序中,用户输入一个 MLS# 来从数组中删除一个家。第一个“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++)

关于c++ - 在结构未排序数组中将索引向左移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15778318/

相关文章:

c++ - VC++ 错误 : error C1083: Cannot open source file: '=0x0401' : No such file or directory

c++ - 我如何在我的窗口小部件窗口外接收或处理 mouseMoveEvent(s)?

java - 将 env var 从 java 传递到 c++

arrays - 在值序列中找到局部最大值

c++ - C/C++ 中的结构赋值是原子的吗?

c++ - C++线程创建/删除与线程停止/恢复

ios - CollectionView numberOfItemsInSection 变量

javascript - 将项目推送到对象键的值数组

c - 从结构中添加或删除数据

c - 动态分配结构 vector 内存