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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T12:43:31+00:00 2026-06-11T12:43:31+00:00

std::vector::erase will accept either a single iterator denoting the position of the element to

  • 0

std::vector::erase will accept either a single iterator denoting the position of the element to be erased, or two iterators denoting a range. But you can’t do this:

std::vector<int> vec = { 1, 2, 3 };
vec.erase(1);

Rather you have to do something like

vec.erase( vec.begin()+1 );

This seems like needless extra work–just wondering, is there some reason from the POV of class design why this overload wouldn’t be included?

Edit: Chose the most comprehensive answer, all were good. Also as I write this this post has 4 close votes for being ‘not constructive’; however, given the quality of the answers I think this was clearly a useful question, certainly for me.

  • 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-06-11T12:43:33+00:00Added an answer on June 11, 2026 at 12:43 pm

    There are two reasons I can think of:

    1) erase is common member function for containers. Using indices only make sense for some containers, so by using iterators you do not have to worry about the type of container you are working on. For example, indices only make sense for random-access containers, by using iterators you make your code more flexible, etc.

    2) erase works well with the standard algorithms like std::remove, std::remove_if, std::unique, etc. It is much more common to want to erase elements based on some predicate, etc. rather than hard coding an index number. Look up “erase-remove idiom” for more information.

    Basically, iterators are consider to be superior to indexing for many reasons and is what you will see used extensively in the C++ standard library. Indices are merley provided for simulated array access were it makes sense.

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

Sidebar

Related Questions

std::vector::erase() does not accept reverse iterator. Is there any way to call this method
std::vector<bool> reprVectors::encode(std::vector<float> input){ std::vector<float> distance; for(size_t i=0;i<this->reprVectorsList.size();i++){ distance.push_back(distBtw(input,this->reprVectorsList[i])); } std::vector<float>::iterator it= min_element(distance.begin(),distance.end()); return this->reprVectorsList[it]->code;
std::vector<const int> vci; vci.push_back(1); vci[0] = 2; With the element type being const int
I have a std::vector<int> , and I want to delete the n th element.
Say I have two containers storing pointers to the same objects: std::list<Foo*> fooList; std::vector<Foo*>
How does stl call the destructors of objects, as in std::vector::erase or std::vector::pop_back?
I want to clear a element from a vector using the erase method. But
Consider the following code snippet: std::vector<int> v; v.reserve(100); v.insert(v.end(), 100, 5); v.erase(v.begin(), v.end()); std::cout
I am having a strange issue when using the erase() function on a std:vector.
I'm trying to delete everything from a std::vector by using the following code vector.erase(

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.