gpt4 book ai didi

C++ 模拟 Pascal 中的记录数组

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:11:21 25 4
gpt4 key购买 nike

我正在学习 C++。我的主要语言是 Free Pascal。在 FP 中我可以这样做:

type
TSomeType = (Foo1, Foo2, Foo3);

TSomeRecord = record
Field1: String;
Field2: String;
end;

var
MyArray: array[TSomeType] of TSomeRecord;

所以数组的长度为 3。然后我可以使用它,例如: MyArray[Foo2].Field1 := 'Some string' 我可以在 C++ 中做类似的事情吗?在C++数组教程中找不到解决方法

问候

最佳答案

C++ 中的等效代码如下所示:

enum { Foo1, Foo2, Foo3, N };

struct SomeRecord
{
string Field1;
string Field2;
}

SomeRecord array[N];

array[Foo2].Field1 = "Some String";

关于C++ 模拟 Pascal 中的记录数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16500039/

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