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 213747
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T18:18:29+00:00 2026-05-11T18:18:29+00:00

I asked this question earlier. I am intrigued by std::set but I have another

  • 0

I asked this question earlier. I am intrigued by std::set but I have another confusing scenario.

Namely, is the following code legal, portable c++ for T=std::vector and T=std::set:

template <typename T>
void remove_elements(T& collection, int removal_value)
{
    typename T::iterator new_end = 
        std::remove(collection.begin(), collection.end(), removal_value);
    collection.erase(new_end, collection.end());
}

According to the SGI reference, set::iterator and set::const_iterator are the same, so I don’t know if this is legit. However, I can’t seem to find another way to get the operation I need to work regardless of type. I could resort to template specialization, however it would be nice not to have to specialize in the first place.

  • 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-11T18:18:29+00:00Added an answer on May 11, 2026 at 6:18 pm

    erase-remove idiom works only for sequence containers. For the standard associative containers it will not work and the solution is not so straightforward.

    Two approaches :

    1. Use remove_copy_if to copy all the
      values into another temporary
      container and then swap the contents
      of the original container with those
      of temporary one. [Refer my answer to related Q]
    2. The other one would be loop to walk
      the container elements and post increment the iterator when you pass it to erase.

    Refer this Q for more details: remove_if equivalent for std::map

    Also, refer Item 9. Choose carefully among erasing options from Effective STL by Scott Meyers.

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

Sidebar

Related Questions

I know I have asked this question in a different manner earlier today but
I asked this question a bit earlier and have only been able to gain
So earlier I asked this question: JQuery Login Redirect. Code Included The php file
I asked a related but very general question earlier (see especially this response ).
Earlier today I asked this question . After spending some time investigating this issue,
Note: This is an extension of an earlier question I asked here: Do additional
Note: This is an extension of an earlier question I asked here: Do additional
I apologize if this question was asked earlier and if its a simple one.
i have used a code to download file it works well in IE7,, but
There seems to be a bit of an issue with two grids containing elements

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.