C++本身的结构?

发布于 2024-09-02 16:05:13 字数 326 浏览 5 评论 0原文

我一直在尝试将此代码移植到 python 中,但在 C++ 中有一些我不太理解的东西(我确实知道一点 C++,但这超出了我的范围):

typedef struct huffnode_s
{
    struct huffnode_s *zero;
    struct huffnode_s *one;
    unsigned char val;
    float freq;
} huffnode_t;

我不明白的是 huffnode_s 是如何在其中的就其本身而言,我以前从未见过这一点,也不太明白。这是什么意思?如果有人可以的话,Python 的等价物是什么?

I've been trying to port this code to python, but there is something I do not quite understand in C++ (I do know a bit of C++ but this is beyond me):

typedef struct huffnode_s
{
    struct huffnode_s *zero;
    struct huffnode_s *one;
    unsigned char val;
    float freq;
} huffnode_t;

What I don't get is how huffnode_s can be within itself, I've never seen this before and don't quite understand it. What does this mean, and if someone can, what would be the python equivalent?

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

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

发布评论

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

评论(8

绝影如岚 2024-09-09 16:05:13

huffnode_s 本身并不存在,只有指向 huffnode_s指针位于其中。由于指针的大小已知,因此没有问题。

huffnode_s isn't within itself, only pointers to huffnode_s are in there. Since a pointer is of known size, it's no problem.

也只是曾经 2024-09-09 16:05:13

这。

class Huffnode(object):
    def __init__(self, zero, one, val, freq):
        """zero and one are Huffnode's, val is a 'char' and freq is a float."""
        self.zero = zero
        self.one = one
        self.val = val
        self.freq = freq

然后,您可以将各种 C 函数重构为此类的方法。

或者也许是这个。

from collections import namedtuple
Huffnode = namedtuple( 'Huffnode', [ 'zero', 'one', 'val', 'freq' ] )

如果您希望 C 函数保持函数状态。

就是这样。

h0 = Huffnode(None, None, 'x', 0.0)
h1 = Huffnode(None, None, 'y', 1.0)
h2 = Huffnode(h0, h1, 'z', 2.0)

这就是所需要的全部。

This.

class Huffnode(object):
    def __init__(self, zero, one, val, freq):
        """zero and one are Huffnode's, val is a 'char' and freq is a float."""
        self.zero = zero
        self.one = one
        self.val = val
        self.freq = freq

You can then refactor your various C functions to be methods of this class.

Or maybe this.

from collections import namedtuple
Huffnode = namedtuple( 'Huffnode', [ 'zero', 'one', 'val', 'freq' ] )

If you want your C functions to remain functions.

That's it.

h0 = Huffnode(None, None, 'x', 0.0)
h1 = Huffnode(None, None, 'y', 1.0)
h2 = Huffnode(h0, h1, 'z', 2.0)

That's all that's required.

浮生面具三千个 2024-09-09 16:05:13

它本身没有结构。它有一个指向该结构的指针。

在内存中 struct huffnode_s 看起来像(32 位机器):


|-------------------- huffnode_s* 零 - 4 字节 ----- ---------|

|------------------ huffnode_s* 1 - 4 字节----------------|

|unsigned char val - 1 字节 + 3 字节填充=======|

|------------------- 浮点频率 - 4 字节 ------------------------- |


这些大小会因机器而异,并且它在内存中的外观由编译器决定。

it does not have a structure in itself. it has a pointer to that structure.

in memory struct huffnode_s would look like (32 bit machine):


|------------------ huffnode_s* zero - 4 bytes --------------|

|------------------ huffnode_s* one - 4 bytes----------------|

|unsigned char val - 1 byte + 3 bytes padding=======|

|------------------- float freq - 4 bytes -------------------------|


these sizes would vary machine to machine, and how it looks in memory is decided by compiler .

风和你 2024-09-09 16:05:13

为了补充 Carl 的答案,在 C++ 中同样的事情也是可能的:(

class Foo {
public:
    Foo() {}

    Foo *anotherFoo;
};   

注意上面的类很愚蠢,但重点是你可以在类类型的类中拥有一个指针)

To add to Carl's answer, the same thing in C++ is also possible:

class Foo {
public:
    Foo() {}

    Foo *anotherFoo;
};   

(Note the above class is silly, but the point is you can have a pointer inside a class that is of the class type)

在你怀里撒娇 2024-09-09 16:05:13

(struct huffnode_s *) 声明一个指向另一个结构的指针,该结构包含与其在其中声明的结构相同的变量。请参阅

(struct huffnode_s *) declares a pointer to another structure that includes same variables as the structure that it's declared in. See this question.

只有一腔孤勇 2024-09-09 16:05:13

这是一个指向 huffnode 内部的 huffnode 的指针。这意味着你可以说:

huffnode_t *node = ...;
huffnode_t *greatgreatgreatgrandchild = node->zero->zero->zero->zero->zero;

这将编译,并且只要所有这些 huffnode 后代实际上都被正确分配和指向,它就会工作。

指针很像 JavaScript 中的对象引用。它们实际上并不包含数据,它们只是引用它。请放心,您看到的不是无限类型。

This is a pointer to a huffnode inside of a huffnode. What this means is that you can say:

huffnode_t *node = ...;
huffnode_t *greatgreatgreatgrandchild = node->zero->zero->zero->zero->zero;

This will compile, and it will work as long as all those huffnode descendents are actually allocated and pointed to correctly.

Pointers are much like object references in JavaScript. They don't actually contain data, they just refer to it. Rest assured that you are not looking at an infinite type.

雨巷深深 2024-09-09 16:05:13

这称为自引用结构,正如它听起来的那样:包含对自身引用的结构。这种情况常见于描述链表节点的结构中。每个节点都需要对链中下一个节点的引用。

struct linked_list_node { 
    int data; 
    struct linked_list_node *next; // <- self reference 
}; 

This is known as a self referential structure and it is exactly what it sounds like: a structure which contains a reference to itself. A common occurrence of this is in a structure which describes a node for a linked list. Each node needs a reference to the next node in the chain.

struct linked_list_node { 
    int data; 
    struct linked_list_node *next; // <- self reference 
}; 
哎呦我呸! 2024-09-09 16:05:13

正如其他人所指出的,对自身的引用只是指向该结构的其他实例的指针。

结构内的指针允许将实例作为链接列表连接在一起。

As others have noted, the references to itself are simply pointers to other instances of that structure.

The pointers within the structure would allow one to connect instances together as a linked list.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文