closed

Pages: 12
vectors interesting...
I tried using vectors, no errors, but print still same result ,nothing deleted

vector<Structure> array;
array.erase(array.begin() + row);
In your code you have omitted most important: how array is filled?
Make sure that you are operating on same container you filled earlier.
http://coliru.stacked-crooked.com/a/7de64a0c23b4c431
Last edited on
Structure* array
vector<Element> array(size);

array.erase(array.begin() + row);

Element *s = &array[0];

The issue is with this, the variable array conflict
Last edited on
vector<Element> array(noElements); Creates a vector with noElements default constructed Elements.

And you have a name conflict: you cannot have two entities with same name.

Use arrays or vector, not boths. And certainly do not mix it like that (aside from special cases of interprocess comunication). Choose one and run with it.

You should consider scope of your variables and which one woukd be used. In that example there is nothing which would suggest that vector would contain our Elements. We wrote them into C-array, not in vector. So they will not magically appear in vector.
so previous i have use array, so i couldnt use vector

S* array = new S[size];
for (int i = 0; i < size; ++i) {
s[i].a = rand ();
s[i].b = rand ();
s[i].c = rand ();
}

is there a way to delete for array?
I have already posted:

if you want to delete element 2, then copy element 3 to where element 2 was, element 4 when element 3 was, etc. Effectively you would shift part of the array to the left. In this case last element would be excess, and you would just need to forget about it (? on example below).
size = 6---\
1 2 3 4 5 6
1 2 4 5 6 ?
size = 5-/


Another way is to create another array with size 1 less than original and copy all elements aside from deleted one:
original:
1 2 3 4 5 6
new:
? ? ? ? ?
----
original:
1 2 3 4 5 6
new:
1 2 4 5 6
Then you can delete orignal array.
as I have constructed previously using array,
is it possible to now for delete I use a vector taking from Structure to delete the particular row?

I have issue coming up deleting the part of array and cover it up
as I have constructed previously using array,
is it possible to now for delete I use a vector taking from Structure to delete the particular row?
Lets refrese it: "I have parked my car in repair shop in New York, can it be fixed in Chicago?
Yes, but you will have to drive (or tow) it to Chicago, and then back (unless you fine with living in Chicago).

You cannot: vector and array are two different entities with no relation to each other.
You can create a vector, copy all data into it and remove a row. However data in array will not change, you will have to copy it back if you want it in array. It is terribly unefficient (you just copied your data twice) and if you want to use vector to do so, use it from the begining.

to "remove" elemet from array, you need to shift elements after deleted:
1
2
3
4
5
6
Element* arr = new Element[size];
//...
// to delete element n:
for(int i = n + 1; i < size; ++i)
    arr[i-1] = arr[i]; //Shifting element to the left
--size; //Decrease size to reflect new logical amount of elements. 
Last edited on
Thank you for your explaination.

Am I right say this ,
what i intent to do now is using row (find the particular row) and delete it away

Structure* array // from previous construct array

for(int i = row + 1; i < size; ++i)
{
array[row-1] = array[i]; //Shifting element to the left
--size;
}

then i print out the array, still same but is the logic right?
Yes. Pretty much same logic.
thanks , may i check how do i print out the array after this as size is --, so size is nolonger size and i do not know the actual size

print:
Structure*s = &array [0];

for (int i = 1; i <= size; i++)
{
cout << "(" << s ->a << ", "
<< s ->b << ", "
<< s ->c << ")";
cout << endl;
++s;
}
Topic archived. No new replies allowed.
Pages: 12