問題1>C++ - 爲什麼叫lexicographical_compare時並不適用於ciCharLess ptr_fun
我想知道爲什麼在我的情況下,我們必須使用NOT2(ptr_fun(ciCharCompare)),而在情況二,我們只需要使用ciCharLess。爲什麼不使用ptr_fun(cICharLess)取而代之?
問2>
有沒有辦法,我可以按照時使用的形式的一般規則?
謝謝
case I:
int ciCharCompare(char c1, char c2)
{
// case-insensitively compare chars c1 and c2
// return -1 if c1 < c2
// return 0 if c1 == c2
// return 1 if c1 > c2
}
string s1("Test 1");
string s2("test 2222222222222222");
pair<string::const_iterator, string::const_iterator>
p = mismatch(s1.begin(), s1.end(),
s2.begin(),
not2(ptr_fun(ciCharCompare))));
http://www.cplusplus.com/reference/algorithm/mismatch/
template <class InputIterator1, class InputIterator2, class BinaryPredicate>
pair<InputIterator1, InputIterator2>
mismatch (InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, BinaryPredicate pred);
預計值:
二元謂詞取兩個元素作爲參數
Case II:
bool ciCharLess(char c1, char c2)
{
// return true if c1 precedes c2 in a case insensitive comparison
}
lexicographical_compare(s1.begin(), s1.end(), s2.begin(), s2.end(),
ciCharLess);
http://www.cplusplus.com/reference/algorithm/lexicographical_compare/
排版:
比較函數對象,服用相同類型的比包含在所述範圍內的兩個值中,如果第一個參數將被認爲是小於第二個參數返回true。