具有通用输入参数的函数
Function with generic input parameters
我对c++还比较陌生。我写了函数WriteToFile
,它在文本文件中写入(路径由字符串a
指定)2D数组(按0行主顺序存储,x行,y列):
void WriteToFile(std::string a, const float *img, const int x, const int y) {
FILE *pFile;
const char * c = a.c_str();
pFile = fopen(c, "w");
for (int i = 0; i < x; i++){
for (int j = 0; j < y; j++)
fprintf(pFile, "%5.5ft", img[i*y + j]);
fprintf(pFile, "n");
}
fclose(pFile);
}
现在我希望这个函数也能处理int
和double
数组。对于int
,它将按原样打印数字,对于双%5.10lf
,必须在fprintf
中使用。我知道,这是绝对可能的。我发现了一些类似的东西,但不知道如何处理输入参数。当然,我可以写3个不同的函数,但我想学习如何写泛型函数。
感谢
您可以使用函数模板和一些辅助函数来获取格式字符串。
这是一个工作程序。
#include <cstdio>
#include <string>
template <typename T> char const* getFormatString();
template <> char const* getFormatString<int>()
{
return "%dt";
}
template <> char const* getFormatString<float>()
{
return "%5.5ft";
}
template <> char const* getFormatString<double>()
{
return "%15.10lft";
}
template <typename T>
void WriteToFile(std::string a, const T *img, const int x, const int y) {
FILE *pFile;
const char * c = a.c_str();
pFile = fopen(c, "w");
for (int i = 0; i < x; i++){
for (int j = 0; j < y; j++)
fprintf(pFile, getFormatString<T>(), img[i*y + j]);
fprintf(pFile, "n");
}
fclose(pFile);
}
int main()
{
int img1[] = {1, 1, 1, 1};
float img2[] = {1, 1, 1, 1};
double img3[] = {1, 1, 1, 1};
WriteToFile("int.img", img1, 2, 2);
WriteToFile("float.img", img2, 2, 2);
WriteToFile("double.img", img3, 2, 2);
}
"现在我希望这个函数也能处理
int
和double
阵列。"
您可以使用模板化函数来处理不同类型的数组,并使用c++标准I/O库
template <typename T>
void WriteToFile(std::string a, const T *img, const int x, const int y) {
std::ofstream file(a);
for (int i = 0; i < x; i++){
for (int j = 0; j < y; j++)
file << std::fixed << std::setw(5) << std::setprecision(5)
<< img[i*y + j] << "t" << std::endl;
}
file.close();
}
您可以考虑为不同的格式化需求提供专门的实现。
您可以选择重载和模板函数。我只想介绍一下C++11附带的一项功能。使用CCD_ 10。
它有优点也有缺点。例如,假设输入类型限于int
和float
:
#include <type_traits>
template <typename T>
void WriteToFile(std::string a, const T *img, const int x, const int y)
{
const char *format = std::is_same<T, int>::value? "%it" : "%5.5ft";
...
fprintf(pFile, format, img[i*y + j]);
...
}
请注意,这种方法是反对泛化的,并且您的代码不是尽可能通用的。
如果需要为每种类型使用不同的格式字符串,可以使用模板化函数并将格式字符串作为参数传入:
template<typename T> void WriteToFile(std::string a, const T *img,
const int x, const int y, std::string formatStr) {
...
fprintf(pFile, formatStr.c_str(), img[i*y + j]);
...
}
一个想法是
template <typename T>
void WriteToFile(std::string a, const T *img, const int x, const int y) {
}
然后是
inline
void write(FILE *file, double) { //write double }
inline
void write(FILE *file, int) { // write }
相关文章:
- 将可变参数函数的参数封装在类实例中
- QML 使用带有参数C++函数
- 使用可变参数函数作为模板参数
- 如何在C++中伪造虚拟可变参数函数模板?
- 为什么可变参数函数不适用于模板
- C++ std::functional 中的可变参数函数模板
- 可变参数函数指针的定义对于VxWorks spyLib来说不清楚
- 使用可变参数函数覆盖具有不同函数签名的虚函数
- 考虑引用和常量的可变参数函数包装器
- 使用可变参数函数将整数和/或整数数组放入单个 int 数组中
- 在可变参数函数中转发特定范围的参数
- 通过引用传递参数;函数返回类型是否必须为 VOID?
- 使用带有一个参数函数的递归找到数字的平方
- 可变参数函数模板不能很好地使用 std::function 作为参数
- 多个可变参数函数的单个模板参数包?
- 参数数据类型未知的可变参数函数
- 可变参数函数参数包扩展
- 使用模板可变参数函数将多个参数传递给另一个函数
- 对可变参数函数的递归调用的链接器错误
- 通过像printf这样的可变参数函数传递一个带有常量字符*转换函数的类