提供STL演算法交換函數
要在STL演算法中使用自訂交換方法,有幾種方法:
會員交換:
免費-常備互換功能:
std::swap 的明確特化:
建議方法:
建議方法是在同一命名空間中使用獨立的交換函數作為班級。這允許在 STL 演算法中呼叫交換時使用 ADL(參數相關查找)。
範例:
namespace Foo { class Bar{}; // dummy void swap(Bar& lhs, Bar& rhs) { // ... } }
在 STL 演算法中使用上述交換函數:
template<class T> void foo(T& lhs, T& rhs) { using std::swap; // enable 'std::swap' to be found // if no other 'swap' is found through ADL // some code ... swap(lhs, rhs); // unqualified call, uses ADL and finds a fitting 'swap' // or falls back on 'std::swap' // more code ... }
以上是如何提供與 STL 演算法一起使用的自訂交換函數?的詳細內容。更多資訊請關注PHP中文網其他相關文章!