From a2ebdd4baa81d21a570792e6895ed8384e43c9c4 Mon Sep 17 00:00:00 2001 From: Kitsune Ral Date: Fri, 27 Jul 2018 12:10:14 +0900 Subject: util.h: findFirstOf A spin on the standard algorithm. --- lib/util.h | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'lib/util.h') diff --git a/lib/util.h b/lib/util.h index c491ff89..7769abce 100644 --- a/lib/util.h +++ b/lib/util.h @@ -191,6 +191,24 @@ namespace QMatrixClient iterator to; }; + /** A replica of std::find_first_of that returns a pair of iterators + * + * Convenient for cases when you need to know which particular "first of" + * [sFirst, sLast) has been found in [first, last). + */ + template + inline std::pair findFirstOf( + InputIt first, InputIt last, ForwardIt sFirst, ForwardIt sLast, + Pred pred) + { + for (; first != last; ++first) + for (auto it = sFirst; it != sLast; ++it) + if (pred(*first, *it)) + return std::make_pair(first, it); + + return std::make_pair(last, sLast); + } + /** A guard pointer that disconnects an interested object upon destruction * It's almost QPointer<> except that you have to initialise it with one * more additional parameter - a pointer to a QObject that will be -- cgit v1.2.3