Functor to find a key in a vector

J

John Doe

Hi,

I have a vector of struct called ViewList defined like this :

class CViewMgr : public CSingleton<CViewMgr>
{
friend class CSingleton<CViewMgr>;

// Constructor
CViewMgr(): m_pCurView(NULL) {}
CViewMgr(CViewMgr const&) {}
virtual ~CViewMgr(void) {}

protected:
CMobidjinnView m_MobidjinnView;


public:
struct ViewInfo
{
ViewInfo(const GString& view, const CViewBase* a_pViewBase)
: m_viewname(view), m_pView(a_pViewBase)
{
}

GString m_viewname;
const CViewBase* m_pView;
};
typedef std::vector<ViewInfo> ViewList;

void Init()
{
m_viewlist.push_back(ViewInfo(_T("SelectModuleView"), &m_MobidjinnView));
}

CViewBase* SwitchView(const GString& strViewName)
{
// I want to search in the ViewList an element
// with a m_viewname matching strViewName
CViewBase* pView = ?????????????????????????;
pView = find_if( m_viewlist.begin(), m_viewlist.end(),
... ) );

return NULL;
}

protected:
ViewList m_viewlist;
CViewBase* m_pCurView;

};


static inline CViewMgr& ViewMgr() { return *(CViewMgr::GetInstance()); }

and I would like to search my vector for a particular value matching the
m_viewname value.
How should I declare my functor ?
 
A

Alexey Stepanyan

Your functor needs to return 'true' when the value of the 'ViewInfo's
name coincides with the name the functor is given at its construction,
something like

š š šstruct EqualNameFunctor {
š š š š šGString nameToLookFor;
š š š š šEqualNameFunctor(GString const& n) : nameToLookFor(n) {}
š š š š šbool operator()(ViewInfo const& vi) {
š š š š š š šreturn vi.m_viewname == nameToLookFor;
š š š š š}
š š š};

and then call it like so

š šViewList::iterator found = find_if(m_viewlist.begin(),
š š š š š š š š š š š š š š š š š š š m_viewlist.end(),
š š š š š š š š š š š š š š š š š š š EqualNameFunctor(strViewName));
š špView = found == m_viewlist.end() ? NULL : found->m_pView;

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask- óËÒÙÔØ ÃÉÔÉÒÕÅÍÙÊ ÔÅËÓÔ -

- ðÏËÁÚÁÔØ ÃÉÔÉÒÕÅÍÙÊ ÔÅËÓÔ -

1) Wouldn't it be better to derive the pedicate from
std::unary_function<ViewInfo, bool> -
at least it is a standard practice.
2) bool operator()(ViewInfo const& vi) - should be const:
bool operator()(ViewInfo const& vi) const
{
return vi.m_viewname == nameToLookFor;
}
3) I would also have GString nameToLookFor as a private member.

struct EqualNameFunctor : public std::unary_function<ViewInfo, bool>
{
explicit EqualNameFunctor(GString const& n) : nameToLookFor(n)
{} // pls note explicit ctor

bool operator()(ViewInfo const& vi) const
{
return vi.m_viewname == nameToLookFor;
}

private:
GString nameToLookFor;
};
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top