简体   繁体   English

C ++中的数据结构

[英]Data Structure in C++

I am working on a problem and implementing an algorithm in C++. 我正在研究一个问题,并在C ++中实现算法。 The algorithm requires a data structure in which it is similar to a 2D array (say a 20x20 array). 该算法需要一个类似于2D数组(例如20x20数组)的数据结构。 The main different is that every cell must connect to the eight neigbours around it (ie up, down, left, right and four corners). 主要区别在于每个单元必须连接到它周围的八个近邻(即上,下,左,右和四个角)。

The status of each member will change based on the data changes of the neighbors. 每个成员的状态将根据邻居的数据变化而变化。 So, every cell grow dynamically. 因此,每个单元都动态增长。 Each cell needs to check the data of all its neighbours constantly. 每个单元都需要不断检查其所有邻居的数据。

Based on that requirement, I imagine this data structure is circular, like a torus or a bagel, which has no edge, so that every cell is interconnected to each other. 基于这一要求,我可以想象这种数据结构是圆形的,就像一个圆环或百吉饼一样,没有边缘,因此每个单元相互连接。

Any idea on the representation of this data structure? 关于这种数据结构的表示有什么想法吗? I am thinking to use a graph of adjacency linked list, in which each member contains a linked list of the surrounding eight neighbors. 我正在考虑使用邻接链表图,其中每个成员都包含周围八个邻居的链表。 What do you think? 你怎么看? Am I on the right track? 我在正确的轨道上吗?

Look for an implementation of the Game of Life, which does this with zeros and ones. 寻找生命游戏的实现,该实现使用零和一。 Unless you are doing something very sophisticated by converging a solution to a set of constraints for each iteration, you will just loop through your array each time, referencing the last complete generation, and update everything to create the new generation at the end of each loop. 除非您通过将解决方案收敛到每次迭代的一组约束条件来做非常复杂的事情,否则您每次都将遍历数组,引用最后一个完整的代,并在每次循环结束时更新所有内容以创建新一代。

Mostly it depends on your problem, but I have my doubts regarding adjacency linked list. 通常,这取决于您的问题,但是我对邻接链表有疑问。 That would be more suitable if your neighbours were growing dynamically, but in this case they seem to be fixed. 如果您的邻居动态增长,那将更合适,但在这种情况下,他们似乎是固定的。 So you might as well just use an array that points to your neighbours. 因此,您也可以只使用一个指向邻居的数组。

Problem statement seems not very clear: 问题陈述似乎不太清楚:

The status of each member will change based on the data changes of the neighbors. 每个成员的状态将根据邻居的数据变化而变化。 So, every cell grow dynamically. 因此,每个单元都动态增长。 Each cell needs to check the data of all its neighbours constantly. 每个单元都需要不断检查其所有邻居的数据。

what does this actually mean? 这到底是什么意思? Lets assume just one value changes. 假设只有一个值更改。 Then all the neighbours should change, and all their neighbours change again, until ALL values have changed. 然后,所有邻居都应该更改,并且所有邻居都再次更改,直到所有值都已更改。 But what about the original value, after a change it's neighbours change, should it change again in response (and keep changing indefinitely - sounds like a bad idea)? 但是原始值又如何呢?在更改之后,它的邻居更改了,它是否应该再次更改以作为响应(并无限期地更改-听起来像个坏主意)?

What about this: we have a simple case of a 1x4 2D array ABCD where A is a neighbour of B and D, and B of A and C, etc. 怎么办:我们有一个简单的1x4 2D数组ABCD案例,其中A是B和D的邻居,A是C和C的邻居,等等。

Say A changes. 说出变化。 So should B and D. Now, C should change - should it instantly change based on both changes in B and D? B和D也应该如此。现在,C应该改变-它是否应该同时基于B和D的改变而改变? or B first, D second? 还是B第一,D第二? or what? 要不然是啥?

What is the meaning/definition of constantly and dynamically in your problem? constantly dynamically解决您的问题的意义/定义是什么? Are there time steps, eg 是否有时间步骤,例如

 time 1: a cell changes
 time 2: all immediate neighbours change simultaneously
 time 3: neighbours of immediate neighbours change
         (and what about the original cell at this point?)
 time 4: etc.

In most cases I would (as most others) suggest a 2D-array based structure, but with a setter method, which, upon invocation, would do change propagation atomically. 在大多数情况下,我会(与其他大多数情况一样)建议一种基于2D数组的结构,但使用setter方法,该方法在调用时会原子地更改传播。 But it really depends on your the definition of constantly and dynamically 但这确实取决于您constantly dynamically定义

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

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