1

Is there an algorithm or helper method in the boost library to search a vector of boost::tuple objects? Here is my code:

typedef boost::tuple<int, char const*, char const*> Tuple;
typedef std::vector<Tuple> ErrorStringMap;

ErrorStringMap mystrings = tuple_list_of
    (10, "10", "ten") 
    (20, "20", "twenty") 
    (30, "30", "thirty") 
    (40, "40", "fourty");

I want to search the vector of tuples and find the first tuple with the value 20 as the first element in the tuple. I want to access that tuple's 2nd and 3rd elements.

ildjarn
  • 62,044
  • 9
  • 127
  • 211
void.pointer
  • 24,859
  • 31
  • 132
  • 243
  • If you're always looking up the element by the first element, consider using an `(unordered_)map` instead. – GManNickG Apr 11 '12 at 22:38
  • 2
    What's wrong with `std::find_if`? – ildjarn Apr 11 '12 at 22:38
  • @ildjarn I'd have to write my own functor for it, I believe. I was hoping boost already had a helper method that would return a reference to the tuple instead of an iterator (easier to work with). I can write my own solution easily enough I just wanted to make sure boost didn't have anything for this first. – void.pointer Apr 11 '12 at 22:43
  • 1
    I suppose you could do `std::find_if(mystrings.begin(), mystrings.end(), boost::bind(&Tuple::get<0>, _1) == 20)` instead of writing an entire functor... [Boost.Range](http://www.boost.org/libs/range/) may allow for something slightly cleaner, but I'm not aware of anything specific offhand. – ildjarn Apr 11 '12 at 22:46
  • @ildjarn I'm using boost lambda's bind, to implement your example, but I get: `error C2660: 'bind' : function does not take 2 arguments` – void.pointer Apr 11 '12 at 22:56
  • That sounds like you're picking up the wrong `bind`. Try fully qualifying it as well as the placeholder. (And Boost.Lambda is dead -- use Boost.Phoenix instead.) – ildjarn Apr 11 '12 at 22:59
  • @ildjarn I use boost.phoenix now and that specific error went away, now it says it can't find `_1`. I even do `using boost::phoenix::_1` and it still says undeclared identifier? – void.pointer Apr 11 '12 at 23:07
  • 2
    [Phoenix's placeholders are in `boost::phoenix::placeholders`](http://boost-sandbox.sourceforge.net/libs/phoenix/doc/html/phoenix/modules/core/arguments.html). Also, if you're using Phoenix, you don't need `bind` for this -- with the proper includes, `at_c<0>(_1) == 20` works. – ildjarn Apr 11 '12 at 23:10
  • @ildjarn: You should post that as an answer. – GManNickG Apr 11 '12 at 23:15
  • @GManNickG : I don't feel like typing all the details needed for a _proper_ answer. You can post it, I'll be happy to upvote it. :-] – ildjarn Apr 11 '12 at 23:17
  • @ildjarn: What if I'm just as lazy? :) I will sometime soon if nobody else does. – GManNickG Apr 12 '12 at 00:05
  • Guys, I need something to mark as an answer. ildjarn, please put something as an answer, even something short & sweet. I'll accept it, whatever you put. :-) – void.pointer Apr 16 '12 at 20:21

1 Answers1

0

ildjarn answered this in the list of comments below my question, however he did not post as an answer, so I will do it for him:

Phoenix's placeholders are in boost::phoenix::placeholders. Also, if you're using Phoenix, you don't need bind for this -- with the proper includes, at_c<0>(_1) == 20 works.

void.pointer
  • 24,859
  • 31
  • 132
  • 243