Add a find_if template for pointer-based containers.
authorcantor <cantor@de75baf8-a10c-0410-a50a-987c0e22f00f>
Thu, 8 Nov 2007 02:29:39 +0000 (02:29 +0000)
committercantor <cantor@de75baf8-a10c-0410-a50a-987c0e22f00f>
Thu, 8 Nov 2007 02:29:39 +0000 (02:29 +0000)
git-svn-id: https://svn.middleware.georgetown.edu/cpp-xmltooling/trunk@418 de75baf8-a10c-0410-a50a-987c0e22f00f

xmltooling/util/Predicates.h

index 4f92439..baf2268 100644 (file)
@@ -83,7 +83,20 @@ namespace xmltooling {
     private:
         const QName& m_q;
     };
-        
+
+    /**
+     * Template algorithm returns first pointer element from a container that matches a predicate.
+     *
+     * @param c read-only container of pointer-based objects
+     * @param p a predicate function
+     * @return  the first object in the container matching the predicate, or NULL
+     */
+    template<typename Container, typename Predicate>
+    typename Container::value_type find_if(const typename Container& c, const typename Predicate& p) {
+        Container::const_iterator i = std::find_if(c.begin(), c.end(), p);
+        return (i!=c.end()) ? *i : NULL;
+    }
+    
 };
 
 #endif /* __xmltooling_predicates_h__ */