c++中删除struct数组中的元素
c++ deleting elements in a struct array
下面的代码接受2个属性的用户输入并存储它。我一直未能找到一种方法来删除信息的属性,如果它被从市场上删除,提示用户输入要删除的属性id,也更新的属性详细信息,如改变出价提示用户输入属性id。我的老师建议我使用结构数组,到目前为止我已经做到了。
#include <iostream> // takes users input and prints out output
#include<conio.h> // declares several useful library functions for performing "console input and output"
#include <stdio.h> // compiler directive which stores standard input output
using namespace std; // defines the standard namespace
struct Property // declares the struct as Property
{
int ID[3]; // declare variable "ID" of data type interger to identify property ID
int Asking_Price; // declare variable "Asking_Price" of data type interger to to identity the asking price for the property
char Type[10]; // declare variable "Type" of data type char to identify the property type (detatched, semi-detatched, terraced, flat, commercial)
char Address[20]; // declare variable "Address" of data type char to identify the property address
};
int main()
{
Property My_Property[2]; // declare the struct, the array and the number of array elements
for (int i=0;i<2;i++) // for loop counts the number of properties from 1 to 5
{
cout << "n Details of property ID " << i + 1 << " are :n"; // Prints out the property ID
cout << "t Enter the property asking price : "; // Prompts the user to enter asking price fopr the property
cin >> My_Property[i].Asking_Price; // Takes users input and stores it in the array
cout << "t Please enter the property type: "; //Prompts the user to enter the property type
cin>>My_Property[i].Type; //Takes users input and stores it in the array
cout << "t Please enter the property address : "; //Prompts the user to enter the property address
cin >> My_Property[i].Address; //Takes users input and stores it in the array
}
for (int j = 0;j<2;j++) // for loop counts the property information for all properties entered
{
cout << "n Information on property number " << j + 1 << " is :n"; //Prints out the stored property ID
cout << "t Asking Price :" << My_Property[j].Asking_Price<<endl; //Prints out the stored asking price for the property
cout << "t Property Type : " << My_Property[j].Type<<endl; //Prints out the stored property type
cout << "t Address : " << My_Property[j].Address<<endl; //Prints out the stored address for the property
}
for (int k=0;k<2;k++)
{
cout << "n Please enter the property ID for the property you would like to delete : n ";
cin>> My_Property[k].ID[3];
if(My_Property[k].ID[3]>2)
{
cout<<"nn This property ID does not exist, press ENTER to try again: n";
}
else
{
//trying to figure out the delete code
}
_getch(); //read characters from screen`enter code here`
}
}
实际上不能从数组中删除项,因为数组有固定的大小。从固定大小的容器中删除项的通常方法是将所有后续元素下移一个空格以覆盖不需要的项。
Property props[10];
int prop_count = 0;
// Fill props somehow, incrementing prop_count each time you add an item
std::cout << "n Please enter the property ID for the property you would like to delete : n ";
int to_delete;
std::cin >> to_delete;
for (int i = to_delete + 1; i < prop_count; ++to_delete) {
props[i - 1] = props[i];
}
prop_count -= 1;
这或多或少是std::vector
在掩护下的工作方式。
如果您不关心保持所有元素连续,您可以添加一些方法来将Property
标记为已删除,但这往往会导致更复杂的代码。
最后,您可能应该使用一个可调整大小的容器,如std::vector
,此时您可以调用std::vector::erase
来擦除其中的项目。
相关文章:
- Mongodb c++驱动程序:如何查询元素的数组
- 将数组作为参数传递给函数安全吗?作为第三方职能部门,可以探索他们想要的之外的其他元素
- 使用strcpy将char数组的元素复制到另一个数组
- 如何将元素添加到数组的线程安全函数?
- cpp二进制搜索问题,计算给定数组中输入元素的出现次数
- 输出没有重复元素的动态数组(收缩数组)C++
- 数组中最大的非重复元素
- 数组元素打印的递归方法
- 对字符数组中的元素执行逐位操作
- 缓存std::数组的选定元素,并在c++中自动保持其一致性
- 如何计算数组中元素的位数?(不是数组的长度),并计算其数字的总和
- 通过交换元素使数组相同
- 按平均值替换数组中的元素
- 打印矢量数组中的所有元素
- 如何将字节数组元素替换为修改的十六进制 ASCII 符号?
- 如何为 c++ 的不同变量类型的结构元素创建动态数组?
- 如何将元素从向量转移到新数组?
- 如何在向量数组中插入元素?
- 为什么 2 个相同数组的元素彼此相等
- C++ 中的二维整数数组,每行中的元素数量不均匀