将 boost::lambda 与 STL 容器结合使用

发布于 2024-12-14 00:27:48 字数 1565 浏览 0 评论 0原文

完整的代码位于 https://gist.github.com/1341623

我想对另一个向量的索引数组(或向量),使得该数组按另一个向量的索引排序。但是,无法解析 vector::at 的类型。

我做了如下尝试:

这是可以的

sort(v.begin(), v.end());

我想根据数组对索引进行排序,但占位符不会重载operator []

sort(index,index+10, a[_1] < a[_2]);

但是,它们重载operator +和operator *

sort(index,index+10, *(a+_1) < *(a+_2));

我想根据矢量,但编译器无法解析“vector::at”的类型。

sort(index,index+10,
  bind(&(vector<int>::at), &v, _1) < bind(&(vector<int>::at), &v, _2));
// error: no matching function for call
// to ‘bind(<unresolved overloaded function type>, ...

在网上搜索后,我发现我必须指定重载方法类型,但编译器仍然说它无法解析该类型。

sort(index,index+10,
   bind(&static_cast<const int (*)(size_t)>(vector<int>::at), &v, _1)
 < bind(&static_cast<const int (*)(size_t)>(vector<int>::at), &v, _2));
// error: invalid static_cast from type ‘<unresolved overloaded function type>’
// to type ‘const int (*)(size_t)’ ...

我尝试获取我想要的 vector::at 版本,但转换似乎失败。

vector<int>::const_reference (*vector_int_at)(vector<int>::size_type)(vector<int>::at);
sort(index,index+10,
  bind(&vector_int_at, &v, _1) < bind(&vector_int_at, &v, _2));
// error: no matches converting function ‘at’ to type ‘const int& (*)(size_t)’ ...

对于这个问题我能做什么呢?或者我误解了什么?

The complete code is on https://gist.github.com/1341623

I'd like to sort an index array (or vector) for another vector, such that the array is ordered by the index of the other vector. However, the type of vector::at cannot be resolved.

I did a try as follows:

This is OK

sort(v.begin(), v.end());

I'd like to sort the indexes according to the array, but placeholders do not overload operator[]

sort(index,index+10, a[_1] < a[_2]);

However, they overload operator+ and operator*

sort(index,index+10, *(a+_1) < *(a+_2));

I'd like to sort the indexes according to the vector, but compiler cannot resolve the type of `vector::at'.

sort(index,index+10,
  bind(&(vector<int>::at), &v, _1) < bind(&(vector<int>::at), &v, _2));
// error: no matching function for call
// to ‘bind(<unresolved overloaded function type>, ...

After searching on the web, I found I have to specify the overloaded method type, but compiler still says it cannot resolve the type.

sort(index,index+10,
   bind(&static_cast<const int (*)(size_t)>(vector<int>::at), &v, _1)
 < bind(&static_cast<const int (*)(size_t)>(vector<int>::at), &v, _2));
// error: invalid static_cast from type ‘<unresolved overloaded function type>’
// to type ‘const int (*)(size_t)’ ...

I tried to get the version of vector::at I want, but the conversion seems to be failed.

vector<int>::const_reference (*vector_int_at)(vector<int>::size_type)(vector<int>::at);
sort(index,index+10,
  bind(&vector_int_at, &v, _1) < bind(&vector_int_at, &v, _2));
// error: no matches converting function ‘at’ to type ‘const int& (*)(size_t)’ ...

What can I do for this problem? Or I misunderstand something?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

淤浪 2024-12-21 00:27:48

请记住,指向成员函数的指针和指向自由函数的指针具有不同的类型。尝试:
向量::const_reference (向量::*vector_int_at)(向量::size_type) const = &向量::at;

Remember that pointers to member functions and pointers to free functions have different types. Try:
vector<int>::const_reference (vector<int>::*vector_int_at)(vector<int>::size_type) const = &vector<int>::at;

澜川若宁 2024-12-21 00:27:48

我通常只是声明一个转发函数来避免与此类事情相关的各种蠕虫病毒:

int vector_at(vector<int> const * v, size_t index) { return v->at(index); }

...

sort(index, index+10, bind(vector_at, &v, _1) < bind(vector_at, &v, _2));

I usually just declare a forwarding function to avoid the various cans of worm associated with this sort of thing:

int vector_at(vector<int> const * v, size_t index) { return v->at(index); }

...

sort(index, index+10, bind(vector_at, &v, _1) < bind(vector_at, &v, _2));
太阳哥哥 2024-12-21 00:27:48

有什么理由不使用 lambda 吗?

sort(index, index+10, [&a](int i, int j) { return a[i] < a[j]; });

Any reason why you do not use a lambda?

sort(index, index+10, [&a](int i, int j) { return a[i] < a[j]; });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文