小编Rei*_*ugh的帖子

指向两种不同结构的指针

我有三个结构header,dataAdataB.在header将确定将要使用的结构.在dataAdataB具有几乎相同的结构(比方说):

struct dataA
{
    int   intValue;
    char  reserved1[8];
    float floatValue;
    char  reserved2[4];
    short shortValue;
};

struct dataA
{
    int   intValue;
    short shortValue;
    char  reserved[2];
    float floatValue;
};
Run Code Online (Sandbox Code Playgroud)

我想打印它像:

sprintf(outStr, "%i, %f, %s", p->intValue, p->floatValue, p->shortValue);
Run Code Online (Sandbox Code Playgroud)

- 要么 -

sprintf(outStr, "%i, %f, %s", p.intValue, p.floatValue, p.shortValue);
Run Code Online (Sandbox Code Playgroud)

我该如何申报p?(注意:这两个dataAdataB具有大的结构,但几乎是相同的数据,除了那些被保留.值)

我在想这样的事情:

void * p;

if (header->type==1)
   p = (dataA*)(pData);
else if (header->type==2) …
Run Code Online (Sandbox Code Playgroud)

c++ pointers structure

2
推荐指数
1
解决办法
134
查看次数

矢量在地图中的地图

typedef vector<string>              tdv_Str;
typedef map<string, tdv_Str, CaseI> tdm_StrList; // var name, possible values
typedef map<short, tdm_StrList>     tdm_VarList; // type, var list
Run Code Online (Sandbox Code Playgroud)

*CaseI是字符串映射排序,不区分大小写.

当我检查tdm_VarList(list)的密钥是否存在时:

if (list.count(key) == 0)
// ...
Run Code Online (Sandbox Code Playgroud)

然后编译它,我得到以下错误:

1>c:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\xtree(1089): error C2220: warning treated as error - no 'object' file generated
1>          c:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\map(81) : see reference to class template instantiation 'std::_Tree<_Traits>' being compiled
1>          with
1>          [
1>              _Traits=std::_Tmap_traits<short,tdm_StrList,std::less<short>,std::allocator<std::pair<const short,tdm_StrList>>,false>
1>          ]
1> …
Run Code Online (Sandbox Code Playgroud)

c++ dictionary vector

0
推荐指数
1
解决办法
62
查看次数

标签 统计

c++ ×2

dictionary ×1

pointers ×1

structure ×1

vector ×1