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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T15:36:27+00:00 2026-06-15T15:36:27+00:00

Eventually if I am trying to delete all the elements of a vector associated

  • 0

Eventually if I am trying to delete all the elements of a vector associated with a key, I am encountering a segmentation fault. My intended output is new b new c new d new a, but i am getting new b new c new d segmentation fault.

    #include <iostream>
#include <vector>
#include <map>
#include <algorithm>

using namespace std;

int main ()
{
  map<char,vector<char> > mmap; //multimap 
  char mychar[] = { 'b','c', 'd'};
  vector<char> vec (mychar,mychar+3);
  vector<char> newvec; 

  mmap.insert (pair<char,vector<char> >('a',vec)); //insert to multimap
  mmap.insert (pair<char,vector<char> >('b',vector<char>()));
  mmap.insert (pair<char,vector<char> >('c',vector<char>()));
  mmap.insert (pair<char,vector<char> >('d',vector<char>()));

  vector<char>::iterator veciter; 
  map<char,vector<char> >::iterator mapiter;

  for(int i=0;i<6;i++)
  {  
  for ( mapiter = mmap.begin(); mapiter != mmap.end(); ++mapiter) 
  {
    //if elements associated with vector of a key are empty the store the key in a new vector
    if(mapiter->second.empty()) 
    {
      newvec.push_back (mapiter->first);
      mmap.erase(mapiter);
    }
    else
    {
       for (veciter = mapiter->second.begin(); veciter != mapiter->second.end(); ++veciter)
       {
         //if an element of a vector of key is found in new vector, erase the element
         if (find(newvec.begin(), newvec.end(), *veciter)!=newvec.end())   
         {
            mapiter->second.erase(veciter);
         }

       }
    }
    // to display values of new vector     
    for (unsigned i=0; i<newvec.size(); ++i)
    cout << "new " << newvec[i]<<' ';
    cout << '\n'; 
  }  
  }

  return 0;
}
  • 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-15T15:36:28+00:00Added an answer on June 15, 2026 at 3:36 pm

    When you pass an iterator to a container’s erase function, that iterator becomes invalidated. You need to account for that. Assuming, for some reason, that neither std::remove nor std::remove_if will work for your situation, the standard idiom goes like this:

    for (it = container.begin(); it != container.end(); /* no increment here */)
    {
        if (should_be_removed(*it))
        {
            // possibly other operations involving the element we are about to remove
            it = container.erase(it);
        }
        else
        {
            // possibly other operations involving the element we chose not to remove
            ++it;
        }
    }
    

    When we erase an element, we capture the return value of the erase operation, which is the next iterator. Otherwise, we increment. Note the space where I left room for other possible operations. If there are no other operations, you should be able to just use std::remove or std::remove_if, combined with the container’s range erase function (the one that takes two iterators).

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

Sidebar

Related Questions

I'm trying to eventually make a sliding form with jQuery, but while testing out
I'm trying to delete all tables from a database except one, and I end
Eventually I'm trying to create a timed slide show that fades images in and
I'm trying to write a little helper for Windows which eventually will accept a
I am trying to generate a RTF document by hand and eventually will do
I'm trying to run my Silverlight tests via a command line (eventually MSBuild) working
I'm trying to fix an existing C-program with VS2005 that eventually calls int system(command)
I'm trying to put together SharePoint 2007 (and, eventually, 2010) training materials for experienced
I have Python nested list that I'm trying to organize and eventually count number
So I am toying with some black magic in Perl (eventually we all do

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.