functor

HowTo sort std::map?

北战南征 提交于 2019-12-01 22:09:40
Here is my map: typedef std::map<int/*security id*/, PositionMonth> PortfolioMonth; where PositionMonth is a structure, ex.: struct PositionMonth { Nav::Shares shares_; Nav::Amount market_value_; PositionMonth(void) {} PositionMonth(const Nav::Amount& market_value , const Nav::Shares& shares) : market_value_(market_value) , shares_(shares) {} }; Question : how to sort std::map by second value key param (by market_value_ , let it be int)? Examples or links? PS. Boost methods not interested! PPS. I'm unable to initialise my std::map with compare functor! Thanks for help! My solution (or how I

Where do std::bind-created functors live?

▼魔方 西西 提交于 2019-12-01 16:54:51
A function pointer can point to anything from a free function, a function object, a wrapper over a member function call. However, the std::bind created functors can have state, as well as custom-created ones. Where that state is allocated, and who is deleting it? Consider the below example - will the state ( the number 10) be deleted when the vector is deleted? Who know to call a deleter on the functor, and no deleter on the function pointer? #include <iostream> #include <functional> #include <vector> using namespace std; using namespace std::placeholders; class Bar { public: void bar(int x,

how to find duplicates in std::vector<string> and return a list of them?

北慕城南 提交于 2019-12-01 16:02:25
So if I have a vector of words like: Vec1 = "words", "words", "are", "fun", "fun" resulting list: "fun", "words" I am trying to determine which words are duplicated, and return an alphabetized vector of 1 copy of them. My problem is that I don't even know where to start, the only thing close to it I found was std::unique_copy which doesn't exactly do what I need. And specifically, I am inputting a std::vector<std::string> but outputting a std::list<std::string> . And if needed, I can use functor. Could someone at least push me in the right direction please? I already tried reading stl

pthread member function of a class with arguments

℡╲_俬逩灬. 提交于 2019-12-01 12:03:23
I was successful at attaching a thread to class member using the code on the bottom of this page: http://www.tuxtips.org/?p=5 . I can't figure out how to expand the code to encapsulate a method such as void* atom(void *inst ) where *inst is a structure that contains various thread parameters. Specifically neither Netbeans nor I understand where the example::*f is defined and how it can be valid in the thread_maker scope. Lalaland I think a better solution for using things such as pthread(which take c callbacks) is to create a wrapper function so that you can use much easier to manipulate boost

pthread member function of a class with arguments

久未见 提交于 2019-12-01 11:15:35
问题 I was successful at attaching a thread to class member using the code on the bottom of this page: http://www.tuxtips.org/?p=5. I can't figure out how to expand the code to encapsulate a method such as void* atom(void *inst ) where *inst is a structure that contains various thread parameters. Specifically neither Netbeans nor I understand where the example::*f is defined and how it can be valid in the thread_maker scope. 回答1: I think a better solution for using things such as pthread(which

Passing a C++ function object to pthread_create function as the thread routine

浪尽此生 提交于 2019-12-01 09:05:24
I know the thread routine that is passed to pthread_create API has the prototype of void *threadproc(void *). I was just wondering if it is possible to use a C++ function object as a thread routine. Here's my code: Execution::run method takes a time_t variable and a functor as parameters. It spawns a POSIX thread in which it spins until the scheduled run time is current and runs some job. #include <pthread.h> #include <ctime> #include <unistd.h> #include <iostream> using namespace std; class ThreadFunctor { public: void *operator()(time_t runTime) { time_t curTime; double timeDiff; time(

Passing a C++ function object to pthread_create function as the thread routine

社会主义新天地 提交于 2019-12-01 07:01:26
问题 I know the thread routine that is passed to pthread_create API has the prototype of void *threadproc(void *). I was just wondering if it is possible to use a C++ function object as a thread routine. Here's my code: Execution::run method takes a time_t variable and a functor as parameters. It spawns a POSIX thread in which it spins until the scheduled run time is current and runs some job. #include <pthread.h> #include <ctime> #include <unistd.h> #include <iostream> using namespace std; class

Calling generic function with Functor using subclass (cats/scalaz)

跟風遠走 提交于 2019-12-01 06:55:57
I've been messing around with some basic examples of Cats/Scalaz and also walking through tutorials to get a feel and I've hit a case which I'm sure there is a solution to. Is it possible to call a generalized function that takes a contextualized value ( F[A] ) with a Functor view ( F[_] : Functor ) with a context that is <: F ? I'm aware that Functor is invariant on type F[_] , and I'm also aware of the existence of Functor.widen , but it seems strange that there is no way to implicitly widen my type for use in a general function. An example in Cats (a similar example with Scalaz exists as

C++ functor as a function pointer

穿精又带淫゛_ 提交于 2019-12-01 05:26:56
I have a Functor which I need to send to a function which receives a function pointer as a parameter (such as CreateThread ). Can I convert it to a static method address somehow? And if not, how can I do it? No, you can't convert a class-type object to a function pointer. However, you can write a non-member wrapper function that calls the member function on the right instance. Many APIs, including CreateThread , allow you to provide a pointer that it will give back to you when it calls your callback (for CreateThread , this is the lpParameter parameter). For example: DWORD WINAPI

Calling generic function with Functor using subclass (cats/scalaz)

北战南征 提交于 2019-12-01 05:24:42
问题 I've been messing around with some basic examples of Cats/Scalaz and also walking through tutorials to get a feel and I've hit a case which I'm sure there is a solution to. Is it possible to call a generalized function that takes a contextualized value ( F[A] ) with a Functor view ( F[_] : Functor ) with a context that is <: F ? I'm aware that Functor is invariant on type F[_] , and I'm also aware of the existence of Functor.widen , but it seems strange that there is no way to implicitly