Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature req: being able to use MFC Collections in std algorithms #1

Open
jpj-verstappen opened this issue Jan 21, 2020 · 1 comment

Comments

@jpj-verstappen
Copy link

Nice work on the range based for loops! Working like a charm.
I would like to use the MFC collection classes in the std algorithms, like std::all_of. Looks like I need an iterator for the begin and the end of the array I'm using (CTypedPtrArray).
Can someone give an example of how to implement this?

Usage example:

CTypedPtrArray<CObArray, CAppointment*> appointmentArr;
bool bAllAppointmentsToday = std::all_of(...begin..., ....end..., 
    [](CAppointment* app) { return app->IsToday(); });
assert(bAllAppointmentsToday);
@mariusbancila
Copy link
Owner

mariusbancila commented May 14, 2020

This just works with begin() and end().

Here is an example:

CTypedPtrArray<CObArray, IntObject*> arr;
arr.Add(new IntObject(1));
arr.Add(new IntObject(3));
arr.Add(new IntObject(5));
arr.Add(new IntObject(7));

bool all = std::all_of(begin(arr), end(arr),
   [](IntObject* o) { return o->value % 2 == 1; });
Assert::IsTrue(all);

for (auto p : arr)
   delete p;

where IntObject is

struct IntObject : CObject
{
   int value;

   explicit IntObject(int const v) noexcept : value(v) {}

   explicit operator int()
   {
      return value;
   }
};

You can find examples in the unit tests project.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants