Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 952431
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T23:53:37+00:00 2026-05-15T23:53:37+00:00

I have a typedef, a class with a member vector using that type and

  • 0

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.

  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T23:53:37+00:00Added an answer on May 15, 2026 at 11:53 pm

    I’m surprised begin and end are even compiling, they shouldn’t. std::vector (and friends) use iterators, not pointers. (Though they are intended to act similarly.)

    In any case, erase takes an iterator, not a pointer. Because vectors are contiguous, you can make utility functions as such, though:

    template <typename T, typename A>
    typename std::vector<T, A>::iterator
    to_iterator(T* pPtr, std::vector<T, A>& pVec)
    {
        ASSERT(pPtr >= &pVec.front() && pPtr <= &pVec.back());
    
        return pVec.begin() + (pPtr- &pVec[0]);
    }
    
    template <typename T, typename A>
    typename std::vector<T, A>::const_iterator
    to_iterator(const T* pPtr, const std::vector<T, A>& pVec)
    {
        ASSERT(pPtr >= &pVec.front() && pPtr <= &pVec.back());
    
        return pVec.begin() + (pPtr - &pVec[0]);
    }
    

    Basically, find out how many elements pPtr is from &pVec[0] (the first element), then add that to pVec.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:

    void truncate (WordNo_t *Ptr)
    {
        // note the == end() bit will be in here anyway:
        m_Words.erase(to_iterator(Ptr, m_Words), end());
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have class with member functions: typedef std::function<bool (const std::string &)> InsertFunction; bool insertSourceFile(
I have a class with a private data member of type vector< A*> .
I have a typedef inside a class from a c++ machine library (dlib) that
If I have a class that contains a number of typedef 'd variables, like
I have two (unrelated) classes. The first one is Point: typedef std::complex<double> complex_number; class
In my current setup, I have a typedef std::function<void (MyClass&, std::vector<std::string>) MyFunction; std::map<std::string, MyFunction>
I have a class as outlined below: class InputReader { public: typedef void (*handler)(std::string,
I have a question about performance of move constructor, pseudo C++ class: typedef tuple<std::string,
i have a class that has this function: typedef boost::shared_ptr<PrimShapeBase> sp_PrimShapeBase; class Control{ public:
If I have a class like this: typedef union { __m128 quad; float numbers[4];

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.