Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/136.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ c++;删除结构数组中的元素_C++_Arrays_Struct - Fatal编程技术网

C++ c++;删除结构数组中的元素

C++ c++;删除结构数组中的元素,c++,arrays,struct,C++,Arrays,Struct,下面的代码接受用户对2个属性的输入并将其存储。我一直未能找到一种方法来删除从市场上删除的房产信息,方法是提示用户输入要删除的房产ID,并更新房产详细信息,例如通过提示用户输入房产ID来更改要价。我的老师建议使用结构数组,到目前为止我已经设法做到了 #include <iostream> // takes users input and prints out output #include<co

下面的代码接受用户对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<<"\n\n 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`

   }
}
#include//接受用户输入并打印输出
#include//声明了几个用于执行“控制台输入和输出”的有用库函数
#包含//编译器指令,该指令存储标准输入输出
使用命名空间std;//定义标准命名空间
struct Property//将结构声明为属性
{
int ID[3];//声明数据类型INTGER的变量“ID”以标识属性ID
int Asking_Price;//声明数据类型integer的变量“Asking_Price”,以标识属性的要价
char Type[10];//声明数据类型char的变量“Type”以标识属性类型(detatched、semi-detatched、tierated、flat、commercial)
char Address[20];//声明数据类型char的变量“Address”以标识属性地址
};
int main()
{
属性My_属性[2];//声明结构、数组和数组元素数

对于(int i=0;i您实际上无法从数组中删除项,因为数组具有固定大小。从固定大小容器中删除项的常用方法是将所有后续元素向下移动一个空格,以覆盖您不想要的项

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;
地产道具[10];
int prop_count=0;
//以某种方式填充道具,每次添加道具时增加道具计数
std::cout>删除;
for(int i=删除+1;i
这或多或少就是
std::vector
在封面下的工作方式

如果不关心保持所有元素的连续性,可以添加一些方法将
属性标记为已删除,但这往往会导致更复杂的代码


最后,您可能应该使用一个可重新调整大小的容器,如
std::vector
,此时您可以调用
std::vector::erase
,从中删除项目。

我在代码中找不到任何需要
delete
的内容。您的老师应该推荐一个容器类,如
std::vector
,并且没有t哑数组。使用数组时没有“删除”这样的事情,因为数组无法调整大小。
cin>>My_属性[k].ID[3];
正在访问超出数组边界的内容。未定义的行为。@当您的数组中有2个项目时,无论您尝试什么技巧,您都有2个项目。如果您继续使用数组,您需要另一个变量来跟踪条目的实际数量。当您删除这些项目时,使用像vector这样的容器实际上会删除这些项目哼哼,不是像在数组中那样假擦除它们。你需要一个
int
来说明有多少个有效条目。我马上看到的问题是,如果你“删除”第一个条目,你需要知道有一个条目,而第二个条目是有效的,而不是第一个。然后你必须“上移”所有用来掩盖漏洞的条目都创建了。换句话说,你正在做一些向量所做的事情。谢谢@miles budnek我想我会重写代码并使用向量