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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T06:51:44+00:00 2026-06-10T06:51:44+00:00

I have a vector of int vector<int> p; now I want to delete one

  • 0

I have a vector of int

vector<int> p;

now I want to delete one of its items that is equal to 3.
there is no remove like p.remove(3)

but there is a erase but at first I should I find it.
there were two questions about this available in stackoverflow. both of them said that we should find it by

std::remove(p.begin(), p.end(), 3) 

but this code isn’t compiled. it said function does not take 3 arguments.

  • 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-10T06:51:45+00:00Added an answer on June 10, 2026 at 6:51 am

    Use Erase Remove Idiom .

    #include <iostream>
    #include <iterator>
    #include <algorithm>
    #include <vector> 
    
    int main()
    {
        using namespace std;
        vector<int> v = {1, 2, 3, 3, 4};
    
        v.erase(find(begin(v), end(v), 3));  // remove first 3
        // v.erase(remove(begin(v), end(v), 3), end(v));  // remove all 3
    
        copy(begin(v), end(v), ostream_iterator<int>(cout, " "));
    }
    

    Vectors erase method takes one or two iterators. The first one, erases everthing from the specified position to the end.
    std::remove from <algorithm> moves all matching elements to the end of the sequence and returns an iterator to the position of the new end. You can use this iterator for erase.
    If you want to remove the first matching element, use std::find to retrieve an iterator to the first element and pass that to erase.

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

Sidebar

Related Questions

I have a std::vector<int> , and I want to delete the n th element.
lets say I have a vector of pair<int,int> . Now I want to extract
I have a vector of pointers like so: vector<Item*> items; I want to clear
I have vector< pair<int, int>> myVec (N); I want to have all pairs initialized
Suppose I have a vector<int> myvec and I want to loop through all of
I have a function that creates a 2D vector void generate(int n) { vector<
I have some numbers stored in a std::vector<int> . I want to find which
Say I have a vector like this: vector< pair<string, pair<int, int> > > cont;
everyone! I have a 2D vector filled with unsigned chars. Now I want to
I have a vector containing large number of elements. Now I want to write

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.