Jump to content
  • Advertisement
Sign in to follow this  
Palidine

std::find on a vector of pointers

This topic is 4265 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey, What's the proper way to implement a std::find on a std::vector of pointers to a class when you want to search by index. example:
struct Poop
{
    int mID;
};

std::vector<Poop*> myPoops;

//what I want to do
int search = 4;
std::find( myPoops.begin(), myPoops.end(), search );

I can't figure out how to write the operator== overload so that this works... -me

Share this post


Link to post
Share on other sites
Advertisement
figured it out:


struct Finder
{
Finder(int id)
: mID(id)
{
}

bool operator()( Poop *test )
{
return test->mID == mID;
}

int mID;
};

std::find_if( myPoops.begin(), myPoops.end(), Finder(4) );

Share this post


Link to post
Share on other sites
I was looking at this and thinking about boost::lambda, something I've been looking at recently. An alternative to the functor is this:


#include <vector>
#include <iostream>
#include <algorithm>
#include <boost/lambda/lambda.hpp>
using namespace boost::lambda;

struct Poop
{
Poop(int i) : id_(i) {}
bool operator==(int i) {return id_ == i;}
int id_;
};


int main()
{
std::vector<Poop*> myPoops;
myPoops.push_back(new Poop(1));
myPoops.push_back(new Poop(2));
myPoops.push_back(new Poop(3));

std::vector<Poop*>::iterator it = std::find_if(myPoops.begin(), myPoops.end(), *_1 == 2);
if(it != myPoops.end())
std::cout << "Success!\n";

delete myPoops[2];
delete myPoops[1];
delete myPoops[0];
myPoops.clear();

system("pause");
}




Jim.

Edit: Of course this only makes sense if defining operator== against an int is a natural operation for your class / struct. Maybe there is a boost::bind option for a general comparison function. Hmm, still figuring that out.

Share this post


Link to post
Share on other sites
And here is the answer using the standard function adaptors, and with boost::bind.


#include <vector>
#include <iostream>
#include <algorithm>
#include <functional>
#include <boost/bind.hpp>


struct Poop
{
Poop(int i) : id_(i) {}
int id_;
};


bool ComparePoopToInt(Poop* myPoop, int poorLonelyInt)
{
return myPoop->id_ == poorLonelyInt;
}


int main()
{
std::vector<Poop*> myPoops;
myPoops.push_back(new Poop(1));
myPoops.push_back(new Poop(2));
myPoops.push_back(new Poop(3));

std::vector<Poop*>::iterator it = std::find_if(
myPoops.begin(),
myPoops.end(),
std::bind2nd(std::ptr_fun(ComparePoopToInt), 2)
);
if(it != myPoops.end())
std::cout << "Success!\n";

it = std::find_if(
myPoops.begin(),
myPoops.end(),
boost::bind(ComparePoopToInt, _1, 2)
);
if(it != myPoops.end())
std::cout << "Success Again!\n";

delete myPoops[2];
delete myPoops[1];
delete myPoops[0];
myPoops.clear();

system("pause");
}



Jim.

Share this post


Link to post
Share on other sites
However, please first consider whether storing a container of pointers is really what you want to do, and if so, why.

Share this post


Link to post
Share on other sites
Perhaps this is exactly what Boost's indirect_iterator was made for?

Personally I always use containers of pointers. I just find it more convenient than any alternative.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!