Hi, is there any function or algorithm that can completely remove a string array element?

For example,

string mystring = 'x', 'xx', 'xx', 'xxx';

if I want to remove 'xx' and I know the position of 'xx' in the array, I want this :

mystring = 'x', 'xxx';

Anything? I think I made it pretty clear? I unsuccessfully tried to just move the unwanted elements towards the end but apparantly, for my problem it didn't work out and ended up causing more problems...

Your syntax is incorrect. Those aren't strings. Nor are they string arrays.

You can't really delete an element in an array. The space has already been set aside. If you have an array of four strings:

mystring[0] = "x";
mystring[1] = "xx";
mystring[2] = "xx";
mystring[3] = "xxx";

You can "delete" elements 1 and 2 by overwriting them and moving element 3 up to element 1:

mystring[0] = "x";
mystring[1] = "xxx";
mystring[2] = "";
mystring[3] = "";

Or have some integer counter like numElements and don't bother to overwrite elements 2 and 3.

string mystring[4];
int numElements = 4;
mystring[0] = "x";
mystring[1] = "xx";
mystring[2] = "xx";
mystring[3] = "xxx";

// "delete" elements 2 and 3

mystring[0] = "x";
mystring[1] = "xxx";
numElements = 2;

Elements 2 and 3 are still there, though. You're just setting up a variable so that you don't use them. "Delete" is in quotes because there are a lot of ways you could define "delete". As far as the program/computer is concerned, the array still has four elements, not two.

Comments
thanks!

Or something like this?

#include <iostream>
#include <string>

int main()
{
   std::string text("foobarbaz"), sub("bar");
   std::cout << "text = " << text << "\n";
   std::string::size_type loc = text.find(sub);
   if ( loc != std::string::npos )
   {
      text.erase(loc, sub.length());
      std::cout << "text = " << text << "\n";
   }
}

/* my output
text = foobarbaz
text = foobaz
*/

VernonDozier: Thanks, I pretty much did the same thing except I just made whatever I wanted to delete equal to "$" and then ignored that element and luckily that worked.

Thanks all for posting!

This question has already been answered. Start a new discussion instead.