我試圖用C++使用模板通用的排序功能,但我卡在寫greater
功能,如果lhs > rhs
C++模板和比較不同類型
template <typename T>
bool Sorter<T>::greater(T lhs, T rhs)
{
return lhs > rhs;
}
上面的代碼會照顧返回true簡單類型如int,long。我該怎麼做才能使代碼適用於std::string
,std::string&
,const char *
。代碼示例將非常有幫助。
首先,返回lhs> rhs'。 – avakar
所有處理'>'-operator。你如何管理'std :: string','std :: string&',const char *'進行字典對比? – triclosan
快速提示:您可以簡單地使用「return(lhs> rhs);」。 – Gaminic