[boost][lambda] How to adapt container objects?
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
AMDG Peter Barnes wrote:
I need to create an index using a std:vector as the sort key. boost::lamda and std::sort seem just the thing:
<snip> // 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]; } };
<snip>
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.
It's other_action
participants (2)
-
Peter Barnes
-
Steven Watanabe