c++容器(vector|map)中使用函数指针
来源:互联网 发布:股票历史数据分析软件 编辑:程序博客网 时间:2024/05/08 22:53
c++容器(vector|map)中使用函数指针
我要动态生成mfc菜单,动态绑定响应命令。
首先把菜单关键词和函数指针绑定在map中,
再通过关键词找到函数来执行。
20121028更正vector可以直接放函数指针类型,没理解到位。
#include "stdafx.h"
#include <vector>
#include <map>
#include <string>
using namespace std;
// 声明一个函数指针
int(*pFunc)(int);
int func1(int nIn){return nIn + 1;}
int func2(int nIn){return nIn + 20;}
typedef int(*pInt)(int);//定义别名才能放在vector中
void main()
{
pFunc = func1;// 把函数名赋给函数指针
int n = pFunc(1);
pFunc = &func2;
n = pFunc(1);
//vector<int(*pFun)(int)> v_pFunc;//不能这样定义
//
vector<pInt> v_pInt;
v_pInt.push_back(func1);
v_pInt.push_back(func2);
int i = v_pInt[0](2);
i = v_pInt[1](2);
//
map<string,pInt> map_pInt;
map_pInt.insert(pair<string,pInt>("key1",func1));
map_pInt.insert(pair<string,pInt>("key2",func2));
int j = map_pInt["key1"](3);
j = map_pInt["key2"](3);
}
#include <vector>
#include <map>
#include <string>
using namespace std;
// 声明一个函数指针
int(*pFunc)(int);
int func1(int nIn){return nIn + 1;}
int func2(int nIn){return nIn + 20;}
typedef int(*pInt)(int);//定义别名才能放在vector中
void main()
{
pFunc = func1;// 把函数名赋给函数指针
int n = pFunc(1);
pFunc = &func2;
n = pFunc(1);
//vector<int(*pFun)(int)> v_pFunc;//不能这样定义
//
vector<pInt> v_pInt;
v_pInt.push_back(func1);
v_pInt.push_back(func2);
int i = v_pInt[0](2);
i = v_pInt[1](2);
//
map<string,pInt> map_pInt;
map_pInt.insert(pair<string,pInt>("key1",func1));
map_pInt.insert(pair<string,pInt>("key2",func2));
int j = map_pInt["key1"](3);
j = map_pInt["key2"](3);
}
//vector只能放类型,不能放函数指针变量名
vector<int(*)(int)> v_pFunc;
v_pFunc.push_back(func1);
v_pFunc.push_back(func2);
int k = v_pFunc[0](5);
k = v_pFunc[1](5);
vector<int(*)(int)> v_pFunc;
v_pFunc.push_back(func1);
v_pFunc.push_back(func2);
int k = v_pFunc[0](5);
k = v_pFunc[1](5);
- c++容器(vector|map)中使用函数指针
- c++容器(vector|map)中使用函数指针
- 【C++】vector容器和list容器中使用单个迭代器的erase函数学习笔记
- C++STL中vector容器 assign()函数的用法
- C++中vector容器的使用方式和函数调用
- C++--vector<>容器,List<>链表,map<>容器的用法
- C++ 中Vector容器使用
- C++中使用vector容器
- 使用boost的xxx_list_of函数为静态容器map和vector等赋初值
- C/C++知识要点2——STL中Vector、Map、Set容器的实现原理
- 【语言-C++】C++容器:list、vector、hash、map的区别及使用范围
- vector中存放函数指针
- stl map容器中指针的释放
- 文本查询程序(c++)set,vector,map容器实现
- c++STL容器(map,set,vector,stack,queue)
- vector -- STL中容器的常用函数
- C++STL中vector容器的用法
- C++STL中vector容器的用法
- Fedora14+NS-2.34+Leach
- oracle中n/varchar、n/varchar2、n/char几种类型的区别与使用
- 编程之美2013 管道系统
- Fedora14+OpenMPI-1.6
- ubuntu等linux下自定义设置程序代理工具proxychains简介
- c++容器(vector|map)中使用函数指针
- vi中一些常用技巧
- 各个数据库软件对于sql的支持
- 统计学笔记——浙江大学版
- 项目3-友元类
- HDU 1072 Nightmare 广度优先搜索(BFS)
- SElinux以及防火墙的关闭
- 正则表达式学习——精通正则表达式
- Java Web 高性能开发,第 1 部分: 前端的高性能