predicate

using std::find with a predicate

梦想与她 提交于 2020-01-02 00:58:06
问题 I want to use std::find function along with a predicate (not sure if I use the correct word). Here is the code #include <iostream> #include <vector> #include <algorithm> using namespace std; class foo { public: typedef pair< int, vector<int> > way; typedef pair< int, int > index; typedef pair< index, vector<way> > entry; vector< entry > table; void bar() { vector<int> v1; v1.push_back(1); v1.push_back(2); way w = make_pair( 1, v1 ); vector<way> v2; v2.push_back(w); index id = make_pair( 10,

Problem using templated predicate in STL algorithms

懵懂的女人 提交于 2020-01-01 11:59:26
问题 I have the following piece of code, that gives an error on the first usage form of pred2. I was hoping if someone could explain why this particular usage is incorrect, as I think pred3 usage is similar. #include <algorithm> bool pred1(const int&) { return true; } template<typename T> bool pred2(const T&) { return true; } struct pred3 { template<typename T> bool operator()(T&) { return true; } }; int main() { int A[] = { 2, 0, 4, 6, 0, 3, 1, -7 }; const int N = sizeof(A) / sizeof(int); std:

Convert Predicate<T> to Expression<Func<T, bool>>

ⅰ亾dé卋堺 提交于 2020-01-01 09:33:14
问题 Is possible to convert a Predicate<T> to Expression<Func<T, bool>> in some way? I would like to use the next IQueryable function using the filters of the my ICollectionView: public static System.Linq.IQueryable<TSource> Where<TSource>(this System.Linq.IQueryable<TSource> source, System.Linq.Expressions.Expression<System.Func<TSource, bool>> predicate) Thanks 回答1: In theory it is possible to convert a delegate 'back' to an expression, because you can request the emitted IL of a delegate, which

Chaining of ordering predicates (e.g. for std::sort)

南笙酒味 提交于 2019-12-31 22:37:10
问题 You can pass a function pointer, function object (or boost lambda) to std::sort to define a strict weak ordering of the elements of the container you want sorted. However, sometimes (enough that I've hit this several times), you want to be able to chain "primitive" comparisons. A trivial example would be if you were sorting a collection of objects that represent contact data. Sometimes you will want to sort by last name, first name, area code . Other times first name, last name - yet other

How do I form a good predicate delegate to Find() something in my List<T>?

早过忘川 提交于 2019-12-31 08:13:32
问题 After looking on MSDN, it's still unclear to me how I should form a proper predicate to use the Find() method in List using a member variable of T (where T is a class) For example: public class Car { public string Make; public string Model; public int Year; } { // somewhere in my code List<Car> carList = new List<Car>(); // ... code to add Cars ... Car myCar = new Car(); // Find the first of each car made between 1980 and 2000 for (int x = 1980; x < 2000; x++) { myCar = carList.Find(byYear(x)

How do I form a good predicate delegate to Find() something in my List<T>?

*爱你&永不变心* 提交于 2019-12-31 08:13:03
问题 After looking on MSDN, it's still unclear to me how I should form a proper predicate to use the Find() method in List using a member variable of T (where T is a class) For example: public class Car { public string Make; public string Model; public int Year; } { // somewhere in my code List<Car> carList = new List<Car>(); // ... code to add Cars ... Car myCar = new Car(); // Find the first of each car made between 1980 and 2000 for (int x = 1980; x < 2000; x++) { myCar = carList.Find(byYear(x)

How do I match contents of an element in XPath (lxml)?

我只是一个虾纸丫 提交于 2019-12-30 02:13:26
问题 I want to parse HTML with lxml using XPath expressions. My problem is matching for the contents of a tag: For example given the <a href="http://something">Example</a> element I can match the href attribute using .//a[@href='http://something'] but the given the expression .//a[.='Example'] or even .//a[contains(.,'Example')] lxml throws the 'invalid node predicate' exception. What am I doing wrong? EDIT: Example code: from lxml import etree from cStringIO import StringIO html = '<a href="http:

XPath/XSLT nested predicates: how to get the context of outer predicate?

血红的双手。 提交于 2019-12-28 04:19:11
问题 It seems that this question was not discussed on stackoverflow before, save for Working With Nested XPath Predicates ... Refined where the solution not involving nested predicates was offered. So I tried to write the oversimplified sample of what I'd like to get: Input: <root> <shortOfSupply> <food animal="doggie"/> <food animal="horse"/> </shortOfSupply> <animalsDictionary> <cage name="A" animal="kittie"/> <cage name="B" animal="dog"/> <cage name="C" animal="cow"/> <cage name="D" animal=

Core Data Predicate Date Comparison

99封情书 提交于 2019-12-28 02:39:26
问题 Im trying to fetch all the objects in an entity matching a user selectedDate (it's an NSDate). The Core Data code is fine but my predicate keeps returning 0 results, the dates are the same in the database as the user is selecting. How should the selectedDate be compared with a date from an entity using a predicate? NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(eDate = %@)", selectedDate]; 回答1: Your predicate looks to be fine. The reason you're finding zero result are returned

How to get Predicate<T> from string expressions at runtime

牧云@^-^@ 提交于 2019-12-26 11:52:13
问题 In Winform application using EntityFramework, I implement a generic Search / Filter UI Components using BindingSource of the BaseForm and build search/filter string expression dynamically from user inputs and properties of the DataSource of BindingSource (Context entity). Find and Filter aren't supported by the BindingSource in EntityFramework, because the query result of ObjectContext or DataContext was IEnumerable type, which didn't implement IBindingList interface ref As a workaround I