#include <algorithm>
#include <vector>
template <class BidirectionalIterator, class UnaryPredicate>
BidirectionalIterator partition(BidirectionalIterator first,
BidirectionalIterator last, UnaryPredicate pred)
{
while (first != last) {
while (pred(*first)) {
++first;
if (first == last) return first;
}
do {
--last;
if (first == last) return first;
} while (!pred(*last));
std::swap(*first, *last);
++first;
}
return first;
}
int main() {
std::vector<int> v = { 1, 55, 17, 65, 40, 18, 77, 37, 77, 37 };
partition(v.begin(), v.end(), [](const int &i) {
return i < 40;
});
return 0;
}
代碼不會編譯。 clang ++(3.5.2/cygwin)和Visual Studio(2013)都抱怨模糊的調用。由於沒有使用using
指令,我不明白什麼是錯的。 要成功編譯,使用::
前綴有幫助。鐺++:錯誤:調用'分區'含糊