0

I got a task to create a map with key values and functions inside. I managed to do that. Now I need to create a loop where I need to call a different print function by pressing up, down, left, right keyboard buttons(eg, i press up, the function prints "UP"). Any idea how? My mentor told me that its a practice for understanding the usage of function object. Here is my program for now:

#include <functional>
#include <iostream>
#include <string.h>
#include <map>



using namespace std;


void up (){

    cout<<"UP";
}

void down (){

    cout<<"DOWN";
}

void left (){

    cout<<"LEFT";
}
void right (){

    cout<<"RIGHT";
}


int main (){

   typedef map<int, void (*)()> mapaF;

    mapaF Kretnje;

    Kretnje.insert(pair<int,void (*)() >(1, up));
    Kretnje.insert(pair<int, void (*)()>(2, down));
    Kretnje.insert(pair<int, void (*)()>(3, left));
    Kretnje.insert(pair<int, void (*)()>(4, right));

    for(auto a: Kretnje){
        cout<<"Kljuc: "<<a.first<<" Funkcija: ";
         a.second();
         cout<<endl;

    }




    return 0;
}
dranec
  • 47
  • 6
  • Here is possible helping thread : http://stackoverflow.com/questions/24708700/c-detect-when-user-presses-arrow-key – Angelus Mortis Feb 19 '16 at 08:42
  • Did your teacher talk about the "function object" (`std::function`) or about "function pointers"? – Simon Kraemer Feb 19 '16 at 09:46
  • Callbacks, he said that i need to start with functions pointers, then std::function which is for creating functions object (objects that behave like functions if im right). – dranec Feb 19 '16 at 10:33

0 Answers0