STL list 使用find_if查找算法


set和multiset map和multimap 有成员函数find函数可快速查找


vector和list 没有find函数想要查找通过迭代器遍历


以下使用类重载运算符实现find_if快速查找:


typedef struct strTmpLinkMan

{

CString TmpLinkManName;

CString TmpLinkManeEmail;

}strTmpLinkMan;

typedef std::list<strTmpLinkMan> TmpLinkMan_t;

typedef std::list<strTmpLinkMan>::iterator TmpLinkManIter_t;


class FindthevalString

{

//STL查找TmpLinkManEmail是否含有

public:

FindthevalString(CString str) : m_LinkManEmail(str) {};

bool operator() (const strTmpLinkMan &t) const

{

return m_LinkManEmail == t.TmpLinkManeEmail;

}

private:

CString m_LinkManEmail;

};

typedef struct strTmpGroup

{

strTmpGroup()

{

pList = NULL;

}

CString strTmpGroupName;

CListUIEx* pList;

TmpLinkMan_t strTmpLinkList;

}strTmpGroup;


typedef std::list<strTmpGroup> strTmpGroupList_t;

typedef std::list<strTmpGroup>::iterator strTmpGroupIter_t;


class Findtheval

{

//STL查找CListUIEx* pList是否含有

public:

Findtheval(CListUIEx* x) : m_age(x) {};

bool operator() (const strTmpGroup &t) const

{

return m_age == t.pList;

}

private:

CListUIEx* m_age;

};



typedef struct stGroupUser_t

{

stGroupUser_t()

{

pTmpGroupList = NULL;

}

strTmpGroupList_t* pTmpGroupList;

}stGroupUser_t;


extern stGroupUser_t g_stTmpGroupInfo;


std::list<strTmpGroup>::iterator finded_pList = find_if(g_stTmpGroupInfo.pTmpGroupList->begin(), g_stTmpGroupInfo.pTmpGroupList->end(), Findtheval(pRightList));

if (finded_pList != g_stTmpGroupInfo.pTmpGroupList->end())

{

    //找到

    //查找是否有相同String LinkmanEmail

    std::list<strTmpLinkMan>::iterator finded_String = find_if(iterTmpGroupInfo->strTmpLinkList.begin(), iterTmpGroupInfo->strTmpLinkList.end(), FindthevalString(stTmpLinkMan.TmpLinkManeEmail));

if (finded_String == iterTmpGroupInfo->strTmpLinkList.end())

{

   //没有查找到

}

}

你可能感兴趣的:(return,find,public)