[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

Functor to find a key in a vector

John Doe

10/30/2008 5:34:00 PM

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 ?







2 Answers

Victor Bazarov

10/30/2008 6:04:00 PM

0

John Doe wrote:
> 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 ?

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

Alexey Stepanyan

11/2/2008 9:01:00 PM

0

On 30 ???, 21:03, Victor Bazarov <v.Abaza...@comAcast.net> wrote:
> 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;
};