C# 如何在C/C++;?
我已经成功地在C#中使用了所谓的“Windows排序”,如下所示C# 如何在C/C++;?,c#,c++,windows,C#,C++,Windows,我已经成功地在C#中使用了所谓的“Windows排序”,如下所示 public class NativeMethods { [DllImport("shlwapi.dll", CharSet = CharSet.Unicode, EntryPoint = "StrCmpLogicalW")] public static extern int StrCmpLogical(string x, string y); } private class NaturalSortCompare
public class NativeMethods
{
[DllImport("shlwapi.dll", CharSet = CharSet.Unicode, EntryPoint = "StrCmpLogicalW")]
public static extern int StrCmpLogical(string x, string y);
}
private class NaturalSortComparer : IComparer
{
public int Compare(object a, object b)
{
return NativeMethods.StrCmpLogical((string)a, (string)b);
}
}
来自UNIX方面,我对微软的味道和C++的变化知之甚少,所以请容忍我。
如何使用C++方法对C++中的一些文件排序?如果有关系:我没有在这个特定的项目中使用MFC <>我的文件是在C++ >代码> STD::列表< /Cord>,我需要将它们排序如下(或类似): 尊敬的自己: 以下是解决方案:list<string> myFiles;
myFiles.push_back("10.somefilename.ext");
myFiles.push_back("9.somefilename.ext");
myFiles.sort(compare);
列出我的文件;
myFiles.push_back(“10.somefilename.ext”);
myFiles.push_back(“9.somefilename.ext”);
排序(比较);
此外:
bool compare(const string& first, const string& second)
{
wstring lhs = widen(first);
wstring rhs = widen(second);
bool ordered = StrCmpLogicalW(lhs.c_str(), rhs.c_str()) < 0;
return ordered;
}
bool比较(常量字符串和第一个、常量字符串和第二个)
{
wstring lhs=加宽(第一);
wstring rhs=加宽(秒);
bool ordered=StrCmpLogicalW(lhs.c_str(),rhs.c_str())<0;
订购退货;
}
最后:
wstring widen(string text)
{
locale loc("");
vector<wchar_t> buffer(text.size());
use_facet< std::ctype<wchar_t> > (loc).widen(text.data(), text.data() + text.size(), &buffer[0]);
return wstring(&buffer[0], buffer.size());
}
wstring加宽(字符串文本)
{
地点loc(“”);
向量缓冲区(text.size());
使用_facet(loc).加宽(text.data(),text.data()+text.size(),&buffer[0]);
返回wstring(&buffer[0],buffer.size());
}
你自己。你为什么要为此做一次品脱?应该为您这样做。只需基于
strmplogical
编写一个比较谓词,将其传递给sort
“为什么要为此进行PInvoke?”我需要以10开头的文件名。
放在9之后。
您建议的方法可以做到这一点吗?我想,如果您搜索该术语,会找到一些答案“自然排序”。例如,。你能在排序之前迭代myFiles
列表,然后加宽
每个字符串,生成一个可以排序的列表吗?如果你有任何类型的大列表(当前的算法可能会重复加宽),它可能比在比较过程中加宽要快得多。
wstring widen(string text)
{
locale loc("");
vector<wchar_t> buffer(text.size());
use_facet< std::ctype<wchar_t> > (loc).widen(text.data(), text.data() + text.size(), &buffer[0]);
return wstring(&buffer[0], buffer.size());
}