473,473 Members | 2,286 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Functor to find a key in a vector

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<ViewInfoViewList;

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 ?



Oct 30 '08 #1
2 3443
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<ViewInfoViewList;

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
Oct 30 '08 #2
On 30 ÏËÔ, 21:03, Victor Bazarov <v.Abaza...@comAcast.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 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;
};
Nov 2 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: porschberg | last post by:
Hi, I have a template class that looks like: template <class T> class Update { friend otl_stream& operator<< (otl_stream&, const shared_ptr<typename T::row>&); friend ostream& operator<<...
5
by: RCS | last post by:
I have this Functor: typedef std::vector < std::string > String_vec; typedef std::vector < String_vec > StrVec_vec; class Compare : public std::unary_function<String_vec, void> { public:...
5
by: Alex Vinokur | last post by:
Functor-parameters in the for_each() and transform() algorithms are passed by value. Might it make sense to have also algorithms for_each2() and transform2() which pass Functor-parameters by...
8
by: lok | last post by:
i have a class: template <class T1, class T2> class CPairMapping { public: typedef std::pair<T1, T2> ValuePair_t; typedef std::vector<ValuePair_t> ValueList_t; typedef std::binary_function<...
2
by: Charles-Antoine Giuliani | last post by:
Is it possible to overload multiple times operator() ? For example for using one or two arguments ? My visual c++ does not seem to like it : I was trying to implement an arbitrary integer...
11
by: James Aguilar | last post by:
In the C++ STL, there are various places where you can call a function that takes a functor as a parameter. This functor may be either a function object or a function pointer. For instance, if...
2
by: news.online.no | last post by:
Hi all Rather than writing std::binary:functions I thought it would be nice to create a comparison expression directly into the algorithm expression. Typical use is struct Person {...
3
by: chsalvia | last post by:
In generic programming, static member functions and functors seem to be very useful. I've noticed that some libraries take the approach of using templated static member functions to provide...
5
by: Fei Liu | last post by:
Hello, I have a situation where I need to design a library for multi-thread application, each thread does some work in a client supplied std::ptr_fun(free_function) or a functor. Now since it's...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.