2010-12-23 42 views
8

目前,我試圖生成的組合,我用下面的代碼:我需要什麼庫來使用std ::佔位符?

#include <vector> 
#include <algorithm> 
#include <iostream> 
#include <functional> 

template<class RandIt, class Compare> 
bool next_combination(RandIt first, RandIt mid, RandIt last) 
{ 
    std::sort(mid, last, std::bind(std::less<int>(), std::placeholders::_2 
              , std::placeholders::_1)); 
    return std::next_permutation(first, last, std::less<int>()); 
} 

使用G ++也無法編譯說:

next_combo.cpp: In function ‘bool next_combination(RandIt, RandIt, RandIt)’: 
next_combo.cpp:10: error: ‘bind’ is not a member of ‘std’ 
next_combo.cpp:10: error: ‘std::placeholders’ has not been declared 
next_combo.cpp:11: error: ‘std::placeholders’ has not been declared 

我認爲的std ::佔位符是在功能性聲明,但現在我很困惑。 我應該只使用boost嗎?

此外,項目的其餘部分是使用C++ 0x,那麼有沒有更好的方式來使用C++ 0x功能編寫這個?

任何幫助非常感謝:)

+3

你必須在你的g ++編譯器中使用C++ 0x標誌來獲取這些標誌。或者boost :: bind。 – Anycorn 2010-12-23 07:34:00

+0

@aaa,謝謝,看起來它已經解決了我的問題。 – shuttle87 2010-12-23 07:36:23

回答

5

爲什麼不使用lambdas而不是繁瑣的綁定?如果你使用的C++ 0x,它有意識寫

std::sort(v.begin(), v.end(), [](int l, int r)->bool { return l > r; });

代替。請注意,您不需要在那裏明確指定返回類型。我這樣寫就是爲了使它更清晰。

7

當然有更好的方法。使用std :: greater_equal而不是執行上述操作。檢查here功能還有什麼。忘了說 - 要使用佔位符,您需要啓用C++ 0x功能。

相關問題