Am I using the right find algorithm? 
Author Message
 Am I using the right find algorithm?

Hi,

I'm trying to figure out if there is a way to use the std. algorithms to
find an element inside a container based on one or more keys.
For instance:

My class could be something like the following

class CPerson
{
    public:
        CPerson(int iID, string sName)
        {
            m_iID = iID;
            m_sName = sName;
        }
    private:
        int        m_iID;
        string    m_sName;

Quote:
};

& I'm storing CPerson* inside the container

Now I would like to find out CPerson object that matches the supplied ID or
name.

Basically I'm looking for an algorithm that implements the following
functions:

CPerson* FindPersonByID(int iID)
CPerson* FindPersonByName(int sName)
...
it could be a find for any of the memeber variables of the class

Currently I'm using my custom find algorithm:

template <class T, class Container, class key>
T*    MyFind(const Container &container, const key &_key)
{
    typedef Container::const_iterator _II;

    T    *pT;
    _II    _F;
    _II    _L;

    _F = container.begin();
    _L = container.end();

    for (_F; _F != _L;  _F++)
    {
        if (T(*(*_F))(_key))
        {
            break;
        }
    }

    if (_F == _L)
    {
        pT = NULL;
    }
    else
    {
        pT = *_F;
    }

    return pT;

Quote:
}

My class will have overloaded function() for each of the keys I want my find
algo to work for.
bool operator()(int iID);
bool operator()(string sName);
...

Thanks.



Fri, 16 Dec 2005 17:22:17 GMT  
 Am I using the right find algorithm?
typedef bool (CPerson::*ID_Pred)(int);
typedef bool (CPerson::*Name_Pred)(string);

// in VC6, you need to use mem1_fun in place of mem_fun
find_if(c.begin(), c.end(),
bind2nd(mem_fun(static_cast<ID_Pred>(&CPerson::operator())), id));
find_if(c.begin(), c.end(),
bind2nd(mem_fun(static_cast<Name_Pred>(&CPerson::operator())), name));

It would be easier if you give different names to your comparison
functions instead of overloading operator() - also think what would
happen if you later need to search, say, on first name and last name,
both of which happen to be strings. It would be even easier if
comparison predicates were implemented as standalone functions or
functors, rather than member functions.
--
With best wishes,
    Igor Tandetnik

"For every complex problem, there is a solution that is simple, neat,
and wrong." H.L. Mencken


Quote:
> Hi,

> I'm trying to figure out if there is a way to use the std. algorithms
to
> find an element inside a container based on one or more keys.
> For instance:

> My class could be something like the following

> class CPerson
> {
>     public:
>         CPerson(int iID, string sName)
>         {
>             m_iID = iID;
>             m_sName = sName;
>         }
>     private:
>         int        m_iID;
>         string    m_sName;
> };

> & I'm storing CPerson* inside the container

> Now I would like to find out CPerson object that matches the supplied
ID or
> name.

> Basically I'm looking for an algorithm that implements the following
> functions:

> CPerson* FindPersonByID(int iID)
> CPerson* FindPersonByName(int sName)
> ...
> it could be a find for any of the memeber variables of the class

> Currently I'm using my custom find algorithm:

> template <class T, class Container, class key>
> T*    MyFind(const Container &container, const key &_key)
> {
>     typedef Container::const_iterator _II;

>     T    *pT;
>     _II    _F;
>     _II    _L;

>     _F = container.begin();
>     _L = container.end();

>     for (_F; _F != _L;  _F++)
>     {
>         if (T(*(*_F))(_key))
>         {
>             break;
>         }
>     }

>     if (_F == _L)
>     {
>         pT = NULL;
>     }
>     else
>     {
>         pT = *_F;
>     }

>     return pT;
> }

> My class will have overloaded function() for each of the keys I want
my find
> algo to work for.
> bool operator()(int iID);
> bool operator()(string sName);
> ...

> Thanks.




Fri, 16 Dec 2005 23:55:47 GMT  
 Am I using the right find algorithm?
I guess you can think about
2 containders:
map<iID, CPerson*>
and
map<sName, CPerson*>
of course with "at a time" insertion in both, so to keep them prppertly
linked to the CPreson instances
with not-too-much (only + sizeof(CPerson*)) additional memory allocation you
will leave the fast search to the map class..


Quote:
> Hi,

> I'm trying to figure out if there is a way to use the std. algorithms to
> find an element inside a container based on one or more keys.
> For instance:

> My class could be something like the following

> class CPerson
> {
>     public:
>         CPerson(int iID, string sName)
>         {
>             m_iID = iID;
>             m_sName = sName;
>         }
>     private:
>         int        m_iID;
>         string    m_sName;
> };

> & I'm storing CPerson* inside the container

> Now I would like to find out CPerson object that matches the supplied ID
or
> name.

> Basically I'm looking for an algorithm that implements the following
> functions:

> CPerson* FindPersonByID(int iID)
> CPerson* FindPersonByName(int sName)
> ...
> it could be a find for any of the memeber variables of the class

> Currently I'm using my custom find algorithm:

> template <class T, class Container, class key>
> T*    MyFind(const Container &container, const key &_key)
> {
>     typedef Container::const_iterator _II;

>     T    *pT;
>     _II    _F;
>     _II    _L;

>     _F = container.begin();
>     _L = container.end();

>     for (_F; _F != _L;  _F++)
>     {
>         if (T(*(*_F))(_key))
>         {
>             break;
>         }
>     }

>     if (_F == _L)
>     {
>         pT = NULL;
>     }
>     else
>     {
>         pT = *_F;
>     }

>     return pT;
> }

> My class will have overloaded function() for each of the keys I want my
find
> algo to work for.
> bool operator()(int iID);
> bool operator()(string sName);
> ...

> Thanks.




Wed, 21 Dec 2005 02:35:57 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Where do I find the id of the printer I am using

2. Utility to find out modified record using Checksum algorithm

3. Am I doing this right?

4. Am I working on the right lines... (long)

5. Am I heading right?

6. Window Classes: Am I doing this right?

7. Am I right wanting to learn C ?

8. CMutex - Am I doing this right?

9. CMutex - am I doing this right?

10. Customizing Open File Dialog -- am I right?

11. Customizing Open File Dialog -- am I right?

12. Am I right????

 

 
Powered by phpBB® Forum Software