2011-04-25 61 views

回答

0

我終於明白了。這是功能。如果您有任何自己的頭文件,請將shlwapi.h和vcclr.h頭文件放在任何自己的頭文件之前,這一點至關重要。這是我一直在努力的問題。不完全明白爲什麼。如果有人有一個很好的解釋,歡迎發表評論。此外,如果有人知道如何將最後三行代碼合併到單個return語句中,歡迎添加註釋。

#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); 
     } 

    }; 
0

你是說你想使用該函數對字符串集合進行排序嗎?

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 ::排序使用該功能,假設你有一個std::vector<std::wstring>所謂的V:

std::sort(v.begin(), v.end(), mycomp); 
+0

也許,我應該更詳細地陳述我的問題。加載shlwapi.h的語法有困難。我不斷收到一堆鏈接錯誤。至於用於調用StrCmpLogicalW的語法,我沒有任何問題想通了。 – user523234 2011-04-25 18:54:51

+0

@ user523234:只需在鏈接器輸入文件列表中添加'shlwapi.lib'即可。 – 2011-04-25 19:06:41

+0

您是否熟悉這樣做的步驟? – user523234 2011-04-28 03:34:59