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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T20:33:21+00:00 2026-05-28T20:33:21+00:00

Coming from Java and using the ArrayList class has me all frustrated when trying

  • 0

Coming from Java and using the ArrayList class has me all frustrated when trying to learn the C++ equivalent (Vectors).

I’m writing a function that removes a certain int given from a vector of ints. For awhile I could not figure out why it was causing a segmentation fault. After looking at the documentation I realized .erase also removes any element after the one being erased. Which is definitely not what I want to do, so I’m a little lost of how I would go about removing only a single element from a vector without removing the elements after.

Function that I currently have which causes a segmentation fault:

void remove(int n){

    for(int a=0; a<list.size(); a++){

        if(list.at(a)==n){
            list.erase (list.begin()+(n-1));
            cout << n << " has been erased" << endl;
            break;
        }
    }
}
  • 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-28T20:33:22+00:00Added an answer on May 28, 2026 at 8:33 pm

    You’re looking for n as an element, but are also using it as an index. I don’t think this is what you want.

    If you replace (n-1) by a, it should work:

            list.erase(list.begin()+a);
    

    Alternative way to remove a single element, using find and erase:

    #include <algorithm>
    //...
    void removeOne(int n){
        vector<int>::iterator found = std::find(list.begin(), list.end(), n) ;
        if (found!=list.end())
            list.erase(found);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using JMS Messaging in my java program. My messages are coming from
I've recently (4 days) started to learn C++ coming from C / Java background.
Coming from an academic background in mutli-agent systems (developed in Java using JADE )
Coming from mootools and JAVA, mootools class implementation is a real nice way to
I'm working on my first project using Python 2.7. We're coming from a Java
I am coming from Java and am currently working on a C# project. What
I'm coming from Java/C++ to Ada and am having trouble figuring out the small
I'm coming from Java, where I'd submit Runnable s to an ExecutorService backed by
I'm starting with Python coming from java. I was wondering if there exists something
I just started learning C++ (coming from Java ) and am having some serious

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.