www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Removing an element from an array

reply Alexander Zhirov <azhirov1991 gmail.com> writes:
I have an array of self-written class `A`. I'm sorry for my 
tactlessness, but I'm confused about the modules. How do I 
correctly find a specific object `fragment` inside the array and 
delete it? I don't quite understand which modules to use to do 
this optimally.

```d
A[] arr;
A fragment = new A;
...
arr.remove(fragment);  // Something like
```

In the pros, I would do it this way, for example via lambda

```c++
arr.erase(std::find_if(arr.cbegin(), arr.cend(), [&](const 
std::reference_wrapper<A> &item)
{
     return &item.get() == &fragment;
}));
```
Nov 10 2022
parent reply "H. S. Teoh" <hsteoh qfbox.info> writes:
On Thu, Nov 10, 2022 at 11:26:45PM +0000, Alexander Zhirov via
Digitalmars-d-learn wrote:
 I have an array of self-written class `A`. I'm sorry for my tactlessness,
 but I'm confused about the modules. How do I correctly find a specific
 object `fragment` inside the array and delete it? I don't quite understand
 which modules to use to do this optimally.
 
 ```d
 A[] arr;
 A fragment = new A;
 ...
 arr.remove(fragment);  // Something like
 ```
I would do something like this: // Warning: untested code import std.algorithm; arr = arr.remove(arr.countUntil(fragment)); T -- Change is inevitable, except from a vending machine.
Nov 10 2022
parent reply Alexander Zhirov <azhirov1991 gmail.com> writes:
On Thursday, 10 November 2022 at 23:36:29 UTC, H. S. Teoh wrote:
 On Thu, Nov 10, 2022 at 11:26:45PM +0000, Alexander Zhirov via 
 Digitalmars-d-learn wrote:
 I have an array of self-written class `A`. I'm sorry for my 
 tactlessness, but I'm confused about the modules. How do I 
 correctly find a specific object `fragment` inside the array 
 and delete it? I don't quite understand which modules to use 
 to do this optimally.
 
 ```d
 A[] arr;
 A fragment = new A;
 ...
 arr.remove(fragment);  // Something like
 ```
I would do something like this: // Warning: untested code import std.algorithm; arr = arr.remove(arr.countUntil(fragment)); T
As always - simple and compact. Thank you:)
Nov 10 2022
parent reply Alexander Zhirov <azhirov1991 gmail.com> writes:
On Friday, 11 November 2022 at 00:02:09 UTC, Alexander Zhirov 
wrote:
```d
import std.algorithm;
arr = arr.remove(arr.countUntil(fragment));
```
And will this method work? ```d A[] arr; A fragment = new A; ... remove(current => current == fragment)(arr); ```
Nov 10 2022
parent reply Alexander Zhirov <azhirov1991 gmail.com> writes:
On Friday, 11 November 2022 at 05:36:37 UTC, Alexander Zhirov 
wrote:
 On Friday, 11 November 2022 at 00:02:09 UTC, Alexander Zhirov 
 wrote:
```d
import std.algorithm;
arr = arr.remove(arr.countUntil(fragment));
```
And will this method work? ```d A[] arr; A fragment = new A; ... remove(current => current == fragment)(arr); ```
And it will be even more accurate so as not to cause an error: ```d A[] arr; A fragment = new A; ... arr = remove(current => current == fragment)(arr); ```
Nov 10 2022
parent ag0aep6g <anonymous example.com> writes:
On Friday, 11 November 2022 at 06:10:33 UTC, Alexander Zhirov 
wrote:
 On Friday, 11 November 2022 at 05:36:37 UTC, Alexander Zhirov 
 wrote:
 On Friday, 11 November 2022 at 00:02:09 UTC, Alexander Zhirov 
 wrote:
```d
import std.algorithm;
arr = arr.remove(arr.countUntil(fragment));
```
And will this method work? ```d A[] arr; A fragment = new A; ... remove(current => current == fragment)(arr); ```
And it will be even more accurate so as not to cause an error: ```d A[] arr; A fragment = new A; ... arr = remove(current => current == fragment)(arr); ```
You forgot the exclamation mark. And UFCS allows you to put `arr` in front, making it a bit easier on the eyes: arr = arr.remove!(current => current == fragment);
Nov 11 2022