2011-11-24 74 views
0

我無法找到我在這裏做錯了什麼。功能eta做我所問,但當我在循環中使用它時,我得到了附加的錯誤。錯誤:'__pred'不能用作函數

bool eta(map<string, TLorentzVector> map_jets, string jet){ 
    return( fabs(map_jets[jet].PseudoRapidity()) > 2.5); 
} 

然後

vector<pair<string,double> > jets_pt(vec_jets.size()); 

    for(vector<pair<string,double> >::iterator it = jets_pt.begin(); it != jets_pt.end(); ++it) 
jets_pt.erase(remove_if(jets_pt.begin(),jets_pt.end(),eta(map_jets,it1->first)),jets_pt.end); 

我得到錯誤

/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_algo.h: In function '_OutputIterator std::remove_copy_if(_InputIterator, _InputIterator, _OutputIterator, _Predicate) [with _InputIterator = __gnu_cxx::__normal_iterator<std::pair<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, double>*, std::vector<std::pair<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, double>, std::allocator<std::pair<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, double> > > >, _OutputIterator = __gnu_cxx::__normal_iterator<std::pair<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, double>*, std::vector<std::pair<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, double>, std::allocator<std::pair<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, double> > > >, _Predicate = bool]': 
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_algo.h:1291: instantiated from '_ForwardIterator std::remove_if(_ForwardIterator, _ForwardIterator, _Predicate) [with _ForwardIterator = __gnu_cxx::__normal_iterator<std::pair<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, double>*, std::vector<std::pair<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, double>, std::allocator<std::pair<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, double> > > >, _Predicate = bool]' 
/misc/cdf/gbertoli/hww/Diboson_v20_taus/Ana/src/Functions.cc:25: instantiated from here 
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_algo.h:1216: error: '__pred' cannot be used as a function 
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_algo.h: In function '_RandomAccessIterator std::__find_if(_RandomAccessIterator, _RandomAccessIterator, _Predicate, std::random_access_iterator_tag) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<std::pair<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, double>*, std::vector<std::pair<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, double>, std::allocator<std::pair<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, double> > > >, _Predicate = bool]': 
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_algo.h:338: instantiated from '_InputIterator std::find_if(_InputIterator, _InputIterator, _Predicate) [with _InputIterator = __gnu_cxx::__normal_iterator<std::pair<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, double>*, std::vector<std::pair<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, double>, std::allocator<std::pair<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, double> > > >, _Predicate = bool]' 
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_algo.h:1287: instantiated from '_ForwardIterator std::remove_if(_ForwardIterator, _ForwardIterator, _Predicate) [with _ForwardIterator = __gnu_cxx::__normal_iterator<std::pair<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, double>*, std::vector<std::pair<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, double>, std::allocator<std::pair<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, double> > > >, _Predicate = bool]' 
/misc/cdf/gbertoli/hww/Diboson_v20_taus/Ana/src/Functions.cc:25: instantiated from here 
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_algo.h:260: error: '__pred' cannot be used as a function 
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_algo.h:264: error: '__pred' cannot be used as a function 
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_algo.h:268: error: '__pred' cannot be used as a function 
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_algo.h:272: error: '__pred' cannot be used as a function 
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_algo.h:280: error: '__pred' cannot be used as a function 
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_algo.h:284: error: '__pred' cannot be used as a function 
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_algo.h:288: error: '__pred' cannot be used as a function 
mv: cannot stat `/misc/cdf/gbertoli/hww/Diboson_v20_taus/tmp/Linux2.6-GCC_4_1/Ana/srt_dep_tmp.27294': No such file or directory 
gmake[2]: *** [/misc/cdf/gbertoli/hww/Diboson_v20_taus/tmp/Linux2.6-GCC_4_1/Ana/libAna-shared/Functions.o] Error 1 
gmake[1]: *** [src.lib] Error 2 
gmake: *** [Ana.all] Error 2 

回答

2

首先,爲remove_if唯一有效的簽名接受一個函數作爲謂詞參數:

jets_pt.erase(std::remove_if(jets_pt.begin(), jets_pt.end(), eta), 
       jets_pt.end()); 

這意味着eta必須是一個函數返回bool並採取精確一個參數,其類型爲價值類型的容器:

bool eta(const std::pair<string, double> & p) 
{ 
    // do something useful with p 
} 

如果這不符合您的賬單,因爲您需要額外的狀態信息謂詞,那麼你需要使它成爲一個函數對象

struct Eta 
{ 
    const std::map<string, TLorentzVector> & map_jets; 
    Eta(const std::map<string, TLorentzVector> & m) : map_jets(m) { } 
    bool operator()(const std::pair<string, double> & p) const 
    { 
    std::map<string, TLorentzVector>::const_iterator it = map_jets.find(p->second); 
    return it == map_jets.end() ? 
       false   : 
       std::fabs(it->second.PseudoRapidity()) > 2.5; 
    } 
}; 

現在你已經使用remove_ifEta實例:

jets_pt.erase(std::remove_if(jets_pt.begin(), jets_pt.end(), Eta(map_jets)), 
       jets_pt.end()); //       ^^^^^^^^^^^^^ 

請注意,您的for循環使用非常可疑;你應該仔細檢查一下。

+0

好的,我不能說明這一點。我已經添加了我想要實現的信息,即從對的向量中刪除元素。 – drkg4b

+0

這也是這個答案的作用。哪一點不清楚? (編輯:)最後一行本身已經做了你想要的,我想。沒有循環,只有一條線。 –

+0

感謝您的回答,我根本沒有經歷過對象。是否有任何其他解決方案來查看我的向量,以找到那些要求從那裏刪除所需條件的元素?我試過使用jets_pt.erase(它),但問題是矢量也可能是0大小,並且可能發生最後一個元素被擦除,並以錯誤結束。 – drkg4b

0

std::remove_if取包含的類型(bool pred (container::value_type),或適當地重載operator()算符)的值。

對於std::map,你必須這樣做,就像這裏解釋remove_if equivalent for std::map

0

您正在通過bool(致電eta)的結果爲remove_if爲 的第三個參數。你需要傳遞一個謂詞:一個指向 函數的指針,該函數接受一個參數並返回一些可以被轉換爲bool或者一個功能對象的東西 。在錯誤消息中,您的第三個參數是 __pred; remove_if在這裏稱它爲 pair<string, double>,並且期望返回一個bool。你必須 提供的東西可以這樣調用。 (您可能要 直視boost::bind

而且BTW:按值傳遞一個mapeta可能不是一個好主意。 如果地圖很大,它會顯着減慢速度。

+0

謝謝,我不是一個好的和有經驗的程序員,我的地圖只有7個元素長,它真的簡化了我想使用的東西的選擇。 – drkg4b