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. 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. I am using VS2013 Compiler. 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,