char achDeviceId[64]; // 设备id
char achDeviceName[64]; // 设备名称
int nDevType; // 设备类型
};
设备信息列表数据存放在vector列表vtDevList中:
vector vtDevList; // 存放设备信息的列表
比如我们需要按照设备名称做升序排列,相关代码如下所示:
std::sort( vtDevList.begin(), vtDevList.end(), [](TDeviceInfo& tDevInfo1, TDeviceInfo& tDevInfo2)->bool{
CString strDevName1 = CopyUtf8ToCStringT(tDevInfo1.achDeviceName);
CString strDevName2 = CopyUtf8ToCStringT(tDevInfo2.achDeviceName);
return (lstrcmp( strDevName1, strDevName2 ) < 0);
});
上述代码中使用到了C++11引入的匿名函数,即lamda表达式。在低版本的Visual Studio中是不支持C++11规范的,所以要专门写个比较函数,设置到sort接口中,如下所示:
BOOL NameCompare( TDeviceInfo& tDevInfo1, TDeviceInfo& tDevInfo2 )
{
CString strDevName1 = CopyUtf8ToCStringT(tDevIn