I want to sort a vector vec containing int iterators pointing to elements in another vector int_vec. I want to use the following compare function: it1 < it2 if and only if
index[it1 - int_vec.begin()] < index[it2 - int_vec.begin()].
Where index is a third vector specifying the key of an iterator. Now the vector index is an internal array of the constructor of A and int_vec is a member variable of a class A. I tried to just pass an anonymous function like this:
std::sort(vec.begin(),flow.end(), [&index,&edges](const int_iter it1 ,const int_iter it2) -> bool
{
index[it1 - int_vec.begin()] < index[it2 - int_vec.begin()];
})
but I get an error telling me that member objects cannot be captured. Exact error message is:
'this' cannot be implicitly captured in this context
index[it1 - int_vec.begin()] < index[it2 - int_vec.begin()];.
I also tried to just declare an external compare function but it is not clear to me how I can bind two fixed values to it(I read about boost::bind which looks like solving exactly this but I would prefer to not download additional libraries).
You have many problems there.
The most obvious one is that your code lacks [this]
.
vec.begin(),flow.end()
You can't take the beginning of one and the end of another vector.
This is the corrected code:
std::sort(vec.begin(),vec.end(), [this,&index,&edges](const int_iter it1 ,const int_iter it2) -> bool
{
index[it1 - int_vec.begin()] < index[it2 - int_vec.begin()];
})
However, you should tell us what you are trying to achieve and I'm sure we can find a better solution. Using vectors of iterators of other vectors is already very dangerous, doing subtractions on them without checking is just careless.
Less dangerous solution:
std::vector<int> int_vec;
std::vector<size_t> int_vec_order(int_vec.size());
std::iota(int_vec_order.begin(), int_vec_order.end(), size_t(0));
std::sort(int_vec_order.begin(), int_vec_order.end(), [&int_vec](const size_t a, const size_t b) {
// apply your order to int_vec.at(a) and int_vec.at(b)
});
// output them
for(const size_t i : int_vec_order) {
// output int_vec.at(i)
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.