在c++中使用StrCmpLogicalW函数进行自然排序
Natural sort order in C++ using StrCmpLogicalW function in this library shlwapi.dll
我尝试学习如何使用StrCmpLogicalW函数。c#中有一篇关于c#自然排序顺序的文章。但我正在寻找c++语法。
谢谢。
你是说你想使用该函数对字符串集合进行排序吗?
bool mycomp(PCWSTR lhs, PCWSTR rhs)
{
return StrCmpLogicalW(lhs,rhs) < 0;
}
或者如果你使用std::wstring
:
bool mycomp(const std::wstring & lhs, const std::wstring & rhs)
{
return StrCmpLogicalW(lhs.c_str(),rhs.c_str()) < 0;
}
然后你可以用这个函数调用std::sort,假设你有一个std::vector<std::wstring>
叫做v:
std::sort(v.begin(), v.end(), mycomp);
我终于弄明白了。这是函数。如果您自己有头文件,那么务必将shlwapi.h和vcclr.h头文件放在任何头文件之前。这是我一直在努力解决的问题。我不完全明白为什么会这样。如果谁有好的解释,欢迎发表评论。此外,如果有人知道如何将最后三行代码组合成单个返回语句,欢迎添加注释。
#include "shlwapi.h" //needed this for StrCmpLogicalW
#include <vcclr.h> //needed this for PtrtoStringChars
//your own header files
ref class FileInfoNameComparer: public IComparer
{
private:
virtual int Compare( Object^ x, Object^ y ) sealed = IComparer::Compare
{
FileInfo^ objX = gcnew FileInfo(x->ToString());
FileInfo^ objY = gcnew FileInfo(y->ToString());
pin_ptr<const wchar_t> wch1 = PtrToStringChars(objX->Name);
pin_ptr<const wchar_t> wch2 = PtrToStringChars(objY->Name);
return StrCmpLogicalW(wch1, wch2);
}
};
要使用StrCmpLogicalW,您可能还需要采取步骤在链接中包含相应的库。对我来说有效的一种方法是使用下面的#pragma和#include:
#pragma comment(lib, "Shlwapi.lib")
#include <shlwapi.h> //for StrCmpLogicalW
相关文章:
- 二叉排序树无法编译
- 仅使用绝对值对数组进行排序,并在C++中显示实际值
- C++选择排序算法中的逻辑错误
- 使用C++程序合并排序没有得到正确的输出
- 计算排序向量的向量中唯一值的计数
- 排序算法c++
- 使用2个键的cpp-stl::优先级队列排序不正确
- 将结构向量排序为子组
- 在c++中尝试对对象数组进行排序时,出现std:bad_alloc错误
- 如何对点云数据进行排序
- 对字符串进行排序时,在c++中处理sort()
- 是否有类似std::lower_bound的函数,而不需要排序/分区输入
- 下面是排序算法O(n)吗
- 在自定义程序中使用本机 Windows 自然顺序排序
- 使用 Qt 对文件名进行自然排序
- C++中目录文件名的自然排序
- 自然排序的带有数字的文件名发出c++
- 没有自然排序顺序的两个std::向量之间的差
- 归并排序函数(自然归并排序)
- 在c++中使用StrCmpLogicalW函数进行自然排序