具有通用输入参数的函数

Function with generic input parameters

本文关键字:参数 函数 输入      更新时间:2023-10-16

我对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);
}

现在我希望这个函数也能处理intdouble数组。对于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);
}

"现在我希望这个函数也能处理intdouble阵列。"

您可以使用模板化函数来处理不同类型的数组,并使用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。

它有优点也有缺点。例如,假设输入类型限于intfloat:

#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 }