gpt4 book ai didi

c++ - STL 容器与结构

转载 作者:行者123 更新时间:2023-11-30 01:09:34 25 4
gpt4 key购买 nike

我想知道什么更好用以及为什么:

之前我用STL容器创建类型,但最近我发现API使用类结构类型,我认为这是由于使用C而不是C++,但我想知道什么是最好的性能和内存。

之前:

typedef std::tuple<std::string, int, int, bool> SomeType;

现在:

typedef struct tagSomeType { std::string sVar; int iVar, iVar2; bool bVar; } SomeType;

谢谢

[编辑] 我放了这个元组的例子是为了让它可见,但是例如

typedef std::pair<int, int> iCoord; /* VS */ struct iCoord{int x, y;};

似乎唯一的区别是 struct 比 STL Container 更具可读性

最佳答案

内存使用和性能没有显着差异。所以主要标准应该是可读性。

一个区别是在 struct 中,每个元素都有一个名称,而 tuple 则不是这样。特别是如果您有超过 2 或 3 个元素,这可以使结构更具可读性。

顺便说一下,您的结构声明是 C 风格的。在 C++ 中,你可以这样写:

struct SomeType { std::string sVar; int iVar, iVar2; bool bVar; };

关于c++ - STL 容器与结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40060681/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com