map的vale是一个函数时,如何调用

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

void func_handleMessage()
{
    std::cout<< "call Text_MessageService "<<std::endl; 
}

void func_pic_handleMessage()
{
     std::cout<< "call Picture_MessageService "<<std::endl; 
}
int main()
{
    std::function<void()> handle1 = func_handleMessage;
    std::function<void()> handle2 = func_pic_handleMessage;

    map<int,MessageService>handleMap;
    std::map<int,std::function<void()>> mymap;

    mymap.insert(make_pair(1,handle1));
    mymap.insert(make_pair(2,handle2));
    mymap.insert(make_pair(3,handle2));
    mymap[1](); //如果map的vale是一个函数,如果要调用到话要用函数调用符()显示调用
    mymap[2]();
    mymap[3]();
    mymap[3];

    for(auto& preMap:mymap)
    { 
        cout << preMap.first<< "  ";
        preMap.second(); //如果map的vale是一个函数,如果要调用到话要用函数调用符()显示调用
        cout<< endl;
    }

    return 1;
}
发布了19 篇原创文章 · 获赞 2 · 访问量 194

猜你喜欢

转载自blog.csdn.net/qfturauyls/article/details/103573749