将结构体未排序数组中的下标向左移动
shifting indices to the left in a struct unsorted array
我有我的整个程序的这个片段。在这个函数中,特别是在程序的这一部分中,用户输入一个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;
}
)
相关文章:
- 尝试通过多个向量访问变量时,向量下标超出范围
- 矢量下标超出SFML游戏中的范围
- 如何在C++程序中使用下标数字?
- 一组值的零开销下标运算符
- 运行时错误:矢量下标超出范围:正在检查空集
- 如何重载下标运算符 [] 以引用 2d STL 数组?
- 使用boost::multiprecision cpp_int左移时出现超时错误
- 如果变量数据包含大于 vector 所有元素的整数,则仅在视觉工作室上接收"矢量下标超出范围"?
- 矢量无效下标.SFML
- C++ IDE 不会推断/自动完成对模板类中的 std::array 下标表达式的成员访问
- 编译期间矢量下标超出范围
- C++ 调试断言失败 - 矢量下标超出视觉工作室的范围
- 有什么理由不扩展 std::set 以添加下标运算符吗?
- 我想制作设置鼠标回调函数,以便在仍然按下鼠标左键时连续传递坐标
- 警告:左移计数 >= 在 C++ 中将字节流读取为双精度变量时的类型宽度
- 在继承的模板类中使用下标 [] 运算符
- 对矢量来说是全新的.矢量下标超出范围
- 左移<<如何在不同的功能中给出不同的结果?
- 如何重载下标运算符以返回可以是左值的可选值?
- 将结构体未排序数组中的下标向左移动