Functor to find a key in a vector

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • John Doe

    Functor to find a key in a vector

    Hi,

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

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

    // Constructor
    CViewMgr(): m_pCurView(NULL ) {}
    CViewMgr(CViewM gr 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_pView Base)
    {
    }

    GString m_viewname;
    const CViewBase* m_pView;
    };
    typedef std::vector<Vie wInfoViewList;

    void Init()
    {
    m_viewlist.push _back(ViewInfo( _T("SelectModul eView"), &m_MobidjinnVie w));
    }

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

    return NULL;
    }

    protected:
    ViewList m_viewlist;
    CViewBase* m_pCurView;

    };


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

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







  • Victor Bazarov

    #2
    Re: Functor to find a key in a vector

    John Doe wrote:
    Hi,
    >
    I have a vector of struct called ViewList defined like this :
    >
    class CViewMgr : public CSingleton<CVie wMgr>
    {
    friend class CSingleton<CVie wMgr>;
    >
    // Constructor
    CViewMgr(): m_pCurView(NULL ) {}
    CViewMgr(CViewM gr 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_pView Base)
    {
    }
    >
    GString m_viewname;
    const CViewBase* m_pView;
    };
    typedef std::vector<Vie wInfoViewList;
    >
    void Init()
    {
    m_viewlist.push _back(ViewInfo( _T("SelectModul eView"),
    &m_MobidjinnVie w));
    }
    >
    CViewBase* SwitchView(cons t GString& strViewName)
    {
    // I want to search in the ViewList an element
    // with a m_viewname matching strViewName
    CViewBase* pView = ??????????????? ??????????;
    pView = find_if( m_viewlist.begi n(), m_viewlist.end( ),
    ... ) );
    >
    return NULL;
    }
    >
    protected:
    ViewList m_viewlist;
    CViewBase* m_pCurView;
    >
    };
    >
    >
    static inline CViewMgr& ViewMgr() { return *(CViewMgr::Get Instance()); }
    >
    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 EqualNameFuncto r {
    GString nameToLookFor;
    EqualNameFuncto r(GString const& n) : nameToLookFor(n ) {}
    bool operator()(View Info const& vi) {
    return vi.m_viewname == nameToLookFor;
    }
    };

    and then call it like so

    ViewList::itera tor found = find_if(m_viewl ist.begin(),
    m_viewlist.end( ),
    EqualNameFuncto r(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

    Comment

    • Alexey Stepanyan

      #3
      Re: Functor to find a key in a vector

      On 30 ÏËÔ, 21:03, Victor Bazarov <v.Abaza...@com Acast.netwrote:
      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 EqualNameFuncto r {
      š š š š šGString nameToLookFor;
      š š š š šEqualNameFunct or(GString const& n) : nameToLookFor(n ) {}
      š š š š šbool operator()(View Info const& vi) {
      š š š š š š šreturn vi.m_viewname == nameToLookFor;
      š š š š š}
      š š š};
      >
      and then call it like so
      >
      š šViewList::iter ator found = find_if(m_viewl ist.begin(),
      š š š š š š š š š š š š š š š š š š š m_viewlist.end( ),
      š š š š š š š š š š š š š š š š š š š EqualNameFuncto r(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_func tion<ViewInfo, bool-
      at least it is a standard practice.
      2) bool operator()(View Info const& vi) - should be const:
      bool operator()(View Info const& vi) const
      {
      return vi.m_viewname == nameToLookFor;
      }
      3) I would also have GString nameToLookFor as a private member.

      struct EqualNameFuncto r : public std::unary_func tion<ViewInfo, bool>
      {
      explicit EqualNameFuncto r(GString const& n) : nameToLookFor(n )
      {} // pls note explicit ctor

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

      private:
      GString nameToLookFor;
      };

      Comment

      Working...