本文介绍了地图中的结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我想做这样的事情: typedef struct { unsigned短信息; int SeqNr; } MsgStruct; map< string,MsgStructMyMsg; 现在我想知道这是如何与标准兼容的,以及我将如何直接访问结构变量? 会是这样的: MyMsg [" index"]。msg = 0xabcd;? 我不是百分百确定这是否可行并且我没有编译器方便 现在来测试一下。 感谢您的意见, Ron - 周软件工程安全时间计划;)Hi,I would like to do something like this:typedef struct{unsigned short msg;int SeqNr;} MsgStruct;map <string, MsgStructMyMsg;Now I''m wondering how this is compatible with the standard and how I wouldaccess the structs variables directly?Would it be something like this:MyMsg["index"].msg= 0xabcd;?I''m not 100% sure if this would work and i don''t have a compiler handy rightnow to test this.Thanks for your opinion,Ron--weeks of software enineering safe hours of planing ;)推荐答案 请考虑放弃这个C-ism。在C ++中我们只需编写 struct MsgStruct { unsigned short msg; int SeqNr; };Please consider abandoning this C-ism. In C++ we simply writestruct MsgStruct {unsigned short msg;int SeqNr;}; 兼容。存储项目的要求相对宽松,并且支持像您的结构一样的POD。项目 需要是可分配和可复制的。 POD是。It is compatible. The requirements for the stored items are relativelyrelaxed and such that PODs like your struct are supported. The itemsneed to be Assignable and Copyconstructible. PODs are. 这应该有效。That should work. 地图的索引返回对存储结构的引用。 在该引用上使用成员访问运算符是完全可以的,并且 也允许通过赋值操作更改成员。没问题 AFAICS。 V - 请删除大写''A'在回复电子邮件时 我没有回复最热门的回复,请不要问The indexing of your map returns a reference to the stored struct.Using the member access operator on that reference is perfectly OK, andchanging the member via the assignment op is allowed too. No problemAFAICS.V--Please remove capital ''A''s when replying by e-mailI do not respond to top-posted replies, please don''t ask 您可以随时使用像这样的在线编译器 http://www.comeaucomputing.com/tryitout/ 来测试你的代码。You could always use an online compiler like this one http://www.comeaucomputing.com/tryitout/to test your code. 这篇关于地图中的结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
07-05 11:22