使用标准C ++库字符串函数和字符串对象优化Simuli金宝appnk String块的生成代码。C ++字符串库提供了一致的C ++代码,并改进了功能,例如字符串长度检索,串联,字符串复制,字符串交换,字符串比较以及子字符串计算。
在模型中matlab: mStrfindSubStr,一个字符串常量块,具有“hello!”的值连接到str
字符串查找块的端口。Inport连接到亚
同一字符串的端口查找块。
模型='mstrfindsubstr';Open_System(型号);
在您的系统的临时文件夹中,为构建和检查过程创建一个临时文件夹。
CurrentDir = PWD;[〜,cgdir] = rtwdemodir();
在“配置参数”对话框中,设置语参数到C ++
。
set_param(型号,'targetlang'那'c ++');
在“配置参数”对话框中,设置代码接口包装参数到C ++类
。
set_param(型号,'codeinterfacepachaging'那'c ++ class');
在“配置参数”对话框中,设置标准数学库参数到C ++ 03(ISO)
。
set_param(型号,'targetlangstandard'那“c++ 03 (ISO)”);
构建的模型。
evalc('SLBUILD(MODEM)');
使用C ++ String库对象查看生成的代码。这是一部分mstrfindsubstr.h.
。代码实例化了std:string对象In1
。
cfile = fullfile(cgdir,'mstrfindsubstr_ert_rtw'那'mstrfindsubstr.h');RTWDEMODBTYPE(CFILE,'外部输入'那'外部输出',0,0);
struct extu_mstrfindsubstr_t {std :: string in1;//'/ in1'};
使用C ++字符串库函数查看生成的代码。这是一部分mstrfindsubstro.cpp.
。代码使用找
用于查找第一个字符匹配的函数,它存在于C ++字符串库中。
cfile = fullfile(cgdir,'mstrfindsubstr_ert_rtw'那'mstrfindsubstr.cpp');RTWDEMODBTYPE(CFILE,'模型步函数'那'模型初始化函数',0,0);
void untitled1modelclass :: step(){uint32_t tmpout;// stringfind:'/ string find'包含:// inport:' / / in1'// stringConstant:' / string常量'tmpout = mstrfindsubstr_p.stringconstant_string.find(mstrfindsubstr_u.in1);if(tmpout ==(uint32_t)std :: string :: npos){//出口:' / out1'mstrfindsubstr_y.out1 = -1;} else {//出口:' / out1'mstrfindsubstr_y.out1 = static_cast (tmpout)+ 1;stringfind的} //结尾:' / string find'}
此代码执行更快,更易于阅读。关闭模型和代码生成报告。
bdclose(模型);Rtwdemoclean;CD(CurrentDir);
限制:
软件循环(SIL)和处理器 - 循环(PIL)模拟中不支持包含STD:String的总线数组。金宝app