- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
让我们考虑以下代码示例:
// Example program
#include <iostream>
#include <string>
#include <algorithm>
class A
{
public:
A( const std::string& name ) : name( name ) {}
inline const std::string& getName() const { return name; }
private:
std::string name;
};
int main()
{
std::vector<A> v;
v.push_back( A("b") );
v.push_back( A("a") );
// want to sort the container, based on it's name!
}
我知道如何做到这一点(定义内部或外部 operator<
,或声明一个提供 operator()
的仿函数结构/类并将其传递给 std::sort
函数),例如:
bool operator<( const A& a ) const
{
return getName() < a.getName();
}
但是,每次我想根据包含的类属性对容器进行排序时,我都懒得这样做,特别是当类为其提供 getter 函数时。
是否可以请求根据类成员函数结果值进行排序(如果这个有可用的比较器,显然)?
类似于:
std::sort( v.begin(), v.end(), &(A::getName) );
无需声明新的运算符或仿函数?
可选问题:如果 vector 包含指针 ( std::vector<A*> v
) 怎么办...单行语句可以根据 A::getName
对其进行排序吗结果?
最佳答案
您可以使用 lambda 函数对 vector 进行排序:
std::sort(v.begin(), v.end(), [] (A const& a, A const& b) {
return a.getName() < b.getName();
});
它比你的版本长,但你不需要在调用 sort
之前写任何东西。
如果你需要经常做那种事情,你可以把所有东西都放在一个小的仿函数中(并让它同时适用于A
、A*
和 smart指针),它使用 mem_fn
(感谢@T.C.):
template <typename T, typename R>
struct cmp_attr {
using fun_t = R (T::*) () const;
cmp_attr (fun_t g) : _g(g) { }
template <typename U>
bool operator() (U const& a, U const& b) const {
auto fn = std::mem_fn(_g);
return fn(a) < fn(b);
}
private:
fun_t _g;
};
// Utility function to have template parameters deduced, a like std::make_pair
template<typename T, typename R>
auto make_cmp_attr (R (T::*g) () const) {
return cmp_attr<T, R>(g);
}
然后:
struct A { const std::string& getName(); }
struct B: public A { }
std::vector<A> v1; // vector of A
std::sort(v1.begin(), v1.end(), make_cmp_attr(&A::getName));
std::vector<A*> v2; // vector of A*
std::sort(v2.begin(), v2.end(), make_cmp_attr(&A::getName));
std::vector<B> v3; // vector of child class
std::sort(v3.begin(), v3.end(), make_cmp_attr(&A::getName));
std::vector<B*> v4; // vector of pointer of child class
std::sort(v4.begin(), v4.end(), make_cmp_attr(&A::getName));
关于c++ - 根据 getter 成员函数对 STL 容器进行排序,无需编写额外的代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36932391/
我正在制作简单播放本地视频的应用程序。 一开始我用https://pub.dev/packages/video_player , video_player: ^0.10.2+1 但是由于某种原因,它在
基本上,我有一个 boolean 值,大型 API 项目中大约 20% 的类都使用它。从实用方法到更大的类,一切都使用它。我可以在程序启动时设置状态(不会改变),但不知道访问它的“最佳”方式。 最初,
我正在处理一些广告数据,例如电子邮件数据。我有两个数据集: 邮件级别,针对每个人,说明他们的邮寄日期,以及他们的转换日期。 import pandas as pd df_emailed=pd.Data
例如,我在 A 列中输入了数据,在 B 列中输入了一些复杂的公式作为 A 中数据的函数。A 中的数据行数取决于用户输入。它可以在 2 到 100,000 之间。传统上,我将使用相同的公式填充 B 列的
我正在寻找一种简单的时钟同步协议(protocol),该协议(protocol)易于实现且占用空间小,并且在没有互联网连接的情况下也可以工作,因此可以用于例如在封闭的实验室网络中。需要明确的是,我不是
这是 Objective-J/Cappuccino 的问题,但我添加了 cocoa 标签,因为框架非常相似。 Cappuccino 的缺点之一是 CoreData 尚未移植,因此您必须手动创建所有模型
例如,如果您按退格键,控制台会显示 keyVal 的空字符串,但这会产生误导,因为 keyVal.length 等于 1 还有一个隐藏字符 element.on('keydown',function(
我已经下载了一个主题,我想安装它。现在我位于“外观”>“主题”>“添加”>“新建/上传主题”。WordPress 需要 FTP 访问。好吧,我在本地计算机上,没有 FTP 服务器正在监听端口 21。
所以我认为我疯了,也许我疯了,但这看起来很简单。假设我有这段代码: let a = {}; a.b.c.d.e.f.g = 'Something Awesome'; 现在您可以想象如果检查噩梦就必须进
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 这个问题似乎不是关于 a specific programming problem, a software
这个问题已经有答案了: How set item checkbox when i click on element span which have this checkbox? (3 个回答) 已关闭
已关闭。这个问题是 not reproducible or was caused by typos 。目前不接受答案。 这个问题是由拼写错误或无法再重现的问题引起的。虽然类似的问题可能是 on-top
我已经为下拉菜单编写了一个自定义指令。这些元素绝对定位在相对定位的父元素内,因此我需要获取下拉触发元素的高度,以便将实际菜单移动到其下方。触发器是指令元素的子元素。我想避免使用成熟的 jQuery,而
我需要向端点提交表单,但由于我无法控制 CORS header ,因此无法使用 AJAX 执行此操作。 我目前正在通过渲染隐藏的 iframe 并将提交作为目标来执行此操作。但我仍然无法捕获该事件(我
我的 JSON 输入: { "Key": "Team", "Value": "AA" } { "Key": "Division", "Value": "BB" } 期望的输出: [
就目前情况而言,这个问题不太适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、民意调查或扩展讨论。如果您觉得这个问题可以改进并可能重新开放,visit
Pair: BUX/TIX Spread: 113 Rate: 10.159/10.272 High/Low: 115 我想获取值 113 和值 115,但
我正在尝试了解 IPB 论坛的运作方式。 如果我勾选记住我,那么即使我关闭浏览器并重新打开它,我也会保持登录状态。 我正在尝试弄清楚这是如何实现的,因为服务器设置的唯一 cookie 在 sessio
我一直在阅读有关 VIM 的 youcompleteme 插件的内容。然而,问题是我想要一个可以转移到其他开发平台(OpenIndiana、FreeBSD、Linux 和 OS X)上的设置。 使用
我需要找到 Excel 电子表格中的最后一个非空单元格,但我需要它的地址,而不是它的值。 例如:当我想要 K 列中最后一个非空单元格的值时,我使用以下公式: =LOOKUP(2;1/(NOT(ISBL
我是一名优秀的程序员,十分优秀!