简体   繁体   English

在std :: map中存储函数指针

[英]Storing function pointers in a std::map

I am trying to store function pointers in a map, along with a structure. 我试图将函数指针与结构一起存储在映射中。 The idea is to execute the corresponding functions, when I find specific values in the structure. 我的想法是在结构中找到特定值时执行相应的功能。 The program is not compiling, and giving me lot of errors when I am trying to insert data into the map through std::make_pair. 该程序未编译,当我尝试通过std :: make_pair将数据插入到映射中时,给了我很多错误。 Here is the code that I have written. 这是我编写的代码。 Please guide me as to what I am doing wrong here.. 请指导我在这里做错了什么..

#include "stdafx.h"
#include <iostream>
#include <string>
#include <map>

struct _timeset
{
    int hr1;
    int min1;
    int secs1;
};

_timeset t1 = { 17, 10, 30 };

void fun1(void)
{
    std::cout << "inside fun1\n";
}

void fun2(void)
{
    std::cout << "inside fun2\n";
}

void fun3(void)
{
    std::cout << "inside fun3\n";
}

std::map<_timeset, void(*)()> m1;

int main()
{
    m1.insert(std::make_pair(t1, fun1));  //Compiling errors here



    return 0;
}

My basics in STL is very poor. 我的STL基础知识很差。 I am using VS2013 Compiler. 我正在使用VS2013编译器。 Also while iterating the map, can I execute the relevant function with something like : 另外,在迭代地图时,我可以执行类似的相关功能:

std::map<_timeset, void(*)()>::iterator it1;
    int i = 0;
    for (i=0,it1 = m1.begin(); it1 != m1.end(); it1++,i++)
    {

        _timeset _t = it1->first;
         //Check Values in _t, and then execute the corresponding function in the map

            (*it1->second)();
    }

many thanks, 非常感谢,

You need to specify comparator for _timeset for std::map to work, for example: 您需要为_timeset指定比较器, _timeset使std::map工作,例如:

struct _timeset
{
    int hr1;
    int min1;
    int secs1;
    bool operator<( const _timeset &t ) const {
        return std::make_tuple( hr1, min1, secs1 ) < std::make_tuple( t.hr1, t.min1, t.secs1 );
    }
};

or you can make it as lamba as described here 或者您可以按此处所述将其制作为兰巴舞

Also while iterating the map, can I execute the relevant function with something like 另外,在迭代地图时,我是否可以执行类似的功能

yes you can, also you do not have to copy your struct _timeset : 是的,您可以,也不必复制struct _timeset

int i = 0;
for ( it1 = m1.begin(); it1 != m1.end(); it1++,i++)
{

    const _timeset &_t = it1->first;
     //Check Values in _t, and then execute the corresponding function in the map

        (*it1->second)();
}

if you want to store functions with different signature, use std::function : 如果要存储具有不同签名的函数,请使用std::function

typedef std::function<void()> func;
std::map<_timeset, func> m1;

void fun1();
void func2( int );
struct foobar { void func3(); };

m1.insert( std::make_pair( t, func1 ) ); // signature matches, no need for bind
m1.insert( std::make_pair( t, std::bind( func2, 123 ) ) ); // signature does not match func2(123) will be called
foobar *pf = ...;
m1.insert( std::make_pair( t, std::bind( func3, pf ) ) ); // signature does not match, pf->func3() will be called
// etc

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM