试图使C++(MFC)代码片段易于重用.有什么选择
Trying to make a C++ (MFC) code snippet easy to reuse . What are the options?
我发现自己反复编写以下函数:
// This function will try to find the 'item' in 'array'.
// If successful, it will return 'true' and set the 'index' appropriately.
// Otherwise it will return false.
bool CanFindItem(data_type item, const data_type* array, int array_size, int& index) const
{
bool found = false;
index=0;
while(!found && i < array_size)
{
if (array[index] == item)
found = true;
else index++;
}
return found;
}
通常我会为每个类/结构等需要它的地方编写一个类似的函数。
我的问题是,有没有一种方法可以让这个片段在不重写的情况下随时使用?我正在VS 2010中编程。
您可以将其移动到.h文件并将template<typename data_type>
放在函数的前面,从而将其转换为模板。
您也可以切换到使用标准C++功能,例如std::find
算法。
即使在MFC中,您也可以使用现代(即1995年后)c++和STL构造
您可以使用std::find。。。链接中有一个使用数组的示例。
(std::find (array,array + array_size, item) != array + array_size);
相关文章:
- 如何将stdout重定向到stderr
- QTableView:endMoveRows在模型中重置水平页眉大小
- Visual studio代码重构似乎不起作用(例如,重命名符号-f2)
- 编写时C++中的输入重定向问题
- 如何避免在仅标头库中C++类/变量重定义
- 更改"child's parent's style sheet"时如何将孩子的样式表重置为 Qt 默认样式表?
- 这个带有模板<类 Vector 的C++代码片段有什么问题>
- 通过重命名C++中的符号来减小二进制大小
- 切换大小写后如何阻止变量重置?
- 获取通过重定向传入的输入文件的名称?
- Opengl 3.1 GLSL 140 在 C++ 年输出白色在片段着色器中
- 在头文件和 cpp 文件中使用一次 #pragma 时出现结构重定义错误
- C++:错误重定义和先前声明
- 函数之前的预期初始值设定项(易于修复?
- 避免模板类中的重定义错误
- 绕道重铸瓷砖层是怎么回事
- 如何以与 API 兼容的方式重命名类成员?
- 这两个代码片段相似,但显示的结果不同
- Windows C++重定向库
- 试图使C++(MFC)代码片段易于重用.有什么选择