Home » U++ Library support » U++ Core » Array : swapping element with a pointer
Array : swapping element with a pointer [message #33756] |
Sat, 10 September 2011 21:30 |
mdelfede
Messages: 1307 Registered: September 2007
|
Ultimate Contributor |
|
|
Hi,
I'd need something like this :
Array[A] array;
A * a = new A;
array.Swap(someIndex, a);
where Array::Swap() should be so :
void Array<A>::Swap(int idx, A*& elem);
This would allow swapping contents of single elements of different arrays without insertion/deletion steps; example :
Array<A> arr1, arr2;
arr1.Add(new A);
arr1.Add(new A);
arr2.Add(new A);
arr2.Add(new A);
A *a = NULL;
arr1.Swap(1, a);
arr2.Swap(1, a);
arr1.Swap(1, a);
That one because I guess removing and then inserting an element in a 10000 elements array just to swap a couple of them would be quite slow....
A more nice way would be some sort of Detach() Set() that don't drop the element but replace it with a dummy (maybe null...) pointer. That would be :
Array<A> arr1, arr2;
arr1.Add(new A);
arr1.Add(new A);
arr2.Add(new A);
arr2.Add(new A);
A *a = arr1.NoDropDetach(1);
A *b = arr2.NoDropDetach(1);
arr1.NoInsertSet(1, b);
arr2.NoInsertSet(1, a);
Max
|
|
|
|
|
|
|
|
Goto Forum:
Current Time: Sat Jun 15 23:10:14 CEST 2024
Total time taken to generate the page: 0.02586 seconds
|