I have read many different threads about how it is not possible to delete values from an array, but I need to find some way to do this.

I am making a program that involves a bottom up approach algorithm where it pairs off data segments, finds the best segments to merge and then merges them. I have it telling me which segments it should merge but then to merge them I need to be able to go in a remove certain values from that array. I can find them by the index or actual value I suppose.

ex

t[]={1,2,3,4,5,6,7}

and I need to remove 4 and 5 and then send the array back through all my functions.


Thanks!@

Recommended Answers

All 10 Replies

Have you thought about using vectors?

If the array in question is small, can you create a copy with only the values you want to use?

t[]={1,2,3,4,5,6,7}

and I need to remove 4 and 5 and then send the array back through all my functions.

So when you 'delete' 4 & 5, what does your array look like? t[]={1,2,3,-1,-1,6,7} -- if so, you can probably figure out how to do this. t[]={1,2,3,6,7} -- if so, you can probably figure out how to do this too.

I have thought about vectors but I would rather find a way to do this with arrays. Copying is a thought, I will look into it

WaltP,

I would like it to look like the latter.

t[]={1,2,3,6,7}

You really have no choice but to shift elements and overwrite the elements to be deleted:

Original: {1,2,3,4,5,6,7}
Delete 4: {1,2,3,5,6,7,7}
Delete 5: {1,2,3,6,7,7,7}

The size of the array won't change though (I gave an example of how that might look with the black elements), so you need to maintain a separate count of legit items and not exceed that.

Hmmm alright, I was hoping there was a way to just remove them and in turn change the size as well. I will have to do some thinking. Thank you

Depending on how you get the values into your array, you could possibly use dynamic allocation, but there will be an efficiency hit because of the extra allocation and copying operations.

I get those values from reading in a CSV

I assume use find algorithm then then distance algorithm and then do dinamic realocation and when start palying with pointer this appoatch will be goo for one time, if it takes more then one realocation should make a class for this or wrap this array, but then why to invent this new stuff while you can get all this with...

Is there an indicator in the CSV that says how many values there are? If so, you should be able to read that indicator then allocate the appropriate number of elements:

ifstream CSVin ("input.csv", ios::in);
int elementCount = 0;
CSVin >> elementCount;
int *values = new int[elementCount];

int readValue;
int currentIndex = 0;
while(CSVin >> (values[currentIndex])) {
  currentIndex++;
}

//remaining needed actions
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.