I have a typedef, a class with a member vector using that type and then a method using std::<vector>::erase().
#typedef DWORD WordNo_t;
class CWordList : public CObject
{
public:
WordNo_t* begin() { return m_Words.begin(); }
WordNo_t* end() { return m_Words.end(); }
void truncate (WordNo_t *Ptr)
{
if (Ptr == end())
return;
ASSERT (Ptr >= begin() && Ptr < end());
// following line generates C2664
m_Words.erase (Ptr, end());
}
private:
std:vector<WordNo_t> m_Words;
}
The detailed error is:
error C2664: ‘std::_Vector_iterator<_Myvec> std::vector<_Ty>::erase(std::_Vector_const_iterator<_Myvec>,std::_Vector_const_iterator<_Myvec>)’ : cannot convert parameter 1 from ‘const WordNo_t’ to ‘std::_Vector_const_iterator<_Myvec>’
Pretty new to STL… Any help would be appreciated.
I’m surprised
beginandendare even compiling, they shouldn’t.std::vector(and friends) use iterators, not pointers. (Though they are intended to act similarly.)In any case,
erasetakes an iterator, not a pointer. Because vectors are contiguous, you can make utility functions as such, though:Basically, find out how many elements
pPtris from&pVec[0](the first element), then add that topVec.begin(). (Transform the offset from a pointer and the pointer to start into the offset from the start.) This operation is O(1). And then: