简体   繁体   English

Class 模板与数组或向量 arguments

[英]Class template with array or vector arguments

I need to write a class template definition with two template parameters (type, functor) and two template arguments (array/std::vector, int) that can execute the following code:我需要编写一个 class 模板定义,其中包含两个模板参数(类型、函子)和两个模板 arguments(array/std::vector, int),可以执行以下代码:

    const char* message= "Message";

    const transform<char, firstFunctor> first(message, lengthOfMessage);

    transform_view<int, secondFunctor> second(intArray, intSize);

    transform<double, thirdFunctor> third(doubleArray, doubleSize);

The type of the array/ vector has to match the type of the first template parameter.数组/向量的类型必须与第一个模板参数的类型相匹配。

I tried some variations like this:我尝试了一些这样的变体:

   template <typename A, typename B>
   class transform
   {
   public:
    transform<A, B>(A[], B) {...};
   }

But I could not get the constructor's first parameter to match all of the three types.但我无法让构造函数的第一个参数匹配所有三种类型。

Any advice is appreciated, thanks!任何建议表示赞赏,谢谢!

you wrote the definition of the constructor incorrectly.您错误地编写了构造函数的定义。

transform<A, B>(A[], B) {...}; you will pass a vector, so why did you write A[] as a parameter type?你会传递一个向量,那你为什么把A[]写成参数类型呢?

You need something like the following你需要类似下面的东西

#include <iostream>

template <typename T>
struct functor{
    void operator()(const T array [], size_t sze) {
        for (int i{}; i < sze; ++i) {
            std::cout << array[i] << " ";
        }
        std::cout << "\n";
    }
};

template<typename T, typename Function>
class transform {
    const T* array;
    size_t sze;
    Function functor{};
public:
    transform(const T array [], size_t sze):array{array}, sze{sze}{
        functor(array, sze);

    }
};

template< typename T, typename E>
using transform_view =  transform<T, E>;


int main()
{
    using firstFunctor = functor<char>;
    using secondFunctor = functor<int>;
    using thirdFunctor = functor<double>;
    const char *message = "Message";
    size_t lengthOfMessage = 7;

    int intArray[] = {1, 3};
    size_t intSize = 2;

    double doubleArray[] = {1.4, 3.2};
    size_t doubleSize = 2;

    //The given three lines
    const transform<char, firstFunctor> first(message, lengthOfMessage);

    transform_view<int, secondFunctor> second(intArray, intSize);

    transform<double, thirdFunctor> third(doubleArray, doubleSize);
}
The output
M e s s a g e
1 3
1.4 3.2

From what I understand, the problem is the first parameter being the array in the template.据我了解,问题是第一个参数是模板中的数组。 Try the following constructor:尝试以下构造函数:

transform(A* arr, B el){
    //now here is a tricky part, because there is a very big difference
    //between a char*, and an int/double/unsigned/float/... *.

}

If you have an array of the type A in the class and want to change it to the one passed:如果您在 class 中有一个类型为 A 的数组,并且想要将其更改为传递的数组:

private:
    A* my_array;

you can try sth like this:你可以这样尝试:

if(dynamic_cast<char*>(arr)) //if arr is of type char* {

    if (this->my_array != nullptr) delete my_array; //not needed if in a basic constructor...
    size_t len = strlen(arr);
    my_array = new char [len + 1];
    strcpy(this->my_array, arr); //(destination, source)
    my_array[len] = '\0';
} 
else //if it is a numeric array
{
this->my_array = arr;
//redirecting the pointers in enough
}

Oh and if you are on Visual Studio, strcpy will work if you write哦,如果你在 Visual Studio 上,如果你写 strcpy 将工作

'#pragma warning (disable: 4996)' at the top of the file.文件顶部的“#pragma 警告(禁用:4996)”。

Otherwise it marks it as unsafe and suggests strncpy, strcpy_s, ...否则,它会将其标记为不安全并建议 strncpy、strcpy_s、...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM