下面代码怎么封装?

mohaixueyuan 2013-12-08 12:00:06
我的代码里面有很多

//省略代码
char szName[128] = {0};
for (int i=0; i<32; i++)
{
sprintf(szName, "Flash01_%02d.png", i+1);
//省略代码
}
//省略代码


里面除了sprintf产生的szName不一样,其他的完全一样。请教大牛们,这该怎么封装?
...全文
124 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
billzheng 2013-12-08
  • 打赏
  • 举报
回复
引用 4 楼 mohaixueyuan 的回复:
[quote=引用 3 楼 billzheng 的回复:] write a function to generate a vector of file names
std::vector<std::string> generateFileNames()
{ 
    std::string prefix("Flash01_"); 
    std::string ext("d.png");

    std::vector<std::string> files;

    for (int i=0; i<32; ++i)
    {
      std::string fn(prefix);
      fn += std::to_string(i);
      fn += ext;
      files.push_back(fn);
    }
    return files;
}
Then you operate on the vector if you need to: std::vector<std::string> files = generateFileNames(); for (std::vector<std::string>::iterator it = files.begin(); it != files.end(); ++it) { // do something. }
感谢billzheng的回答,你的这个想法我也考虑过,就是不知道有没更好的实现[/quote] The discipline of design is to make one function do one thing, make a class do one thing.
mohaixueyuan 2013-12-08
  • 打赏
  • 举报
回复
引用 3 楼 billzheng 的回复:
write a function to generate a vector of file names
std::vector<std::string> generateFileNames()
{ 
    std::string prefix("Flash01_"); 
    std::string ext("d.png");

    std::vector<std::string> files;

    for (int i=0; i<32; ++i)
    {
      std::string fn(prefix);
      fn += std::to_string(i);
      fn += ext;
      files.push_back(fn);
    }
    return files;
}
Then you operate on the vector if you need to: std::vector<std::string> files = generateFileNames(); for (std::vector<std::string>::iterator it = files.begin(); it != files.end(); ++it) { // do something. }
感谢billzheng的回答,你的这个想法我也考虑过,就是不知道有没更好的实现
billzheng 2013-12-08
  • 打赏
  • 举报
回复
write a function to generate a vector of file names
std::vector<std::string> generateFileNames()
{ 
    std::string prefix("Flash01_"); 
    std::string ext("d.png");

    std::vector<std::string> files;

    for (int i=0; i<32; ++i)
    {
      std::string fn(prefix);
      fn += std::to_string(i);
      fn += ext;
      files.push_back(fn);
    }
    return files;
}
Then you operate on the vector if you need to: std::vector<std::string> files = generateFileNames(); for (std::vector<std::string>::iterator it = files.begin(); it != files.end(); ++it) { // do something. }
mohaixueyuan 2013-12-08
  • 打赏
  • 举报
回复
想过这样封装

int* DTools::createSip(float delayPerUnit, const char* fmt, ...)
{
	//省略代码
	char szName[128] = {0};
 	for ( int i = 0; i < 32; i++ )
 	{
		va_list arg;
		va_start(arg, fmt);
		vsprintf(szName, fmt, arg);

		//省略代码
 	}

	//省略代码
}
但是调用的时候int*pResult = DTools::createSip(0.015f, "Flash01_%02d.png", i+1); 这时候还没有i。。。
DBG-CHEN 2013-12-08
  • 打赏
  • 举报
回复
帮你顶,不知道。

64,637

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧