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

  • Home
  • SEARCH
  • 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 7413019
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T06:37:51+00:00 2026-05-29T06:37:51+00:00

I have two vector<T> in my program, called active and non_active respectively. This refers

  • 0

I have two vector<T> in my program, called active and non_active respectively. This refers to the objects it contains, as to whether they are in use or not.

I have some code that loops the active vector and checks for any objects that might have gone non active. I add these to a temp_list inside the loop.

Then after the loop, I take my temp_list and do non_active.insert of all elements in the temp_list.

After that, I do call erase on my active vector and pass it the temp_list to erase.

For some reason, however, the erase crashes.

This is the code:

non_active.insert(non_active.begin(), temp_list.begin(), temp_list.end());
active.erase(temp_list.begin(), temp_list.end());

I get this assertion:

Expression:("_Pvector == NULL || (((_Myvec*)_Pvector)->_Myfirst <= _Ptr && _Ptr <= ((_Myvect*)_Pvector)->_Mylast)",0)

I’ve looked online and seen that there is a erase-remove idiom, however not sure how I’d apply that to a removing a range of elements from a vector<T>

I’m not using C++11.

  • 1 1 Answer
  • 0 Views
  • 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-29T06:37:52+00:00Added an answer on May 29, 2026 at 6:37 am

    erase expects a range of iterators passed to it that lie within the current vector. You cannot pass iterators obtained from a different vector to erase.

    Here is a possible, but inefficient, C++11 solution supported by lambdas:

    active.erase(std::remove_if(active.begin(), active.end(), [](const T& x)
    {
        return std::find(temp_list.begin(), temp_list.end(), x) != temp_list.end();
    }), active.end());
    

    And here is the equivalent C++03 solution without the lambda:

    template<typename Container>
    class element_of
    {
        Container& container;
    
        element_of(Container& container) : container(container) {}
    
    public:
    
        template<typename T>
        bool operator()(const T& x) const
        {
            return std::find(container.begin(), container.end(), x)
                != container.end();
        }
    };
    
    // ...
    
    active.erase(std::remove_if(active.begin(), active.end(),
                                element_of<std::vector<T> >(temp_list)),
                 active.end());
    

    If you replace temp_list with a std::set and the std::find_if with a find member function call on the set, the performance should be acceptable.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two endpoints (xa,ya) and (xb,yb) of two vectors, respectively a and b,
Say I have two 2D vectors, one for an objects current position and one
I have two arrays of System.Data.DataRow objects which I want to compare. The rows
I am having trouble compiling a program I have written. I have two different
Let's say I have a program(C++, for example) that allocates multiple objects, never bigger
My program has a class with a vector of Level objects named levels .
I have a abstract super class called RealAlgebraicNumber and two inherited classes called IntervalRepresentation
So I have a vector of pointers holding a baseclass. I create two elements
Suppose I have two containers of elements: std::vector<std::string> foo = { aa, bb, cc
I have two vectors of integers, and for each element of the second vector

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.