迭代器vs指针类成员?

时间:2012-05-23 21:01:11

标签: pointers iterator

我想我刚刚发现问题(也许不是?),但我想了解原因。 以下是代码快照。我试图让它尽可能简单;

class FOO
{
public:
    int m_iNum; //sort according to this.
    std::vector<unsigned char>::iterator m_itContents; 
            //filling this contributes to issue with sorting in debug only? 
            //m_Contents does not come into other  sections below at all.
    unsigned char *m_Contents; //using this instead, 
                                       //no asserts or crash in debug.
};

class MyFile
{
public:
    std::vector<FOO> vFoo1; //this gets filled with file data.
};

std::vector<FOO> vFooVirtualList; //class member

int iColumnSel = -1;
int iSortResult1 = 1;
int iSortResult2 = -1;
int iVirtualListCount = 0;


void FillList()
{
    std::vector<Foo>::iterator itFoo;
    itFoo = MyFile->Foo1.begin() ;
    iVirtualListCount = 0;

    vFooVirtualList.clear();
    std::vector<Foo> vTemp;
    vTemp.swap( vFooVirtualList );
    vFooVirtualList.reserve(FileCount); // int filecount 

    for(int i=0;i< FileCount ;i++ ,itFoo++) 
            {
            vFooVirtualList.push_back(*itFoo);
            iVirtualListCount++;
    }
    // Tell the list box to update itself.
    m_List.SetItemCountEx(iVirtualListCount); 
            //m_List is CListCtrl, used as virtual list.
    m_List.Invalidate();
}

void OnLvnGetdispinfoMainframelist(NMHDR *pNMHDR, LRESULT *pResult)
{
    NMLVDISPINFO *pDispInfo = reinterpret_cast<NMLVDISPINFO*>(pNMHDR);
    LV_ITEM* pItem= &(pDispInfo)->item;
    std::vector<FOO>::iterator itFooList;
    itFooList = vFooVirtualList.begin() + pItem->iItem;
    CString str;

    if(pItem->mask & LVIF_TEXT)
    {
        // then display the appropriate column
        switch(pItem->iSubItem)
        {
        case 0:
            str.Format(L"%d",itFooList->m_iNum);  
            lstrcpy(pItem->pszText, str);
            break;

        default:
            break;
        }
    }
    *pResult = 1;
}

//sort by column
void OnLvnColumnclickMainframelist(NMHDR *pNMHDR, LRESULT *pResult)
{
    LPNMLISTVIEW pNMLV = reinterpret_cast<LPNMLISTVIEW>(pNMHDR);    
    for(iColumnSel = 0; iColumnSel < 1; iColumnSel++)
    {
        if(iColumnSel == pNMLV->iSubItem)
            SortByCol(iColumnSel);
    }
    // Redraw the list.
    m_List.Invalidate();        
    *pResult = 0;
}

//sorting done based only on Num.
int __cdecl CompareByFileNum(const void *elem1, const void *elem2)
{
    int result = 0;
    FOO *Item1 = (FOO*)elem1;
    FOO *Item2 = (FOO*)elem2;

    switch(iColumnSel)
    {
        case 0:
            if(Item1->m_iNum <  Item2->m_iNum)
                result = iSortResult1;
            if(Item1->m_iNum > Item2->im_Num) 
                result = iSortResult2;
            return result;
            break;
        default:
            break;
    }
    return 1;
}

void SortByCol(const int ColIndex)
{
    //fill list. sort. then load another file = crash. Only in debug mode?
    //if list is in proper order, no problem on new file load. otherwise crash.
    qsort( static_cast<void*>(&vFOOVirtualList[0]), iVirtualListCount, sizeof(FOO), CompareByFileNum);
    iSortResult1 = (iSortResult1*-1);
    iSortResult2 = (iSortResult2*-1);
}

想知道迭代器需要做些什么吗? FillList()这里工作正常,除了第二次调用(即新文件),如果virtuallist与第一次调用顺序不同,崩溃时使用新文件,但仅在调试模式下,&amp;只有我使用iterator&amp;不是指针。 我得到“iterator list corrupted or Unhandled exception at 0x0042c509”(略有变化)&amp;在任何一种情况下都参考xutility,分别是181或177行。

0 个答案:

没有答案