Boost logo

Boost Users :

Subject: [Boost-users] [boost][lambda] How to adapt container objects?
From: Peter Barnes (pdbj_at_[hidden])
Date: 2010-06-22 13:19:28


I need to create an index using a std:vector as the sort key.
boost::lamda and std::sort seem just the thing:

// Data vectors
//
// Goal is to sort index based on values of random
//
std::vector<int> index(10); // 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
std::vector<int> random(10); // random values, not in order

namespace bll = boost::lambda ;

// Sorted through vector pointer
//
// This compiles
//
std::vector<int> * ranp = & random;
std::sort(index.begin(),
          index.end(),
            bll::var(*ranp)[bll::_1]
          < bll::var(*ranp)[bll::_2]
          );

In reality, my std::vector is wrapped in another object (for reasons
given below, but unrelated to this question), with an operator[] to
retrieve contents:

// Container wrapper
class Container
{
std::vector<int> _v;
public:
Container(std::vector<int> v) : _v(v) { } ;
const int operator[](const int i) { return _v[i]; }
};

// Sorted through container object
//
// This *does not* compile
//
Container * cvp = new Container(random);
std::sort(index.begin(),
          index.end(),
            bll::var(*cvp)[bll::_1]
          < bll::var(*cvp)[bll::_2]
          );

/* Compiler error (g++ 4.01 (Apple)):
obj-indexer.cc:95: instantiated from here
boost/lambda/detail/operator_lambda_func_base.hpp:137: error:
conversion from 'const int' to non-scalar type
'boost::lambda::detail::unspecified' requested
*/

I don't understand the error message, and can't make out from the
documentation why this doesn't work. Do I need to extend the return
type deduction system somehow? But operator[] is not one of the
action types.

Thanks for your help,
Peter

Aside: Why do I wrap std::vector inside Container?

I'm memory constrained, so I combine a std::vector<uint16_t> with a
std::map to catch the overflows. The operator[i] then computes

int ret = vector[i] +
        ( (o = map.find(i)) != map.end() ? o->second * 2^16 : 0);

____________
Peter Barnes
pdbj_at_[hidden]

____________
Peter Barnes
pdbj_at_[hidden]


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net