用于数组、向量、内置类型、STL 的 C++ 模板函数 [英] C++ template function for arrays, vectors, built in types, STL
问题描述
我想创建一个具有将发送给它的数据保存到文本文件的函数的类.可以传递给它的数据可以是 std::string
、int
、double
、float
之类的任何东西>、unsigned int
等,也可以是:std::vector
、T[]
和 std::vector
, T[][]
.
I would like to make a class that has a function that saves the data sent to it, to a text file. The data that can be passed to it can be anything like a std::string
, int
, double
, float
, unsigned int
, etc., and can also be an: std::vector<T>
, T[]
, and std::vector<std::vector<T>>
, T[][]
.
现在,显然如果它是一个数组,我将需要遍历它以将数据发送到文本文件.
Now, obviously if it is an array, I will need to iterate through it to send the data to text-file.
我想使用模板,但我不知道如何做数组.
I was thinking of using templates, but I'm not sure how to do the arrays.
这是要走的路吗?
class CMyClass
{
template<typename T>
void SaveData(T data);
void SaveData(std::vector<T> data);
void SaveData(std::string data);
void SaveData(T* data); // Perhaps I'll also need to pass in the size.
void SaveData(std::vector<std::vector<T>> data);
void SaveData(T** data);
};
我认为 std::string
的代码将与 std::vector
的代码相同,前提是 T
是一种内置类型(如 int
或 float
或其他东西).
I imagine that the code for the std::string
would be the same as the code for the std::vector<T>
provided that T
is a built in type (like an int
or float
or something).
然后我相应地为每个 SaveData(...)
编写函数?
And then I write the functions for each of the SaveData(...)
accordingly?
推荐答案
首先,您可以对类或函数进行模板化.由于您也想处理数组,因此您必须选择后一个选项.示例如下:
First, you can either template the class or the functions. Since you want to do arrays as well, you must go with the latter option. Example follows:
class CMyClass
{
public:
template<typename T> void SaveData(const T &data);
template<typename T, size_t N> void SaveData(const T (&data)[N]);
template<typename T, size_t N, size_t M> void SaveData(const T (&data)[N][M]);
template<typename T> void SaveData(const std::vector<T> &data);
template<typename T> void SaveData(const std::vector<std::vector<T> > &data);
void SaveData(const std::string &data);
};
定义函数后,以下示例显示了如何调用它们:
Once you have defined the functions, the following example shows how you can call them:
int i;
int i1[5];
int i2[5][7];
std::vector<int> v1;
std::vector<std::vector<int> > v2;
std::string s;
CMyClass saveClass;
saveClass.SaveData<int>(i);
saveClass.SaveData<int>(i1);
saveClass.SaveData<int>(i2);
saveClass.SaveData<int>(v1);
saveClass.SaveData<int>(v2);
saveClass.SaveData(s);
根据您的要求,您可以将类设为单例,将函数设为静态,完全不需要实例化 CMyClass,只需按如下方式调用函数:
Depending on your requirements, you could make the class a singleton and the functions static, omitting the need to instantiate CMyClass at all and simply calling the functions as follows:
CMyClass::SaveData<int>(i);
CMyClass::SaveData<int>(i1);
CMyClass::SaveData<int>(i2);
CMyClass::SaveData<int>(v1);
CMyClass::SaveData<int>(v2);
CMyClass::SaveData(s);
注意事项:
- 参数也应该是引用(即&data"而不是data"),这样每次调用函数时只传递引用而不是执行整个容器的副本.
- 我已经明确地将这些函数声明为 public,假设这是完整的类并且它的函数将被另一个类访问.默认情况下,类的成员是私有的.
- 确保每个嵌套的>"之间有一个空格.
祝你好运!
这篇关于用于数组、向量、内置类型、STL 的 C++ 模板函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!