在cpp中存储数组 [英] storing arrays in cpp

查看:92
本文介绍了在cpp中存储数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个公共类Globals,它显然拥有我所有的全球数据。


我有一个243项目的数组,目前的结构类型


typedef struct STAR

{

int x;

int y;

int stellar_class;

} t_star;


t_star StarArray [243] = {

{.....},

};

如果我在CI中执行此应用程序只需要将数组作为单独的

C源文件,编译它,并使用extern t_star StarArray []其中

我需要它。


在cpp中执行此操作的正确方法是什么?


对全球数据不满意,你不能把它放在班级

标题中,因为这是针对ansi约定的....


你是否应该在类头中定义空数组然后在类创建工具中填写它?
?什么浪费堆栈空间....


疑惑...


-

解决方案

*不锈钢:

我有一个公共类Globals,它显然拥有我所有的全球数据。

我有一个243项的数组,目前的结构类型

typedef struct STAR
{x /> int x;
int y;
int stellar_class;
} t_star;


1)风格:不要使用全部大写,除了宏,用C或C ++。

2)风格:不要用用于C ++代码中结构的typedef。


只需要执行


struct t_star {int x; int y; int stellar_class; };

t_star StarArray [243] = {
{.....},
};


如果这是一个常量,请使用''const''。


而不是说明元素的数量,计算它们。


t_star const StarArray [] = {...};

size_t const StarArrayCount = sizeof(StarArray)/ sizeof(* StarArray);

如果我在CI中执行此应用程序只是将数组作为单独的C源文件,编译它,并且有extern t_star StarArray []其中我需要它。

在cpp中执行此操作的正确方法是什么?


没有the正确的方法。


一个好的方法就是像在C中那样做。

全球数据不赞成,


全局_variables_不满意,有充分的理由。


你不能把它放在班级
标题中这样反对ansi约定....


没有类标题(大概你的意思是头文件)

和那里'没有像C ++编程的ansi约定那样。


你可以将数据定义放在头文件中。


最简单的方法就是声明数据''const'';然后它有

内部联动。


如果你想保证数据只在程序中出现一次

你可以使用一些技巧。


一个这样的技巧是一个返回数据引用的函数。

你应该在中定义空数组然后将类标题填入类创建工具中?多么浪费堆栈空间....




没有类创建工具这样的东西。在C ++中。


形式上甚至没有堆栈空间这样的东西,但为此它

是(与其他东西相比)不难理解

你的意思。


然而,你错了。


-

答:因为它弄乱了人们通常阅读文字的顺序。

问:为什么这么糟糕?

A:热门发布。

问:usenet和电子邮件中最烦人的事情是什么?


Stainless写道:< blockquote class =post_quotes>我有一个公共类Globals,它显然拥有我的所有全局数据。


不要把它变成公共课。

你可能会更好地使用命名空间。

我有一个243项的数组,目前的结构类型

typedef struct STAR
{
int x;
int y;
int stellar_class;
} t_star;

t_star StarArray [243] = {
{.....},
};

如果我在CI中执行此应用程序只会将数组作为单独的C源文件,编译它,并有外部t_star StarArray [],我需要它。

在cpp中这样做的正确方法是什么?


有几种方法。

有全局数据是不受欢迎的,你不能把它放在班级
标题中,因为这是反对ansi约定....

你应该在类头中定义空数组然后在类创建工具中填充它吗?多么浪费堆栈空间....




不,你应该让这些成员静态并在外面定义它们

的类定义,并在那里初始化。


V


>我有一个公共类Globals,它显然拥有我所有的全局数据。


我有一个243项的数组,目前的结构类型

typedef struct STAR
{x /
int y;
int stellar_class;
} t_star;

t_star StarArray [243] = {
{.....},
};

如果我在CI中执行此应用程序只是将数组作为单独的C源文件,请编译它in,并且有外部t_star StarArray [],我需要它。

在cpp中执行此操作的正确方法是什么?


好​​吧,我最有可能以错误的方式做这件事,但我喜欢使用

标准容器类而不是像结构一样的数组。

最简单(未经测试)的例子我可以提出:


Globals.h

------- ------

class CGlobals {

public:

CGlobals :: CGlobals();

CGlobals ::〜CGlobals();

struct STAR

{

int x;

int y;

int stellar_class;

} t_star;


}


foo.h < br $>
-------

包括Globals.h

class foo {

foo: :foo();

foo ::〜foo();


std :: list< CGlobals> StarArray;

}


foo.cpp

----------


static void main(){

CGlobals bar;

bar.t_star.x = 1;

bar.t_star .y = 2;

bar.t_star.stellar_class = 4;


StarArray.push_back(bar);

}


这样,你就可以拥有一个动态数组(在这种情况下是列表),并且仍然使用结构...


但是......如果你只在一个类中使用这个结构和数组,

将它声明为私有或受保护的成员,并以这种方式使用它。

如果你想让数组全局化,那就把它变成一个公共成员,只要你想使用它就只包括那个类头文件。


一种更简单的方法是使用你想要的三个int来创建一个类,

然后创建一个该类的std :: list。这将完全消除

结构。

C ++是惊人的,我希望我根本不会混淆你。我也是这样做的。

我自己。

您是否应该在类标题中定义空数组然后在类创建工具中填充它?什么浪费堆栈空间....


你使用什么IDE?

困惑...




如果你正在使用C,你不会长时间困惑:)。


-ben


I have a public class Globals, which obviously holds all my global data.

I have an array of 243 items, currently structs of type

typedef struct STAR
{
int x;
int y;
int stellar_class;
}t_star;

t_star StarArray[243]={
{.....},
};
If I was doing this app in C I would just have the array as a seperate
C source file, compile it in, and have extern t_star StarArray[] where
ever I needed it.

What''s the correct way to do this in cpp?

having global data is frowned upon, you can''t put it in the class
header as this is against ansi conventions....

Are you supposed to define the empty array in the class header then
fill it in the class creation tool? What a waste of stack space....

puzzled...

--

解决方案

* Stainless:

I have a public class Globals, which obviously holds all my global data.

I have an array of 243 items, currently structs of type

typedef struct STAR
{
int x;
int y;
int stellar_class;
}t_star;
1) Style: Don''t use all uppercase except for macros, in C or C++.
2) Style: Don''t use a typedef for a struct in C++ code.

Just do

struct t_star{ int x; int y; int stellar_class; };
t_star StarArray[243]={
{.....},
};
If this is meant to be a constant, use ''const''.

Instead of stating the number of elements, compute them.

t_star const StarArray[] = { ... };
size_t const StarArrayCount = sizeof(StarArray)/sizeof(*StarArray);
If I was doing this app in C I would just have the array as a seperate
C source file, compile it in, and have extern t_star StarArray[] where
ever I needed it.

What''s the correct way to do this in cpp?
There no "the" correct way.

A good way would be to do the same as you''d do in C.
having global data is frowned upon,
Having global _variables_ is frowned on, for good reasons.

you can''t put it in the class
header as this is against ansi conventions....
There''s no such thing as class header (presumably you mean header file)
and there''s no such thing as ansi conventions for C++ programming.

You can put data definitions in a header file.

The easiest way is to just declare the data ''const''; then it has
internal linkage.

If you want the data to be guaranteed to occur only once in the program
you can use a number of tricks.

One such trick is a function that returns a reference to the data.
Are you supposed to define the empty array in the class header then
fill it in the class creation tool? What a waste of stack space....



There''s no such thing as "class creation tool" in C++.

Formally there''s not even such a thing as stack space, but for that it
is (in contrast to the other things) not difficult to understand what
you mean.

However, you''re wrong about that.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?


Stainless wrote:

I have a public class Globals, which obviously holds all my global data.
Don''t make it a "public class". You''re probably going to be better off
with a namespace.
I have an array of 243 items, currently structs of type

typedef struct STAR
{
int x;
int y;
int stellar_class;
}t_star;

t_star StarArray[243]={
{.....},
};
If I was doing this app in C I would just have the array as a seperate
C source file, compile it in, and have extern t_star StarArray[] where
ever I needed it.

What''s the correct way to do this in cpp?
There are several ways.
having global data is frowned upon, you can''t put it in the class
header as this is against ansi conventions....

Are you supposed to define the empty array in the class header then
fill it in the class creation tool? What a waste of stack space....



No, you''re supposed to make those members ''static'' and define them outside
of the class definition, and initialise them there.

V


> I have a public class Globals, which obviously holds all my global data.


I have an array of 243 items, currently structs of type

typedef struct STAR
{
int x;
int y;
int stellar_class;
}t_star;

t_star StarArray[243]={
{.....},
};
If I was doing this app in C I would just have the array as a seperate
C source file, compile it in, and have extern t_star StarArray[] where
ever I needed it.

What''s the correct way to do this in cpp?
Alright, I most likely do this the wrong way, but I like to use
standard container classes rather than arrays for things like structs.
The simpliest (not tested) example of this I can come up with:

Globals.h
-------------
class CGlobals{
public:
CGlobals::CGlobals();
CGlobals::~CGlobals();
struct STAR
{
int x;
int y;
int stellar_class;
}t_star;

}

foo.h
-------
include "Globals.h"
class foo{
foo::foo();
foo::~foo();

std::list<CGlobals> StarArray;
}

foo.cpp
----------

static void main(){
CGlobals bar;
bar.t_star.x = 1;
bar.t_star.y = 2;
bar.t_star.stellar_class = 4;

StarArray.push_back(bar);
}

This way, you can have a dynamic array (list in this case), and still
use structs...

HOWEVER...if you''re only using this struct and array in one class,
declare it as a private or protected member, and utilize it that way.
If you want the array global, make it a public member, and just include
that class header file whenever you want to use it.

An easier way would be to make a class with the three int''s you want,
and then make a std::list of that class. This would eliminate the
struct altogether.

C++ is amazing and I hope I didn''t confuse you at all. I do that too
myself sometimes.
Are you supposed to define the empty array in the class header then
fill it in the class creation tool? What a waste of stack space....
What IDE are you using?
puzzled...



If you''re working with C, you won''t be puzzled for very long :).

-ben


这篇关于在cpp中存储数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆