我想使用std::stable_partition
删除std::vector
中的条目。该向量是某个库 A 中模板类的成员。我正在尝试使用Checker
设置名为operator()
的结构,以充当std::stable_partition
的谓词。
我的问题似乎与How do I define a sort predicate for a templated container class in C++有关,但在我的情况下,谓词的参数取决于模板(跨两个DLL)
某些DLL A :
VectorKey2GenericPtr.h
#include <vector>
#include <set>
template <class KEY_TYPE, class GENERIC_TYPE>
class VectorKey2GenericPtr
{
protected:
std::vector< std::pair<const KEY_TYPE, GENERIC_TYPE*> > m_Vector;
std::set<KEY_TYPE> m_keysToBeRemoved;
public:
void Remove(const std::set<KEY_TYPE>& keysOfEntriesToBeRemoved);
protected:
struct Checker
{
std::set<KEY_TYPE> m_keysToBeRemoved;
explicit Checker(std::set<KEY_TYPE>& keysToBeRemoved):m_keysToBeRemoved(keysToBeRemoved) {}
//bool operator() (const std::pair<const KEY_TYPE, GENERIC_TYPE>& pairToBeTested) <-- before fix suggested by @MassimilianoJanes
bool operator() (const std::pair<const KEY_TYPE, GENERIC_TYPE*>& pairToBeTested)
{
const KEY_TYPE& keyToBeTested = pairToBeTested.first;
typename std::set<KEY_TYPE>::iterator it = m_keysToBeRemoved.find(keyToBeTested);
bool found = false;
if (it != m_keysToBeRemoved.end())
{
found = true;
m_keysToBeRemoved.erase(it);
}
return found;
}
};
};
VectorKey2GenericPtr.cpp
#include "VectorKey2GenericPtr.h"
#include <functional>
template <class KEY_TYPE, class GENERIC_TYPE>
void VectorKey2GenericPtr<KEY_TYPE, GENERIC_TYPE>::
Remove(const std::set<KEY_TYPE>& keysOfEntriesToBeRemoved)
{
m_keysToBeRemoved = keysOfEntriesToBeRemoved;
typename std::vector< std::pair<const KEY_TYPE, GENERIC_TYPE*> >::iterator bound;
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
// !!! I would like to use Checker like this: !!!
bound = std::stable_partition( m_Vector.begin(), m_Vector.end(), Checker(m_keysToBeRemoved) );
//
// TODO: Remove one partition from the map
//
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
}
class SomeInterfaceFromDLLB;
template class VectorKey2GenericPtr<int, SomeInterfaceFromDLLB>;
template class VectorKey2GenericPtr<int, SomeInterfaceFromDLLB>::Checker;
某些DLL B :
SomeInterfaceFromDLLB.h
class SomeInterfaceFromDLLB{};
EDIT1
在@Massimiliano Janes评论我的原始编译错误后:
C2079
std::pair<const KEY_TYPE, GENERIC_TYPE>::second
使用未定义的类`SomeInterfaceFromDLLB'
已修改:
Checker:'bool operator()(const std :: pair&amp; pairToBeTested)`
到
Checker:bool operator()(const std :: pair&amp; pairToBeTested)
现在我仍然收到以下错误:
C2166 l-value指定const对象
这与行:
有关bound = std::stable_partition( m_Vector.begin(), m_Vector.end(), Checker(m_keysToBeRemoved) );
utility.h:
_Myt& operator=(_Myt&& _Right)
_NOEXCEPT_OP((is_nothrow_move_assignable<_Ty1>::value && is_nothrow_move_assignable<_Ty2>::value))
{ // assign from moved pair
first = _STD forward<_Ty1>(_Right.first);
second = _STD forward<_Ty2>(_Right.second);
return (*this);
}
有什么建议吗?