简体   繁体   English

如何有效地让主 function 可以使用这个变量?

[英]How to make this variable available to the main function efficiently?

I am doing some calculations, and I need to write a few large arrays (~100 elements each) by hand.我正在做一些计算,我需要手写几个大的 arrays(每个约 100 个元素)。 They look like this:它们看起来像这样:

int arr[] = {3, 4, 5,
             4, 9,
             3, 4, 6, 9};

Having multiple rows like this is important for readability, because the rows in the array correspond rows of a matrix.像这样有多行对于可读性很重要,因为数组中的行对应于矩阵的行。

To not clutter the main function, I would like to put these arrays in a header file.为了不弄乱主要的 function,我想将这些 arrays 放在一个 header 文件中。 As far as I know, there are two options:据我所知,有两种选择:

  1. Make these arrays global variables in the header file.在 header 文件中制作这些 arrays 全局变量。 I'd like to avoid global variables if possible.如果可能的话,我想避免使用全局变量。

  2. Make a function in the header file, like this:在header文件中制作一个function,像这样:

     void data(int *arr) { arr[0] = 3; arr[1] = 4; arr[2] = 5; arr[3] = 4; arr[4] = 9; arr[5] = 3; arr[6] = 4; arr[7] = 6; arr[8] = 9; }

and pass in arr from the main function. But this is very ugly, and time-consuming to write.并从 main function 传入arr 。但这非常难看,而且写起来很费时。

Is there any other solution?还有其他解决办法吗?

A solution just occurred to me, I can write the arrays as "global variables" inside the header file, but #include the file in the main function rather than at the top of the program.我刚刚想到一个解决方案,我可以将 arrays 写为 header 文件中的“全局变量”,但#include该文件在主 function 而不是程序的顶部。

You approach would differ depending on the logic of your program, and also whether you are going with C or C++. (In most cases people will get very angry if you tag both. see )您的方法会有所不同,具体取决于您的程序逻辑,以及您是使用 C 还是 C++。(在大多数情况下,如果您同时标记两者,人们会非常生气。 请参阅

If you have a set of many arrays, predefined at compile time, ie, contains some data at the start of your program, you can define them in a header file with reasonable names.如果您有一组许多 arrays,在编译时预定义,即在程序开始时包含一些数据,您可以在 header 文件中使用合理的名称定义它们。 If you wish, you can prefix them with an identifier, like myMats_mat1 , for your matricies to avoid naming conflicts.如果您愿意,可以为您的矩阵在它们前面加上一个标识符,例如myMats_mat1 ,以避免命名冲突。 C++ also presents a native way to accomplish this by namespace s. C++ 还提供了一种通过namespace实现这一点的原生方法。

Normally though, you would have a cleaner, neater logic.不过,通常情况下,您会有更清晰、更整洁的逻辑。 For instance, if you have a large amount of data, like your many arrays with hundreds of elements, you usually would store that in a file (a regular text file, or a binary file), and have functions to read it and write back to local containers when they are needed.例如,如果您有大量数据,例如包含数百个元素的许多 arrays,您通常会将其存储在一个文件(常规文本文件或二进制文件)中,并具有读取和写回的功能在需要时发送到本地容器。 This also allows you to both separate your data from your code, and occupy less memory since you only would work with a presumably smaller subset of the data.这还允许您将数据与代码分开,并占用更少的 memory,因为您只会使用可能较小的数据子集。

In most cases, you would also have multiple functions to do stuff with these arrays, perhaps you would have a function to compare two matricies, another to take determinant, another to sum all elements, whatever.在大多数情况下,您还会有多个函数来处理这些 arrays,也许您会有一个 function 来比较两个矩阵,另一个取行列式,另一个对所有元素求和,等等。 You would also group these together in seperate .h and .c files together with your arrays (in the .h file).您还可以将它们与 arrays(在.h文件中)一起分组到单独的.h.c文件中。 Again depending on your program, you may not even need to access the arrays themselves in your main function, and just call functions to do stuff on them.同样,根据您的程序,您甚至可能不需要在main function 中访问 arrays 本身,而只需调用函数对它们执行操作即可。 In such case, you can define your arrays as static , restricting their scope to that particular file, hence not really putting them global scope.在这种情况下,您可以将 arrays 定义为static ,将它们的 scope 限制在该特定文件中,因此不会真正将它们放在全局 scope 中。

In C++, you can also group them in a class, both your arrays and the related functions.在 C++ 中,您还可以将它们分组在一个 class 中,包括您的 arrays 和相关功能。 Its basically the same idea as above, just the C++ way of implementing it.和上面的思路基本一样,只是C++的实现方式而已。

I should stress that the ideas that I throw out there may be good, acceptable or outright horrible depending on your use case.我应该强调,根据您的用例,我在那里提出的想法可能是好的、可以接受的或完全可怕的。 Take all with a grain of salt.对所有事情持保留态度。

you can write in any file even an csv then you can use excel open and edit it,only number,like你可以在任何文件中写入 csv 然后你可以使用 excel 打开并编辑它,只有数字,比如

1,2,3,4,
5,6,7,8,
2,4,4,4

then in c file like然后在 c 文件中

int arr[] ={
#include"a.csv"
};

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

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