将std :: map迭代对象存储为CGridCtrl中的itemdata [英] Storing std::map iteration object as itemdata in CGridCtrl

查看:122
本文介绍了将std :: map迭代对象存储为CGridCtrl中的itemdata的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在试图决定如何传达我的问题.

I have been trying to decide how to convey my problem.

我有一些地图:

typedef struct tagDemoEntryAssign
{
    COleDateTime datMeeting;
    CString      strAssignment;
    int          iAssignmentType; // AJT v16.0.9
    int          iStudyPoint;     // AJT v16.0.3
    int          iNextStudyPoint; // AJT v16.0.9
} S_DEMO_ENTRY_ASSIGN;

typedef std::vector<S_DEMO_ENTRY_ASSIGN> PublisherAssignments;

typedef struct tagDemoEntryEx
{
    CString     strName;
    E_GENDER    eGender;
    E_POSITION  ePosition;
    E_APPOINTED eAppointed;
    BOOL        bDemonstrations; // AJT v16.0.3

    PublisherAssignments  vectorItemAssign; // Sorted array of S_DEMO_ENTRY_ASSIGN structures.
} S_DEMO_ENTRY_EX;

typedef std::map<CString, S_DEMO_ENTRY_EX> PublisherMap;
typedef std::map<CString, S_DEMO_ENTRY_EX>::iterator PublisherMapIter;

我最终得到一些数据,并填写了CGridCtrl.我尝试设置单元格的项目数据.具体来说:

I end up having some data, and filling in a CGridCtrl. I try to set the item data for the cells. Specifically:

m_Grid.SetItemData(iRowCount - 1, DEMO_COLUMN_NAME, (LPARAM)&iter->second);

当我尝试访问LPARAM数据时无效.

When I try to access the LPARAM data is it not valid.

为什么?

我认为问题与这行代码有关:

I think the problem is related to this line of code:

S_DEMO_ENTRY_ASSIGN sAssign = iter->second.vectorItemAssign.back();

我将其分配为项目数据:

I assign it as item data:

m_Grid.SetItemData(iRowCount - 1, DEMO_COLUMN_LAST_USED, (LPARAM)&sAssign);

它随后在这里被使用:

int CALLBACK CDemoPickerDlg::pfnCellCompareDate(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort)
{
    CGridCellBase* pCell1 = (CGridCellBase*)lParam1;
    CGridCellBase* pCell2 = (CGridCellBase*)lParam2;
    if (!pCell1 || !pCell2) return 0;

    S_DEMO_ENTRY_ASSIGN *psItem1 = (S_DEMO_ENTRY_ASSIGN*)pCell1->GetData();
    S_DEMO_ENTRY_ASSIGN *psItem2 = (S_DEMO_ENTRY_ASSIGN*)pCell2->GetData();

    // If a name has never been used the structure pointer will be null.
    if (psItem1 == NULL && psItem2 == NULL)
        return 0;
    else if (psItem1 == NULL)
        return -1;
    else if (psItem2 == NULL)
        return 1;
    else if (psItem1->datMeeting < psItem2->datMeeting)
        return -1;
    else if (psItem1->datMeeting == psItem2->datMeeting)
        return 0;
    else
        return 1;

}

我认为这是错误的:

S_DEMO_ENTRY_ASSIGN sAssign = iter->second.vectorItemAssign.back();

如果我理解正确的力学原理,那么上面的内容就是对结构进行复制.然后,我分配该副本的指针.并且副本超出了地图的迭代循环范围.

If I understand the mechanics right, the above is making a copy of the structure. Then I am assigning the pointer of this copy. And the copy goes out of scope in the iteration loop of the map.

我认为我需要存储一个指向实际iter->second.vectorItemAssign.back()对象的指针.

I need to store a pointer to the actual iter->second.vectorItemAssign.back() object instead I think.

推荐答案

我不得不将一行代码更改为:

I had to change one line of code to:

m_Grid.SetItemData(iRowCount - 1, DEMO_COLUMN_LAST_USED, (LPARAM)&iter->second.vectorItemAssign.back());

这篇关于将std :: map迭代对象存储为CGridCtrl中的itemdata的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆