动态模板数组 [英] Dynamic template arrays
问题描述
我想要一个模板类包含两个模板动态数组作为成员变量。我也使用一个成员函数来赋值给它们。但是我有代码的问题,我不知道原因(可能的语法)。在下面的代码中有什么问题?
template< typename T&
class ArrayHolder
{
public:
ArrayHolder();
void setArrays(T [],T [],int,int);
private:
T * array1;
T * array2;
};
template< typename T>
ArrayHolderHolder< T> :: setArrays(T firstarray [],T secondarray [],int N1,int N2)
{
array1 = new T [N1]
array2 = new T [N2];
}
setArrays,什么是最有效的将数组从参数(firstarray,secondarray)复制到它们?
ArrayHolderHolder< T> :: setArrays(T firstarray [],T secondarray [ ],int N1,int N2)
^^^^^^^^^^^^^^^
这里有一个拼写错误。类名是 ArrayHolder
,而不是 ArrayHolderHolder
。此外,您没有写入返回类型。
由于不能传递数组(按值),所以如果使用指针因为它是类模板,所以更喜欢在类本身中定义函数:
template< typename T> ;
class ArrayHolder
{
public:
ArrayHolder();
void setArrays(T * firstarray,T * secondarray,int N1,int N2)
{
array1 = new T [N1];
array2 = new T [N2];
//如果要复制,则使用`std :: copy`:
std :: copy(firstarray,firstarray + N1,array1);
std :: copy(secondarray,secondarray + N2,array2);
}
private:
T * array1;
T * array2;
};
顺便说一下,你可以使用 st ::向量
as:
std :: vector< T& array1;
std :: vector< T> array2;
在 setArrays的参数列表中也一样
以及。如果这样做,则 setArray
将变为:
void setArrays (const std :: vector< T& first,const std :: vector< T>& second)
{
//也许你需要这样做!
array1.clear();
array2.clear();
array1.insert(array1.end(),first.begin(),first.end());
array2.insert(array2.end(),second.begin(),second.end());
}
很简单,不是吗?没有内存分配,没有释放!
或者更好的是,如果你接受参数的值,那么你可以这样写:
void setArrays(std :: vector< T> first,std :: vector< T> second)
{
//不需要清除!
array1.swap(first);
array2.swap(second);
}
最后一个实现是一个惯用的解决方案, >
I want a template class to contain two template dynamic arrays as member variables. I use also a member function to assign to them values. However I got problem with the code and I do not know the reason (probably the syntax). What is the problem in the following code?
template<typename T>
class ArrayHolder
{
public:
ArrayHolder();
void setArrays( T [], T [],int,int);
private:
T *array1;
T *array2;
};
template<typename T>
ArrayHolderHolder<T>::setArrays(T firstarray[],T secondarray[] ,int N1, int N2)
{
array1 = new T[N1];
array2 = new T[N2];
}
After the initialization of the dynamic arrays in setArrays, what is the most effective to copy the arrays from the parameters (firstarray,secondarray) to them?
ArrayHolderHolder<T>::setArrays(T firstarray[],T secondarray[] ,int N1, int N2)
^^^^^^^^^^^^^^^^^
Here is one typo. The class name is ArrayHolder
, notArrayHolderHolder
. Also, you didn't write the return type.
Since you cannot pass array (by value), it is better if you use pointer notation function in parameter list, and since it is class template, prefer defining the functions in the class itself:
template<typename T>
class ArrayHolder
{
public:
ArrayHolder();
void setArrays(T *firstarray,T *secondarray ,int N1, int N2)
{
array1 = new T[N1];
array2 = new T[N2];
//if you want to copy, then use `std::copy` as:
std::copy(firstarray, firstarray + N1, array1);
std::copy(secondarray, secondarray + N2, array2);
}
private:
T *array1;
T *array2;
};
By the way, instead of raw arrays, you could use st::vector
as:
std::vector<T> array1;
std::vector<T> array2;
And same in the parameter list of setArrays
as well. If you do so, then setArray
would become this:
void setArrays(const std::vector<T> & first, const std::vector<T> & second)
{
//maybe you need to do this!
array1.clear();
array2.clear();
array1.insert(array1.end(), first.begin(), first.end());
array2.insert(array2.end(), second.begin(), second.end());
}
Simple, isn't it? No memory allocation, no deallocation!
Or even better, if you accept the arguments by value, then you could write this:
void setArrays(std::vector<T> first, std::vector<T> second)
{
//no need to clear!
array1.swap(first);
array2.swap(second);
}
The last implementation is an idiomatic solution, preferred by library implementers.
这篇关于动态模板数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!