2013-02-22 2 views
1

아래의 첫 번째 코드 단편에서는 std :: remove 함수로 제공되는 정적 조건 함수를 기반으로 멤버 함수 내에서 벡터에서 요소를 제거하려고합니다. 그런 다음 제 2 스 니펫에 표시된 템플릿 오류가 많이 발생합니다. 제가 누락 된 부분을 말씀해 주시겠습니까?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()); 

} 

SNIPPET 2 (컴파일 OUTPUT)

/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

+0

내가 아는 한 RemoveCond :: condition을 템플릿 arguement로 사용할 수 없습니다. http://stackoverflow.com/a/7627218/767543에서 "이것은 허용되지 않지만 f는 C++ 03의 템플릿 함수에 전달 될 수 없습니다." 라고 명시되어 있습니다. 비록 내가 틀릴 수도있다. –

+0

나는 터미널의 버퍼에 맞지 않는 많은 에러 메시지를 가지고있다. –

답변

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

그래, 제거는 어떤 술어도 취하지 않는다. –

+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'가 여기에 선언되었습니다. –

+0

@Fardaarda 답변에 추가했습니다. – Angew

관련 문제