当前位置:首页 » 编程软件 » 动态编译仿函数

动态编译仿函数

发布时间: 2022-10-08 22:47:06

1. call [eax]这样的动态调用 在C++中是如何体现的

1、函数指针
typedef int (*func_t)(int, int);
int f(int a, int b)
{
return a + b;
}
int g(func_t f, int a, int b)
{
return f(a, b);
}
int main()
{
printf("%d\n", g(f, 1, 2));
}
2、函数对象(C++11或TR1)
#include <functional> //#include <tr1/functional>
using namespace std;
//using namespace std::tr1;
int f(int a, int b)
{
return a + b;
}
int g(function<int(int,int)> f, int a, int b)
{
return f(a, b);
}
int main()
{
printf("%d\n", g(f, 1, 2));
}
方法2所使用的对象可以与仿函数共同工作,但速度比方法1慢(类型擦除导致虚函数调用)

2. 地图结构

#include <afx.h>
#include <iostream>
#include <map>
#include <algorithm>

using namespace std;

struct Node
{
int nData;
Node *pNext;
Node(int initData=0, Node* p=NULL):nData(initData),pNext(p)
{
}
};

int main()
{
typedef map<CString ,Node > mapList;
mapList testMap;
CString strName;
Node theNode;

//the method 1 : using the make_pair and insert command insert to map
strName = "first";
theNode.nData = 1;
testMap.insert(make_pair(strName,theNode));

//the method 2 :Using the key insert to map
strName = "second";
theNode.nData = 2;
testMap[strName] = theNode;

mapList::const_iterator it;
mapList::iterator iter;
for(iter = testMap.begin();iter != testMap.end();++iter)
{
strName = (*iter).first;
theNode = (*iter).second;
cout<<"the key name is "<<strName.GetBuffer(0)<<endl;
cout<<"the node data is "<<theNode.nData<<endl;
}

return 0;
}

分析:插入map的结构方法有两种,一种是insert函数法,配合使用make_pair很容易实现;还有一种就是索引法,用KEY做索引,testMap[strName] = theNode;也可以将其插入到map结构中。

---------------------------------------------------------------------------------------------------------------------------------

STL中map结构:

template <class Key, class T, class Compare = less<Key>, class Alloc = alloc>
第一个参数Key是关键字类型
第二个参数T是值类型
第三个参数Compare是比较函数(仿函数)
第四个参数是内存配置对象

map内部存储机制实际是以红黑树为基础,红黑树在插入节点时,必须依照大小比对之后在一个合适的位置上执行插入动作。所以作为关键字,起码必须有“<”这个比较操作符。但是复杂数据类型或者自定义类型struct,class等,如果没有明确定义“<”比较操作符,就不能与map直接搭配使用,或者在类型定义结构中重载定义“<”比较操作符,或者自己定义第三个参数。

在选择map的关键字时,注意以下两点,同时这两点也是改错的方法:
a) 关键字明确定义“<”比较操作符
b) 没有“<”比较操作符,自定义仿函数替代第三个参数Compare,该仿函数实现“()”操作符,提供比较功能。插入时各节点顺序以该仿函数比较。

STL中map结构:

template <class Key, class T, class Compare = less<Key>, class Alloc = alloc>
第一个参数Key是关键字类型
第二个参数T是值类型
第三个参数Compare是比较函数(仿函数)
第四个参数是内存配置对象

map内部存储机制实际是以红黑树为基础,红黑树在插入节点时,必须依照大小比对之后在一个合适的位置上执行插

入动作。所以作为关键字,起码必须有“<”这个比较操作符。但是复杂数据类型或者自定义类型struct,class等,

如果没有明确定义“<”比较操作符,就不能与map直接搭配使用,除非自己定义第三个参数。

在选择map的关键字时,注意以下两点,同时这两点也是改错的方法:
a) 关键字明确定义“<”比较操作符
b) 没有“<”比较操作符,自定义仿函数替代第三个参数Compare,该仿函数实现“()”操作符,提供比较功能。

插入时各节点顺序以该仿函数为纲。

1.以std::pair为关键字插入map

struct comp
{
typedef std::pair<int, int> value_type;

bool operator () (const value_type & ls, const value_type &rs) const
{
return ls.first < rs.first || (ls.first == rs.first && ls.second < rs.second);
}
};

int main()
{
std::map<std::pair<int, int>, int, comp> testMap;

testMap.insert(std::make_pair(std::make_pair(1,2), 3));
testMap.insert(std::make_pair(std::make_pair(11,22), 33));
testMap.insert(std::make_pair(std::make_pair(111,222), 333));

std::map<std::pair<int, int>, int, comp>::iterator it = testMap.find(std::make_pair(111,222));

if (it == testMap.end())
cout<<"The key is not in the map."<<endl;
else
cout<<"the key value is :"<<it->first.first<<","<<it->first.second<<endl
<<"the nonkey value is"<<it->second<<endl;

return 0;
}

分析:pair结构中没有明确定义"<"比较操作符,如果不定义比较函数的comp,而直接插入就会编译出错。这里我们

定义了一个外部的比较函数是实现"()"比较操作符.

2.以结构体或类为关键字插入map
struct Node
{
string strKey;
int nKeyData;

Node(string stmp,int ntmp=0):strKey(stmp),nKeyData(ntmp){}

bool operator < (const Node &rhs) const
{
return strKey<rhs.strKey;
}
};

int main()
{
std::map<struct Node, int> testMap;

testMap.insert(std::make_pair(Node("test1",1), 3));
testMap.insert(std::make_pair(Node("test2",2), 33));
testMap.insert(std::make_pair(Node("test3",22), 333));

std::map<struct Node, int>::iterator it = testMap.find(Node("test2",2));

if (it == testMap.end())
cout<<"The key is not in the map."<<endl;
else
cout<<"the key value is :"<<it->first.strKey<<","<<it->first.nKeyData<<endl
<<"the nonkey value is "<<it->second<<endl;
return 0;
}

分析:注意operator < (const Node &rhs) const ,一定要按照这个形式来写,少一个const都会报错。原因在于map<struct Node, int> testMap 定义时,结构体中的“<”在less调用这个比较符时,它都是以const方式传入,不可能再以非const方式调用,故出错。

刚刚给出的例子,是在struct结构中定义了'<' 操作符:bool operator < (const Node &rhs) const 。除此之外,我们还可以用另一种方法,内联函数的方法解决以上出现的问题:

struct Node
{
string strKey;
int nKeyData;

Node(string stmp,int ntmp=0):strKey(stmp),nKeyData(ntmp){}
friend bool operator < (const struct Node &lhs, const struct Node &rhs);

// bool operator < (const Node &rhs) const
// {
// return strKey<rhs.strKey;
// }
};

inline bool operator < (const struct Node &lhs, const struct Node &rhs)
{
return lhs.strKey<rhs.strKey;
}

PS:
在map容器中插入数据有很多函数可用,这里只讨论最普通的insert操作,在STL中它是这样定义的。

pair<iterator, bool> insert(const value_type& x);

map容器不允许键值重复,在执行插入操作后,可以凭借该返回值获取操作结果。返回值是一个迭代器和布尔值的键值对,迭代器指向map中具有该值的元素,布尔值表示是否插入成功。如果布尔值为true,表示插入成功,则迭代器为新插入值在map中的位置;布尔值为false,表示插入失败(已经存在该值),迭代器为原有值在map中的位置。

3. c++语言sort函数问题,为什么编译错误求助

1)sort()的第三个参数,是一个比较函数(或仿函数),这个比较函数的返回值应该是bool型的。
2)sort()内部在排序的时候,会用赋值符号"=",来对元素进行顺序调整,而你的元素是char[100]型的,是不能用"="来赋值的(char[]数组一般是用strcpy()来赋值的)。
3)while(scanf("%d",&n)&&n) 会引起死循环。因为读到文件末尾的话,scanf()会返回-1。
改正后的代码如下:
#include<cstdlib>
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<string>
#include<map>
#include <algorithm>
#include<set>
#include<vector>
#define inf 0x7fffffff
#define E 1e-9
#define N 25
using namespace std;
string a[N];
int n;
bool cmp(const string &a, const string &b)
{
string aa, bb;
aa = a + b;
bb = b + a;;
return aa < bb;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("ex.in","r",stdin);
#endif
while( scanf("%d",&n) == 1 && n )
{
for(int i=0;i<n;++i)
{
char tmp[100];
scanf("%s",tmp);
a[i] = tmp;
}
sort( a, a+n, cmp );
for(int i=0;i<n;++i)
printf("%s",a[i].c_str());
printf("\n");
}
return 0;
}

4. 电脑里常用的的英语有那些(汉语意思)

硬件类(Hardware)
软件类(Software)
网络类(Network)

CPU(Center Processor Unit)中央处理单元
mainboard主板
RAM(random access
memory)随机存储器(内存)
ROM(Read Only Memory)只读存储器
Floppy Disk软盘
Hard Disk硬盘
CD-ROM光盘驱动器(光驱)
monitor监视器
keyboard键盘
mouse鼠标
chip芯片
CD-R光盘刻录机
HUB集线器
Modem= MOlator-DEMolator,调制解调器
P-P(Plug and Play)即插即用
UPS(Uninterruptable Power Supply)不间断电源
BIOS(Basic-input-Output
System)基本输入输出系统
CMOS(Complementary Metal-Oxide-Semiconctor)互补金属氧化物半导体
setup安装
uninstall卸载
wizzard向导
OS(Operation Systrem)操作系统
OA(Office AutoMation)办公自动化
exit退出
edit编辑
复制
cut剪切
paste粘贴
delete删除
select选择
find查找
select all全选
replace替换
undo撤消
redo重做
program程序
license许可(证)
back前一步
next下一步
finish结束
folder文件夹
Destination Folder目的文件夹
user用户
click点击
double click双击
right click右击
settings设置
update更新
release发布
data数据
data base数据库
DBMS(Data Base Manege
System)数据库管理系统
view视图
insert插入
object对象
configuration配置
command命令
document文档
POST(power-on-self-test)电源自检程序
cursor光标
attribute属性
icon图标
service pack服务补丁
option pack功能补丁
Demo演示
short cut快捷方式
exception异常
debug调试
previous前一个
column行
row列
restart重新启动
text文本
font字体
size大小
scale比例
interface界面
function函数
access访问
manual指南
active激活
computer language计算机语言
menu菜单
GUI(graphical user
interfaces )图形用户界面
template模版
page setup页面设置
password口令
code密码
print preview打印预览
zoom in放大
zoom out缩小
pan漫游
cruise漫游
full screen全屏
tool bar工具条
status bar状态条
ruler标尺
table表
paragraph段落
symbol符号
style风格
execute执行
graphics图形
image图像
Unix用于服务器的一种操作系统
Mac OS苹果公司开发的操作系统
OO(Object-Oriented)面向对象
virus病毒
file文件
open打开
colse关闭
new新建
save保存
exit退出
clear清除
default默认
LAN局域网
WAN广域网
Client/Server客户机/服务器
ATM( Asynchronous
Transfer Mode)异步传输模式
Windows NT微软公司的网络操作系统
Internet互联网
WWW(World Wide Web)万维网
protocol协议
HTTP超文本传输协议
FTP文件传输协议
Browser浏览器
homepage主页
Webpage网页
website网站
URL在Internet的WWW服务程序上
用于指定信息位置的表示方法
Online在线
Email电子邮件
ICQ网上寻呼
Firewall防火墙
Gateway网关
HTML超文本标识语言
hypertext超文本
hyperlink超级链接
IP(Address)互联网协议(地址)
SearchEngine搜索引擎
TCP/IP用于网络的一组通讯协议
Telnet远程登录
IE(Internet Explorer)探索者(微软公司的网络浏览器)
Navigator引航者(网景公司的浏览器)
multimedia多媒体
ISO国际标准化组织
ANSI美国国家标准协会
able 能
activefile 活动文件
addwatch 添加监视点
allfiles 所有文件
allrightsreserved 所有的权力保留
altdirlst 切换目录格式
并能够解决更大范围内的磁盘问题
andotherinFORMation 以及其它的信息
archivefileattribute 归档文件属性
assignto 指定到
autoanswer 自动应答
autodetect 自动检测
autoindent 自动缩进
autosave 自动存储
availableonvolume 该盘剩余空间
badcommand 命令错
badcommandorfilename 命令或文件名错
batchparameters 批处理参数
binaryfile 二进制文件
binaryfiles 二进制文件
borlandinternational borland国际公司
bottommargin 页下空白
bydate 按日期
byextension 按扩展名
byname 按名称
bytesfree 字节空闲
callstack 调用栈
casesensitive 区分大小写
要求出现确认提示,在你想覆盖一个
centralpointsoftwareinc central point 软件股份公司
changedirectory 更换目录
changedrive 改变驱动器
changename 更改名称
characterset 字符集
checkingfor 正在检查
检查磁盘并显示一个状态报告
chgdrivepath 改变盘/路径
china 中国

chooseoneofthefollowing 从下列中选一项
clearall 全部清除
clearallbreakpoints 清除所有断点
clearsanattribute 清除属性
clearscommandhistory 清除命令历史
clearscreen 清除屏幕
closeall 关闭所有文件
codegeneration 代码生成
colorpalette 彩色调色板
commandline 命令行
commandprompt 命令提示符
compressedfile 压缩文件
配置硬盘,以为 MS-DOS 所用
conventionalmemory 常规内存
***ceptemptyones 拷贝目录和子目录,空的除外
拷贝设置了归档属性的文件
把文件拷贝或搬移至另一地方
把一个软盘的内容拷贝到另一个软盘上
diskette 复制磁盘
C拷贝M移动 O比 F搜索R改名 D删除 V版本 E浏览A属性 W写字 P打印 L列表
rightc 版权(c
创建DOS分区或逻辑DOS驱动器
createextendeddospartition 创建扩展DOS分区
在扩展DOS分区中创建逻辑DOS驱动器
createprimarydospartition 创建DOS主分区
createsadirectory 创建一个目录
创建,改变或删除磁盘的卷标
currentfile 当前文件
currentfixeddiskdrive 当前硬盘驱动器
currentsettings 当前设置
currenttime 当前时间
cursorposition 光标位置

defrag 整理碎片
dele 删去
删除分区或逻辑DOS驱动器
删除一个目录和所有的子目录及其中的所有文件
deltree 删除树
devicedriver 设备驱动程序
dialogbox 对话栏
directionkeys 方向键
directly 直接地
directorylistargument 目录显示变量
directoryof 目录清单
directorystructure 目录结构
diskaccess 磁盘存取
disk 磁盘拷贝
磁盘服务功能: C拷贝 O比较 F搜索R改卷名V校验 浏览E编缉M图 L找文件 N格式化
diskspace 磁盘空间
displayfile 显示文件
displayoptions 显示选项
displaypartitioninFORMation 显示分区信息
显示指定目录和所有目录下的文件
显示指定属性的文件
显示或改变文件属性
displaysorsetsthedate 显示或设备日期
以单色而非彩色显示安装屏信息
显示系统中已用和未用的内存数量
显示磁盘上所有文件的完整路径和名称
显示或改变当前目录
doctor 医生
doesn 不
doesntchangetheattribute 不要改变属性
dosshell DOS 外壳
doubleclick 双击
你想显示逻辑驱动器信息吗(y/n)?
driveletter 驱动器名
editmenu 编辑选单
emsmemory ems内存
endoffile 文件尾
endofline 行尾
enterchoice 输入选择
entiredisk 转换磁盘
environmentvariable 环境变量
esc esc
everyfileandsubdirectory 所有的文件和子目录
existingdestinationfile 已存在的目录文件时
expandedmemory 扩充内存
expandtabs 扩充标签
explicitly 明确地
extendedmemory 扩展内存
fastest 最快的
fatfilesystem fat 文件系统
fdiskoptions fdisk选项
fileattributes 文件属性
fileFORMat 文件格式
filefunctions 文件功能
fileselection 文件选择
fileselectionargument 文件选择变元
filesin 文件在
filesinsubdir 子目录中文件
fileslisted 列出文件
filespec 文件说明
filespecification 文件标识
filesselected 选中文件
findfile 文件查寻
fixeddisk 硬盘
fixeddisksetupprogram 硬盘安装程序
fixeserrorsonthedisk 解决磁盘错误
floppydisk 软盘
FORMatdiskette 格式化磁盘
FORMatsadiskforusewithmsdos 格式化用于MS-DOS的磁盘
FORMfeed 进纸
freememory 闲置内存
fullscreen 全屏幕
functionprocere 函数过程
graphical 图解的
graphicslibrary 图形库
groupdirectoriesfirst 先显示目录组
hangup 挂断
harddisk 硬盘
hardwaredetection 硬件检测
ha**een 已经
helpfile 帮助文件
helpindex 帮助索引
helpinFORMation 帮助信息
helppath 帮助路径
helpscreen 帮助屏
helptext 帮助说明
helptopics 帮助主题
helpwindow 帮助窗口
hiddenfile 隐含文件
hiddenfileattribute 隐含文件属性
hiddenfiles 隐含文件
howto 操作方式
ignorecase 忽略大小写
在常规和上位内存
incorrectdos 不正确的DOS
incorrectdosversion DOS 版本不正确
indicatesabinaryfile 表示是一个二进制文件
indicatesanasciitextfile 表示是一个ascii文本文件
insertmode 插入方式
请用scandisk,不要用chkdsk
inuse 在使用
invaliddirectory 无效的目录
is 是
kbytes 千字节
keyboardtype 键盘类型
labeldisk 标注磁盘
laptop 膝上
largestexecutableprogram 最大可执行程序
largestmemoryblockavailable 最大内存块可用
lefthanded 左手习惯
leftmargin 左边界
linenumber 行号
linenumbers 行号
linespacing 行间距
listbyfilesinsortedorder 按指定顺序显示文件
listfile 列表文件
listof 清单
locatefile 文件定位
lookat 查看
lookup 查找
macroname 宏名字
makedirectory 创建目录
memoryinfo 内存信息
memorymodel 内存模式
menubar 菜单条
menucommand 菜单命令
menus 菜单
messagewindow 信息窗口
microsoft 微软
microsoftantivirus 微软反病毒软件
microsoftcorporation 微软公司
mini 小的
modemsetup 调制解调器安装
molename 模块名
monitormode 监控状态
monochromemonitor 单色监视器
moveto 移至
multi 多
newdata 新建数据
newer 更新的
newfile 新文件
newname 新名称
newwindow 新建窗口
norton norton
nostack 栈未定义
noteusedeltreecautiously 注意:小心使用deltree
onlinehelp 联机求助
optionally 可选择地
or 或
pageframe 页面
pagelength 页长
在显示每屏信息后暂停一下
pctools pc工具
postscript 附言
prefixmeaningnot 前缀意即"不
prefixtoreverseorder 反向显示的前缀
presetche** 用前缀和放在短横线-后的开关(例如/-w)预置开关
pressakeytoresume 按一键继续
pressanykeyforfilefunctions 敲任意键执行文件功能
pressentertokeepthesamedate 敲回车以保持相同的日期
pressentertokeepthesametime 敲回车以保持相同的时间
pressesctocontinue 敲esc继续
pressesctoexit 敲键退出
pressesctoexitfdisk 敲esc退出fdisk
敲esc返回fdisk选项

5. 为什么c++14把random_shuffle标为deprecated

random_shuffle有两种形式,即

template<classRandomAccessIterator>
voidrandom_shuffle(RandomAccessIteratorfirst,RandomAccessIteratorlast);

template<classRandomAccessIterator,classRandomNumberGenerator>
voidrandom_shuffle(RandomAccessIteratorfirst,RandomAccessIteratorlast,
RandomNumberGenerator&&rnd);

二者被deprecated(即不推荐使用)的原因并不一样。

  • 前者使用编译器指定的随机数发生器来打乱数组的内容,这个随机数发生器通常是rand函数,它被deprecated的原因也和rand函数有关。由于rand函数是C语言的遗留产物,不同编译器实现方法不一样,产生随机数的可靠性也千差万别,并且C++11提供了一整套明确规定了算法的随机数发生器(位于<random>头文件)。有人提议把rand函数标为deprecated,来让更多的人使用C++11提供的“更好的东西”。使用rand函数的random_shuffle也被连带标为deprecated。

  • 后者要求rnd仿函数(只能是仿函数)产生的随机数的范围就是数组的下标范围,这样的仿函数通常不是很容易写,完全可以被更好的东西代替——这个更好的东西就是shuffle函数

shuffle函数与random_shuffle的第二种形式相似,同样是接受一个仿函数,但对随机数的范围要求更松,因而可以直接传一个std::mt19997对象,或者std::random_device对象(都是C++11直接提供的随机数发生器)的进去,更便于使用,同时不像rand一样,不能保证打乱结果的可靠性

6. C++容器类中参数列表如何调用

一、STL容器类
STL(Standard
Template
Library)的六大组件:容器(containers)、迭代器(iterators)、空间配置器(allocator)、配接器(adapters)、算法(algorithms)、仿函数(functors)六个部分。其交互关系:容器通过空间配置器取得数据存储空间,空间配置器通过迭代器存取容器的内容,仿函数可以协助空间配置器完成不同的策略变化,配接器可以修饰或套接仿函数。
C++中的容器类包括“顺序存储结构”和“关联存储结构”,前者包括vector,list,deque等;后者包括set,map,multiset,multimap等。若需要存储的元素数在编译器间就可以确定,可以使用数组来存储,否则,就需要用到容器类了。
1、vector
连续存储结构,每个元素在内存上是连续的;
支持高效的随机访问和在尾端插入/删除操作,但其他位置的插入/删除操作效率低下;
2、deque
连续存储结构,即其每个元素在内存上也是连续的,类似于vector,不同之处在于,deque提供了两级数组结构,第一级完全类似于vector,代表实际容器;另一级维护容器的首位地址。
这样,deque除了具有vector的所有功能外,还支持高效的首端插入/删除操作。
3、list
非连续存储结构,具有双链表结构,每个元素维护一对前向和后向指针,因此支持前向/后向遍历。
支持高效的随机插入/删除操作,但随机访问效率低下,且由于需要额外维护指针,开销也比较大。
4、vector
--
list
--
deque:
a、若需要随机访问操作,则选择vector;
b、若已经知道需要存储元素的数目,
则选择vector;
c、若需要随机插入/删除(不仅仅在两端),则选择list
d、只有需要在首端进行插入/删除操作的时候,才选择deque,否则都选择vector。
e、若既需要随机插入/删除,又需要随机访问,则需要在vector与list间做个折中。
f、当要存储的是大型负责类对象时,list要优于vector;当然这时候也可以用vector来存储指向对象的指针,同样会取得较高的效率,但是指针的维护非常容易出错,因此不推荐使用。
5、capacity
--
size
a、capacity是容器需要增长之前,能够盛的元素总数;只有连续存储的容器才有capacity的概念(例如vector,deque,string),list不需要capacity。
b、size是容器当前存储的元素的数目。
c、vector默认的容量初始值,以及增长规则是依赖于编译器的。
6、用vector存储自定义类对象时,自定义类对象须满足:
a、有可供调用的无参构造函数(默认的或自定义的);
b、有可用的拷贝赋值函数(默认的或自定义的)

热点内容
内置存储卡可以拆吗 发布:2025-05-18 04:16:35 浏览:333
编译原理课时设置 发布:2025-05-18 04:13:28 浏览:376
linux中进入ip地址服务器 发布:2025-05-18 04:11:21 浏览:610
java用什么软件写 发布:2025-05-18 03:56:19 浏览:31
linux配置vim编译c 发布:2025-05-18 03:55:07 浏览:107
砸百鬼脚本 发布:2025-05-18 03:53:34 浏览:941
安卓手机如何拍视频和苹果一样 发布:2025-05-18 03:40:47 浏览:739
为什么安卓手机连不上苹果7热点 发布:2025-05-18 03:40:13 浏览:802
网卡访问 发布:2025-05-18 03:35:04 浏览:510
接收和发送服务器地址 发布:2025-05-18 03:33:48 浏览:371