【发布时间】:2016-10-04 06:21:17
【问题描述】:
我是一名转向 C# 的 C++ 程序员(真的很完整)。到目前为止,这是一个非常简单的过渡:)
我正在将一些代码(嗯,重写它)从 C++ 移植到 C#。我对如何移植以下 C++ STL 结构有很多可能性。这是我的 C++ 结构布局的 C++ 代码 sn-p(我没有费心显示枚举以节省混乱,但如果需要,我可以添加):
struct DeviceConnection_t
{
DeviceType_e device;
DeviceState_e state;
bool isPass;
DeviceConnection_t() :
device(DEV_TYPE_UNKNOWN),
state(DEV_STATE_DISCONNECTED),
isPass(false)
{}
};
struct Given_t
{
std::string text;
std::vector<DeviceConnection_t> deviceConnections;
};
struct Action_t
{
ActionEventType_e type;
uint32_t repeat_interval;
uint32_t repeat_duration;
DeviceType_e device;
bool isDone;
Action_t() :
type(AE_TYPE_UNKNOWN),
repeat_interval(0),
repeat_duration(0),
device(DEV_TYPE_UNKNOWN),
isDone(false)
{}
};
struct When_t
{
std::string text;
std::multimap<uint32_t, Action_t> actions; // time, action
};
所以这里我有一个 DeviceConnection_t 的向量,我在这里读到过:c-sharp-equivalent-of-c-vector-with-contiguous-memory 可以直接变成 C# List<DeviceConnection_t>。这似乎行得通,到目前为止一切顺利。
接下来是我的multimap<int, Action_t>,其中 int 是预期/允许重复条目的时间值。
我在这里读到:multimap-in-net 在 C# 中没有等价物,但有各种实现。
所以我可以使用其中一个,但我读到的其他问题如下:order-list-by-date-and-time-in-string-format 让我想到可能有更好的方法来实现我想要的。
我真正想要的是:
1.Action_t 的时间顺序列表 - 其中time 可能是 Action_t 的一个元素(我将它作为我的 c++ 中的一个元素删除,因为它成为我的多映射键)。我还需要能够搜索集合以查找时间值。
2. 某种默认构造函数来填充新实例化结构的默认值,但我也看不出这是如何完成的。
我真的很喜欢 Dictionary C# 类的外观,但我认为这不符合我目前的任何要求(这里可能有误)。
所以我的两个问题是:
- 创建按时间排序的对象集合的最佳方法是什么?
- 如何为结构的新实例分配默认值? (与 C++ 中的默认构造函数一样)?
【问题讨论】:
标签: c# c++ data-structures