2013-02-22 124 views
1

在下面的第一個代碼片段中,我試圖從一個成員函數中的一個向量中移除一個元素,該元素基於一個靜態條件函數饋入std :: remove函數。然後,我收到了第二個片段中顯示的大量模板錯誤。你能告訴我我錯過了什麼嗎?從std :: vector中刪除一個項目

SNIPPET 1(CODE)

void removeVipAddress(std::string &uuid) 
{ 
      struct RemoveCond 
      { 
      static bool condition(const VipAddressEntity & o) 
      { 
       return o.getUUID() == uuid; 
      } 
      }; 

      std::vector<VipAddressEntity>::iterator last = 
      std::remove(
        mVipAddressList.begin(), 
        mVipAddressList.end(), 
        RemoveCond::condition); 

      mVipAddressList.erase(last, mVipAddressList.end()); 

} 

內容片段2(編譯輸出)

/usr/include/c++/4.7/bits/random.h:4845:5: note: template<class _IntType> bool  std::operator==(const std::discrete_distribution<_IntType>&, const std::discrete_distribution<_IntType>&) 
/usr/include/c++/4.7/bits/random.h:4845:5: note: template argument deduction/substitution failed: 
In file included from /usr/include/c++/4.7/algorithm:63:0, 
      from Entity.hpp:12: 
/usr/include/c++/4.7/bits/stl_algo.h:174:4: note: ‘ECLBCP::VipAddressEntity’ is not derived from ‘const std::discrete_distribution<_IntType>’ 
In file included from /usr/include/c++/4.7/random:50:0, 
       from /usr/include/c++/4.7/bits/stl_algo.h:67, 
       from /usr/include/c++/4.7/algorithm:63, 
       from Entity.hpp:12: 
/usr/include/c++/4.7/bits/random.h:4613:5: note: template<class _RealType> bool std::operator==(const std::extreme_value_distribution<_RealType>&, const std::extreme_value_distribution<_RealType>&) 
/usr/include/c++/4.7/bits/random.h:4613:5: note: template argument deduction/substitution failed: 
In file included from /usr/include/c++/4.7/algorithm:63:0, 
      from Entity.hpp:12: 
/usr/include/c++/4.7/bits/stl_algo.h:174:4: note: ‘ECLBCP::VipAddressEntity’ is not derived from ‘const std::extreme_value_distribution<_RealType>’ 
+0

這只是一些註釋部分的錯誤信息,真正的錯誤是缺少的。 – PlasmaHH 2013-02-22 13:15:43

+0

據我所知,你不能在這裏使用RemoveCond :: condition作爲模板參數。看看http://stackoverflow.com/a/7627218/767543其中「然而這是不允許的,f不能傳遞給C++ 03中的模板函數。」陳述。 雖然我可能錯了 – 2013-02-22 13:17:52

+0

我得到了很多錯誤信息,無法放入終端的緩衝區中...... – 2013-02-22 13:18:57

回答

4

我猜你正在尋找std::remove_if(),不std::remove()

std::remove_if()接受謂詞作爲第三個參數,並刪除滿足該謂詞的元素。

std::remove()將值作爲第三個參數,並刪除與該值相等的元素。

編輯

爲了使這項工作,你還必須把你的RemoveCond定義成一個謂語的對象,因爲它需要的狀態。像這樣:

void removeVipAddress(std::string &uuid) 
{ 
     struct RemoveCond : public std::unary_function<VipAddressEntity, bool> 
     { 
     std::string uuid; 

     RemoveCond(const std::string &uuid) : uuid(uuid) {} 

     bool operator() (const VipAddressEntity & o) 
     { 
      return o.getUUID() == uuid; 
     } 
     }; 

     std::vector<VipAddressEntity>::iterator last = 
     std::remove(
       mVipAddressList.begin(), 
       mVipAddressList.end(), 
       RemoveCond(uuid)); 

     mVipAddressList.erase(last, mVipAddressList.end()); 

} 
+0

是的,刪除不需要謂詞。 – 2013-02-22 13:16:56

+0

雅,這有助於感謝,但後來我得到了以下錯誤。你對這些有何評論? Entity.hpp:在靜態成員函數'static bool ECLBCP :: VipAddressSet :: removeVipAddress(std :: string&):: RemoveCond :: condition(const ECLBCP :: VipAddressEntity&)'中: Entity.hpp:203:32:錯誤:使用包含函數的參數 Entity.hpp:197:7:錯誤:'std :: string&uuid'在這裏聲明 – 2013-02-22 13:17:01

+0

@Fardaarda我已將它添加到答案中。 – Angew 2013-02-22 13:29:32