www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Array remove 1 item? (std.container)

reply "Damian" <damianday hotmail.co.uk> writes:
Hi, I've got the jist of using most of std.container.Array, but
I can't seem to remove a single item, I understand I must remove
a range.

Array!int arr;
arr.insert([1, 2, 3, 4, 5]);

So now how would I remove lets say the number 3 from my array in 
the most efficient way? which would leave me with [1, 2, 4, 5] ??

For reference I'm using an Array to replace a C++ std::vector..
Jan 09 2013
parent reply "Robert" <krokro6 hotmail.com> writes:
On Wednesday, 9 January 2013 at 15:06:01 UTC, Damian wrote:
 Hi, I've got the jist of using most of std.container.Array, but
 I can't seem to remove a single item, I understand I must remove
 a range.

 Array!int arr;
 arr.insert([1, 2, 3, 4, 5]);

 So now how would I remove lets say the number 3 from my array 
 in the most efficient way? which would leave me with [1, 2, 4, 
 5] ??

 For reference I'm using an Array to replace a C++ std::vector..
Hi, you can do arr.linearRemove(arr[2..3]); but i dont know if its the best way...
Jan 09 2013
next sibling parent reply "Damian" <damianday hotmail.co.uk> writes:
On Wednesday, 9 January 2013 at 15:20:40 UTC, Robert wrote:
 On Wednesday, 9 January 2013 at 15:06:01 UTC, Damian wrote:
 Hi, I've got the jist of using most of std.container.Array, but
 I can't seem to remove a single item, I understand I must 
 remove
 a range.

 Array!int arr;
 arr.insert([1, 2, 3, 4, 5]);

 So now how would I remove lets say the number 3 from my array 
 in the most efficient way? which would leave me with [1, 2, 4, 
 5] ??

 For reference I'm using an Array to replace a C++ std::vector..
Hi, you can do arr.linearRemove(arr[2..3]); but i dont know if its the best way...
Ah yes this I've been doing and its working as expected, I was not sure if i was doing it the correct way though.
Jan 09 2013
parent reply "monarch_dodra" <monarchdodra gmail.com> writes:
On Wednesday, 9 January 2013 at 15:29:50 UTC, Damian wrote:
 On Wednesday, 9 January 2013 at 15:20:40 UTC, Robert wrote:
 On Wednesday, 9 January 2013 at 15:06:01 UTC, Damian wrote:
 Hi, I've got the jist of using most of std.container.Array, 
 but
 I can't seem to remove a single item, I understand I must 
 remove
 a range.

 Array!int arr;
 arr.insert([1, 2, 3, 4, 5]);

 So now how would I remove lets say the number 3 from my array 
 in the most efficient way? which would leave me with [1, 2, 
 4, 5] ??

 For reference I'm using an Array to replace a C++ 
 std::vector..
Hi, you can do arr.linearRemove(arr[2..3]); but i dont know if its the best way...
Ah yes this I've been doing and its working as expected, I was not sure if i was doing it the correct way though.
Yeah, that's pretty much the correct way. To do it. BTW: Have you tried giving standard arrays and slices a chance before going to Array? Unless you absolutely need deterministic RAII, you shouldn't need Array.
Jan 09 2013
parent "Damian" <damianday hotmail.co.uk> writes:
On Wednesday, 9 January 2013 at 15:40:48 UTC, monarch_dodra wrote:
 On Wednesday, 9 January 2013 at 15:29:50 UTC, Damian wrote:
 On Wednesday, 9 January 2013 at 15:20:40 UTC, Robert wrote:
 On Wednesday, 9 January 2013 at 15:06:01 UTC, Damian wrote:
 Hi, I've got the jist of using most of std.container.Array, 
 but
 I can't seem to remove a single item, I understand I must 
 remove
 a range.

 Array!int arr;
 arr.insert([1, 2, 3, 4, 5]);

 So now how would I remove lets say the number 3 from my 
 array in the most efficient way? which would leave me with 
 [1, 2, 4, 5] ??

 For reference I'm using an Array to replace a C++ 
 std::vector..
Hi, you can do arr.linearRemove(arr[2..3]); but i dont know if its the best way...
Ah yes this I've been doing and its working as expected, I was not sure if i was doing it the correct way though.
Yeah, that's pretty much the correct way. To do it. BTW: Have you tried giving standard arrays and slices a chance before going to Array? Unless you absolutely need deterministic RAII, you shouldn't need Array.
I have not, I've tried to choose the closest container equivalent to std::vector.
Jan 09 2013
prev sibling parent "Jonathan M Davis" <jmdavisProg gmx.com> writes:
On Wednesday, January 09, 2013 16:20:39 Robert wrote:
 On Wednesday, 9 January 2013 at 15:06:01 UTC, Damian wrote:
 Hi, I've got the jist of using most of std.container.Array, but
 I can't seem to remove a single item, I understand I must remove
 a range.
 
 Array!int arr;
 arr.insert([1, 2, 3, 4, 5]);
 
 So now how would I remove lets say the number 3 from my array
 in the most efficient way? which would leave me with [1, 2, 4,
 5] ??
 
 For reference I'm using an Array to replace a C++ std::vector..
Hi, you can do arr.linearRemove(arr[2..3]); but i dont know if its the best way...
Either that or use find and take. arr.linearRemove(take(find(arr[], elementValueToRemove), 1)); But if you're trying to remove by index, then arr[2 .. 3] makes more sense. If it were a container without random access though, you'd be stuck using find or popFrontN to to get to the element that you want to remove, and then you'd use take to get the first element only (or however many elements you wanted to remove). - Jonathan M Davis
Jan 09 2013