创建类数组

发布于 2024-11-04 02:50:33 字数 972 浏览 4 评论 0 原文

我有一个类,例如:

class me362
{
public:
    void geometry(long double xLength);
    void mesh(int xNode);
    void properties(long double H, long double D, long double K,long double Q, long double DT,long double PHO,long double CP, long double TINF);
    void drichlet(long double TLeft,long double TRight);
    void neumann(bool Tlinks, bool Trechts);
    void updateDiscretization(long double**** A,long double* b, long double* Tp);
    void printVectorToFile(long double *x);
private:
    int xdim;
    long double xlength;
    long double tleft;
    long double tright;
    long double h;
    long double d;
    long double k;
    long double q;
    long double dt;
    long double cp;
    long double rho;
    long double Tinf;
    bool tlinks;
    bool trechts;
};

我使用

me362 domain1;
me362 domain2;
me362 domain3;

但我想确定要初始化的域的数量来初始化它。所以我需要一个 me362 结构的动态数组。我怎样才能做到这一点?能做到吗?

谢谢大家,

埃姆雷。

I got a class such as:

class me362
{
public:
    void geometry(long double xLength);
    void mesh(int xNode);
    void properties(long double H, long double D, long double K,long double Q, long double DT,long double PHO,long double CP, long double TINF);
    void drichlet(long double TLeft,long double TRight);
    void neumann(bool Tlinks, bool Trechts);
    void updateDiscretization(long double**** A,long double* b, long double* Tp);
    void printVectorToFile(long double *x);
private:
    int xdim;
    long double xlength;
    long double tleft;
    long double tright;
    long double h;
    long double d;
    long double k;
    long double q;
    long double dt;
    long double cp;
    long double rho;
    long double Tinf;
    bool tlinks;
    bool trechts;
};

And I initialize it using

me362 domain1;
me362 domain2;
me362 domain3;

But I want to determine the number of domains that I want to initialize. So I need a dynamic array of me362 structures. How can I do that? Can it be done?

Thank you all,

Emre.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

看透却不说透 2024-11-11 02:50:33

是的,这是可以做到的。使用 std::vector 来代替,这会在每次 Push_back 操作时动态增加其大小。

std::vector<me362> obj ;

for( int i = 0; i < numberOfInstancesRequired; ++i )
{
    obj.push_back( me362() ) ;
}

Yes, it can be done. Use std::vector instead which increases it's size dynamically on every push_back operation.

std::vector<me362> obj ;

for( int i = 0; i < numberOfInstancesRequired; ++i )
{
    obj.push_back( me362() ) ;
}
鱼忆七猫命九 2024-11-11 02:50:33

使用 std::vector,它为您处理动态内存:

#include <vector>

// ...

std::vector<me362> domains;

std::vector 还具有许多不错的功能和保证,例如与 C 布局兼容、具有引用局部性、每个元素零开销等等。

另请注意, std::vector 有一个构造函数,该构造函数采用整数参数,并创建许多元素:

// Will create a vector with 42 default-constructed me362 elements in it
std::vector<me362> domains(42);

请参阅任何标准库参考(例如 cppreference.comcplusplus.com)了解有关使用 std::vector 的详细信息。)

Use std::vector, which handles dynamic memory for you:

#include <vector>

// ...

std::vector<me362> domains;

std::vector also has a lot of nice features and guarantees, like being layout-compatible with C, having locality of reference, zero overhead per element, and so on.

Also note that std::vector has a constructor that takes an integral argument, and creates that many elements:

// Will create a vector with 42 default-constructed me362 elements in it
std::vector<me362> domains(42);

See any standard library reference (like cppreference.com or cplusplus.com) for details about using std::vector.)

蹲墙角沉默 2024-11-11 02:50:33

首先,欢迎来到STL(标准模板库)的世界!

在您的情况下,您可以使用 std::vector ,因为它可以为您保存可变数量的元素。

#include<vector>
using namespace std;

//Create a std::vector object with zero size
vector<me362> myVector;

//Insert new items
myVector.push_back(domain1);
myVector.push_back(domain2);
myVector.push_back(domain3);

//Get the size of the vector, i.e., number of elements in vector
myVector.size();

此外,您可以像这样创建矢量对象。

//nMe362: number of elements in vector, me362Obj: init value of each me362 object
vector<me362> myVector(nMe362, me362Obj);

For starters, welcome to the world of STL(standard template library)!

In your case, you can use std::vector, as it can hold variable number of elements for you.

#include<vector>
using namespace std;

//Create a std::vector object with zero size
vector<me362> myVector;

//Insert new items
myVector.push_back(domain1);
myVector.push_back(domain2);
myVector.push_back(domain3);

//Get the size of the vector, i.e., number of elements in vector
myVector.size();

Besides, you can create a vector object like this.

//nMe362: number of elements in vector, me362Obj: init value of each me362 object
vector<me362> myVector(nMe362, me362Obj);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文