adding search capability to a Vector

K

Kei

Hello,

I am trying to add a simple search behaviour to a STL Vector like the
following:

//---- declaration ---

typedef struct {
std::string name;
std::string favouriteFood;
} TBuddy;
typedef TBuddy* PBuddy;

class TBuddies: public std::vector<PBuddy> {
public:
std::string favouriteFoodOf(const std::string& name) const;
};

//---- implementation ---

std::string TBuddies::findValue(const std::string& name) const
{
for(TBuddies::iterator i= this->begin(); i!= this->end(); ++i) {
PBuddy b = *i;
if(b->name ==s) return b->favouriteFood;
}
return "";
}



But it couldn't compile... There is a strange error saying "Invalid
conversion from TBuddy const* const to TBuddy**". But the "const"
keyword in the function is required.

Somehow, is the iterator trying to modify the instance ?

Thanks!
 
I

Im.fehead

Hello,

I am trying to add a simple search behaviour to a STL Vector like the
following:

//---- declaration ---

typedef struct {
        std::string name;
        std::string favouriteFood;} TBuddy;

typedef TBuddy* PBuddy;

class TBuddies: public std::vector<PBuddy> {
public:
        std::string favouriteFoodOf(const std::string& name) const;

};

//---- implementation ---

std::string TBuddies::findValue(const std::string& name) const
{
        for(TBuddies::iterator i= this->begin(); i!= this->end(); ++i) {
                PBuddy b = *i;
                if(b->name ==s) return b->favouriteFood;
        }
        return "";

}

But it couldn't compile... There is a strange error saying "Invalid
conversion from TBuddy const* const to TBuddy**". But the "const"
keyword in the function is required.

Somehow, is the iterator trying to modify the instance ?

Thanks!


findValue function is const function.

std::string TBuddies::findValue(const std::string& name) const

So

you have to change next context.

"TBuddies::iterator i= this->begin();" --> "TBuddies::const_iterator
i= this->begin();".
 
S

SG

I am trying to add a simple search behaviour to a STL Vector like the
following:

//---- declaration ---

typedef struct {
        std::string name;
        std::string favouriteFood;} TBuddy;

This struct isn't C compatible anyway so you might want to define it
the C++ way:

struct TBuddy {
//...
};
typedef TBuddy* PBuddy;

Why do you want to store pointers in the vector?
class TBuddies: public std::vector<PBuddy> {
public:
        std::string favouriteFoodOf(const std::string& name) const;
};

Why public inheritance? Why making your own class?
//---- implementation ---

std::string TBuddies::findValue(const std::string& name) const
{
        for(TBuddies::iterator i= this->begin(); i!= this->end(); ++i) {
                PBuddy b = *i;
                if(b->name ==s) return b->favouriteFood;
        }
        return "";
}

Since the member function is declared const, 'this' points to a const
TBuddies object. The functions begin() and end() are "const
overloaded". Their const versions return instances of const_iterator.

Anyways, inside findValue you don't need access to any protected
fields. You better make it a free function that calles std::find_if.

Or better yet: use a std::map. It looks like std::map (instead of
std::vector) is what you are looking for.

Cheers!
SG
 
I

Ian Collins

Kei said:
Hello,

I am trying to add a simple search behaviour to a STL Vector like the
following:

Your probably want to read up on std::find and if you are storing
pointers, std::find_if.
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top