将 boost::lambda 与 STL 容器结合使用
完整的代码位于 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
请记住,指向成员函数的指针和指向自由函数的指针具有不同的类型。尝试:
向量::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;
我通常只是声明一个转发函数来避免与此类事情相关的各种蠕虫病毒:
I usually just declare a forwarding function to avoid the various cans of worm associated with this sort of thing:
有什么理由不使用 lambda 吗?
Any reason why you do not use a lambda?