Chrisshan Reyvallee
Chrisshan Reyvallee

Reputation: 13

Is there any way to delete one element of an array of structure?

For example as shown in the code. I got structure called tutor and after that I initialize the code tutor lecturer[1000]; Is there any way to delete lecturer[1] besides manually assign NULL to the every attributes in lecturer[1]? or is there some code which look like this "lecturer[1] = NULL" ? Thank youu for taking time to read and answering.

Note: assume that I already initialize and fill all the attributes for lecturer[0], lecturer[1] and lecturer[2].

struct tutor {

    int tutorID;
    string name;
    string date_Joined;
    string date_Terminated;
    double hourlyPayRate;
    string phone;
    string address;
    int center_Code;
    string center_Name;
    int subject_Code;
    string subject_Name;
    int rating;

};
.
.
.
tutor lecturer[1000];

Upvotes: 1

Views: 89

Answers (1)

Scheff's Cat
Scheff's Cat

Reputation: 20141

There is no way to delete an element from an array.

The only possibility is to move all following elements one index down. Thereby, it should be remarked somewhere that the number of used elements has been decremented by 1 because the size of an array cannot be changed as well.

(A better replacement would be a std::vector, of course.)

Demo of how to remove an element from an array:

#include <iostream>
#include <string>
struct tutor {
  int tutorID;
  std::string name;
};

std::ostream& operator<<(std::ostream &out, const tutor &entry)
{
  return out << "ID: " << entry.tutorID << ", name: " << entry.name;
}

int main()
{
  tutor lecturer[1000];
  size_t n = 0;
  // fill array
  lecturer[n++] = { 1, "Klaus" };
  lecturer[n++] = { 2, "Dieter" };
  lecturer[n++] = { 3, "Barbara" };
  lecturer[n++] = { 4, "Elisabeth" };
  // print array
  for (size_t i = 0; i < n; ++i) {
    std::cout << i << ": " << lecturer[i] << '\n';
  }
  // remove element 1 (He got COVID-19.)
  size_t j = 1;
  std::cout << "Remove " << j << ": " << lecturer[j] << '\n';
  for (size_t i = j + 1; i < n; ++i) {
    lecturer[i - 1] = lecturer[i];
  }
  --n; // remark that array has been shortened by 1
  // print array
  for (size_t i = 0; i < n; ++i) {
    std::cout << i << ": " << lecturer[i] << '\n';
  }
}

Output:

0: ID: 1, name: Klaus
1: ID: 2, name: Dieter
2: ID: 3, name: Barbara
3: ID: 4, name: Elisabeth
Remove 1: ID: 2, name: Dieter
0: ID: 1, name: Klaus
1: ID: 3, name: Barbara
2: ID: 4, name: Elisabeth

Live Demo on coliru

Note:

This kind of removal has O(N) (worst case).

It can be achieved faster by moving the last element to the place of the removed. This has O(1) (every case) but can be used only if the order of elements is not important.

In this case, the removal would be:

  // remove element 1 (He got COVID-19.)
  size_t j = 1;
  std::cout << "Remove " << j << ": " << lecturer[j] << '\n';
  lecturer[j] = lecturer[--n];

Output:

0: ID: 1, name: Klaus
1: ID: 2, name: Dieter
2: ID: 3, name: Barbara
3: ID: 4, name: Elisabeth
Remove 1: ID: 2, name: Dieter
0: ID: 1, name: Klaus
1: ID: 4, name: Elisabeth
2: ID: 3, name: Barbara

Live Demo on coliru

Upvotes: 1

Related Questions