Home > Enterprise >  C removing a element in string array which appears 5 times
C removing a element in string array which appears 5 times

Time:01-17

Here is the code I have, I am having trouble finding a way to remove all Apple elem in the array. I am able to count the apples in the array. I hope someone can help...

    string items[10] = { "Apple", "Oranges", "Pears", "Apple", "bananas", "Apple", "Cucumbers", "Apple", "Lemons", "Apple" };
     

    //Counts the total amount of apples
    int n = sizeof(items) / sizeof(items[0]);
    cout << "Number of times Apple appears : "
        << count(items, items   n, "Apple");

    //remove the element Apple from array
    if (string items[].contains("Apple"))
    {
        items[].remove("Apple");
    }

CodePudding user response:

Some options you'd have would be:

  1. Walk your array of items and substitute your "Apple" strings for empty strings.
  2. Use a std::vector of strings and whether a) initialize it with the array of items and then call std::erase_if (C 20) to remove the "Apple" strings, or b) initialize it without elements and then call std::copy_if together with std::back_inserter to append the non-"Apple" strings.

[Demo]

#include <algorithm>  // copy_if, transform
#include <iostream>  // cout
#include <string>
#include <vector>  // erase_if

int main()
{
    {
        std::string items[10] = { "Apple", "Oranges", "Pears", "Apple", "bananas", "Apple", "Cucumbers", "Apple", "Lemons", "Apple" };
        std::transform(std::begin(items), std::end(items), std::begin(items), [](auto& s) {
            return (s == "Apple" ? "" : s); 
        });
        for (const auto& s : items) { std::cout << s << ", "; }
        std::cout << "\n";
    }

    {
        const std::string items[10] = { "Apple", "Oranges", "Pears", "Apple", "bananas", "Apple", "Cucumbers", "Apple", "Lemons", "Apple" };
        std::vector<std::string> v{std::cbegin(items), std::cend(items)};
        std::erase_if(v, [](auto & s) { return s == "Apple"; });
        for (const auto& s : v) { std::cout << s << ", "; }
        std::cout << "\n";
    }

    {
        const std::string items[10] = { "Apple", "Oranges", "Pears", "Apple", "bananas", "Apple", "Cucumbers", "Apple", "Lemons", "Apple" };
        std::vector<std::string> v{};
        std::copy_if(std::cbegin(items), std::cend(items), std::back_inserter(v), [](auto& s) {
            return s != "Apple";
        });
        for (const auto& s : v) { std::cout << s << ", "; }
    }
}

// Outputs:
//
//   , Oranges, Pears, , bananas, , Cucumbers, , Lemons, , 
//   Oranges, Pears, bananas, Cucumbers, Lemons, 
//   Oranges, Pears, bananas, Cucumbers, Lemons, 

CodePudding user response:

The simplest and most efficient way to remove a number of elements in an array is to create a new array:

#include <iostream>

using namespace std;

int main()
{
    string items[10] = { "Apple", "Oranges", "Pears", "Apple", "bananas", "Apple", "Cucumbers", "Apple", "Lemons", "Apple" };
    
    string new_items[10];
    int new_size = 0;
    
    // construct new array
    for (int i = 0; i < sizeof(items)/sizeof(string); i  ) {
        if ("Apple" != items[i]) {
            new_items[new_size  ] = items[i];
        }
    }

    // print new array
    for (int i = 0; i < new_size; i  ) {
        cout << new_items[i] << " ";
    }
    cout << endl;

    return 0;
}
  •  Tags:  
  • Related