unary-function

Is it possible to pass a literal value to a lambda unary predicate in C++?

旧街凉风 提交于 2020-02-24 09:22:07
问题 Given the following code that does a reverse lookup on a map: map<char, int> m = {{'a', 1}, {'b', 2}, {'c', 3}, {'d', 4}, {'e', 5}}; int findVal = 3; Pair v = *find_if(m.begin(), m.end(), [findVal](const Pair & p) { return p.second == findVal; }); cout << v.second << "->" << v.first << "\n"; Is it possible to pass a literal value (in this case 3) instead of having to store it in a variable (i.e. findVal) so that it can be accessed via the capture list? Obviously one of the constraints in this

Why have unary_function, binary_function been removed from C++11?

眉间皱痕 提交于 2019-12-18 11:51:33
问题 I found that binary_function is removed from C++11. I am wondering why. C++98: template <class T> struct less : binary_function <T,T,bool> { bool operator() (const T& x, const T& y) const {return x<y;} }; C++11: template <class T> struct less { bool operator() (const T& x, const T& y) const {return x<y;} typedef T first_argument_type; typedef T second_argument_type; typedef bool result_type; }; MODIFIED ---------------------------------------------------------------------------- template

Is There a Indirection Functor?

别说谁变了你拦得住时间么 提交于 2019-12-07 00:28:27
问题 I'm looking for a unary functor which will dereference it's argument and return the result. Of course I can write one, it just seemed like something should already exist. So given the code: const auto vals = { 0, 1, 2, 3 }; vector<const int*> test(size(vals), nullptr); iota(begin(test), end(test), data(vals)); transform(cbegin(test), cend(test), ostream_iterator<int>(cout, " "), [](const auto& i){ return *i; }); Live Example I was hoping that there was a functor that I could use instead of

Is There a Indirection Functor?

无人久伴 提交于 2019-12-05 05:47:50
I'm looking for a unary functor which will dereference it's argument and return the result. Of course I can write one, it just seemed like something should already exist. So given the code: const auto vals = { 0, 1, 2, 3 }; vector<const int*> test(size(vals), nullptr); iota(begin(test), end(test), data(vals)); transform(cbegin(test), cend(test), ostream_iterator<int>(cout, " "), [](const auto& i){ return *i; }); Live Example I was hoping that there was a functor that I could use instead of the lambda. Does such a thing exist, or do I need to just use the lambda? Assuming that by "functor" you

Why have unary_function, binary_function been removed from C++11?

∥☆過路亽.° 提交于 2019-11-30 05:15:17
I found that binary_function is removed from C++11. I am wondering why. C++98: template <class T> struct less : binary_function <T,T,bool> { bool operator() (const T& x, const T& y) const {return x<y;} }; C++11: template <class T> struct less { bool operator() (const T& x, const T& y) const {return x<y;} typedef T first_argument_type; typedef T second_argument_type; typedef bool result_type; }; MODIFIED ---------------------------------------------------------------------------- template<class arg,class result> struct unary_function { typedef arg argument_type; typedef result result_type; };