Delete An Element From An Array

How would I delete "Bloodhound"

 
  std::string LegendArray[] = { "Bloodhound","Gibraltar","Lifeline","Pathfinder","Wraith","Bangalore","Caustic","Mirage" };
Last edited on
Short answer: Overwrite it with the element to the right of it (shifting each affected element), and pretend the size of the array is one less now.

Alternative answer: By changing the array to a vector.
1
2
3
4
5
6
7
8
9
10
11
12
13
// Example program
#include <iostream>
#include <string>
#include <vector>

int main()
{
    std::vector<std::string> LegendArray = { "Bloodhound","Gibraltar","Lifeline","Pathfinder","Wraith","Bangalore","Caustic","Mirage" };
    
    std::cout << LegendArray.size() << std::endl;
    LegendArray.erase(LegendArray.begin()); // erase first element
    std::cout << LegendArray.size() << std::endl;
}


Long Answer:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// Example program
#include <iostream>
#include <string>

void erase_element(std::string arr[], int& size, int index_to_erase)
{
    for (int i = index_to_erase; i < size-1; i++)
    {
        arr[i] = arr[i+1];
    }
    
    size--;
}

void print_array(std::string arr[], int size)
{
    for (int i = 0; i < size; i++)
    {
        std::cout << arr[i] << ' ';   
    }
    std::cout << '\n';
}

int main()
{
     std::string LegendArray[] = { "Bloodhound","Gibraltar","Lifeline","Pathfinder","Wraith","Bangalore","Caustic","Mirage" };
     
     int size = 8;
     erase_element(LegendArray, size, 0);
     print_array(LegendArray, size);
}

Last edited on
1
2
3
4
5
6
7
8
9
10
11
int main() {
  std::string LegendArray[] = { "Bloodhound","Gibraltar","Lifeline","Pathfinder","Wraith","Bangalore","Caustic","Mirage" };

  // The array size originally is 8, but after removing Bloodhound, there will
  // only be 7 legends left. So (i = 0) and shift the elements to the left and
  // stop the loop when (i < 7).
  for(size_t i = 0; i < 7; i++)
    LegendArray[i] = LegendArray[i+1];

  return 0;
}


It's important to note that while it may have seem that you removed an element from the array, the size of the array is still 8. The example I provided is just a removal of an element via shifting.

EDIT:
Ganado beat me to it, it seems. Vectors would be much easier for this type of thing since the STL provides a lot of useful things.
Last edited on
I had edited my answer to provide more detail, so you kinda still beat me with your code.
Last edited on
alternate answers:
1) just kill it. LegendArray[0] = ""; //its gone. when printing, you can have a condition, eg if(LegendArray[x] != "") cout << LegendArray[x]; or the like. this is a sentinel approach.

2) if order does not matter, you can swap it with the last one and delete it there.

3) you can create another construct that refers to the original and remove the pointer for that element.

4) one of our gurus would have to help me here; I am still trying to learn this one, but there is that move semantics stuff … probably a way to exploit that to avoid the copies.

these have something in common: avoid the data copy if you can, its the slowest way. 3 is the time-space tradeoff approach (wastes space to save time). Depends on what you need.
Last edited on
Topic archived. No new replies allowed.