1

for eg

input name:jacob
input id:1234

input name:dani
input id:444

enter id to delete data :444
data deleted succesfully ;

after that display the remaining data

#include <iostream>
using namespace std;
  class student{
   char name[20];
   int id;
   public:
    int search(int k);
    void adddata(){
     cout<<"input name :";
     cin>>name;
     cout<<"input id :";
     cin>>id;
    }
   void display(){
    cout<<name<<endl;
    cout<<id<<endl;
}
    
};

          }
    int main(){
        int d;
        student data[5];
        for(int i=0;i<5;i++){
            data[i].adddata();
        }
        
        cout<<"enter id to delete data :";
        cin>>d;
        
        
        }
}
1
  • 1
    An array is not a good container for such purposes. Use std::list. Commented Jul 31, 2021 at 16:27

1 Answer 1

4

how to delete one record from array of object in c++

It isn't possible to erase element of an array. An array has a constant number of elements through its lifetime.

The C++ standard library provides an implementation of "resizable array" data structure. It's called std::vector. You can erase from it using the erase member function.

However, since you appear to be searching elements by arbitrary "id", an associative data structure would be more efficient for that use case, such as std::unordered_set.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.