如何在使用此数组初始化对象时知道数组的大小?

时间:2023-02-06 21:45:26

the question says: define a container (possibly a template) of action objects (will be named Runner). Action Objects (AO) are objects that perform a user supplied function. The idea is that once an action container is loaded with AOs it can be asked to run or execute them. The results of the execution are returned in a Results array. example:

问题是:定义一个操作对象的容器(可能是一个模板)(将命名为Runner)。操作对象(AO)是执行用户提供的功能的对象。这个想法是,一旦一个动作容器加载了AO,就可以要求它运行或执行它们。执行结果将在Results数组中返回。例:

 AO a1(mul, x, x); //if run method is called it returns x*x
 AO a2(add, y, 1); //if run method is called it returns y+1
 AO a3(print, s); //if run method is called it prints s on cout

 Runner<AO> r = {a1,a2,a3};

so now int the runner class how should I implement a constructor that takes the constant array and knows it size

所以现在在转子类中我应该如何实现一个构造函数来获取常量数组并知道它的大小

 template<typename a>
 class runner{
 a* m;
 int size;
public:
runner(const a x[]){
//how to know the size of x[] to initialize m=new a[size] and then fill it with a1,a2,a3 ??
}

2 个解决方案

#1


An array can not know it's size and thus you should pass it as an argument to the constructor.
Even simpler: Use std::vector. With the handy std::initializer_list you can keep initializing your objects with the brace initializers {}. Here an example:

数组不能知道它的大小,因此你应该将它作为参数传递给构造函数。更简单:使用std :: vector。使用方便的std :: initializer_list,您可以使用大括号初始化器{}继续初始化对象。这是一个例子:

#include <iostream>
#include <vector>
#include <initializer_list>

template<typename a>
class runner{
private:
    std::vector<a> objects;
public:
    runner(std::initializer_list<a> init) {
         objects.insert(objects.end(), init.begin(), init.end());
    }
    void iterate() {
        for(auto obj : objects)
            std::cout << obj << std::endl;
    }
};

int main() {
    //As template type I've chosen int, you can choose your own class AO aswell
    //This calls the constructor with the std::initializer_list
    runner<int> test = {1,2,3,4,7,0};
    test.iterate();
    return 0;
}

#2


What u can do is push all the objects in a vector and then pass it in Ctor.

你可以做的是将所有对象推入向量中,然后在Ctor中传递它。

Follow these Steps :

跟着这些步骤 :

Include:

#include<vector>

In main(): Declare objects of class AO and push them to vector<AO> x

在main()中:声明类AO的对象并将它们推送到vector x

AO a1(mul, x, x); //if run method is called it returns x*x
AO a2(add, y, 1); //if run method is called it returns y+1
AO a3(print, s);

vector<AO> x;
x.push_back(a1);
x.push_back(a2);
x.push_back(a3);

Runner<AO> r(x);

Modify the template class

修改模板类

class Runner{
    a* m;
    int size;
public:
    Runner(vector<a> x)
    {
        m = new a[x.size()];
        for (int i = 0; i < x.size(); i++)
        {
            m[i] = x.at(i);
        }
    }
};

Hope this is actually what u want

希望这实际上是你想要的

#1


An array can not know it's size and thus you should pass it as an argument to the constructor.
Even simpler: Use std::vector. With the handy std::initializer_list you can keep initializing your objects with the brace initializers {}. Here an example:

数组不能知道它的大小,因此你应该将它作为参数传递给构造函数。更简单:使用std :: vector。使用方便的std :: initializer_list,您可以使用大括号初始化器{}继续初始化对象。这是一个例子:

#include <iostream>
#include <vector>
#include <initializer_list>

template<typename a>
class runner{
private:
    std::vector<a> objects;
public:
    runner(std::initializer_list<a> init) {
         objects.insert(objects.end(), init.begin(), init.end());
    }
    void iterate() {
        for(auto obj : objects)
            std::cout << obj << std::endl;
    }
};

int main() {
    //As template type I've chosen int, you can choose your own class AO aswell
    //This calls the constructor with the std::initializer_list
    runner<int> test = {1,2,3,4,7,0};
    test.iterate();
    return 0;
}

#2


What u can do is push all the objects in a vector and then pass it in Ctor.

你可以做的是将所有对象推入向量中,然后在Ctor中传递它。

Follow these Steps :

跟着这些步骤 :

Include:

#include<vector>

In main(): Declare objects of class AO and push them to vector<AO> x

在main()中:声明类AO的对象并将它们推送到vector x

AO a1(mul, x, x); //if run method is called it returns x*x
AO a2(add, y, 1); //if run method is called it returns y+1
AO a3(print, s);

vector<AO> x;
x.push_back(a1);
x.push_back(a2);
x.push_back(a3);

Runner<AO> r(x);

Modify the template class

修改模板类

class Runner{
    a* m;
    int size;
public:
    Runner(vector<a> x)
    {
        m = new a[x.size()];
        for (int i = 0; i < x.size(); i++)
        {
            m[i] = x.at(i);
        }
    }
};

Hope this is actually what u want

希望这实际上是你想要的