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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T23:26:26+00:00 2026-05-24T23:26:26+00:00

class CSensor { public: CSensor(int nVal1,char* pVal2,unsigned int nVal3); CSensor(const CSensor& refMessage); const CSensor&

  • 0
 class CSensor
 {
    public:
    CSensor(int nVal1,char* pVal2,unsigned int nVal3);
    CSensor(const CSensor& refMessage);
    const CSensor& operator=(const CSensor& refMessage);
   ~CSensor(void);
   private:
   int m_nVal1;
   char* m_pVal2;   
   unsigned int m_nVal3;
 };

 // vector erase

 std::vector<CSensor> SensorList;
 CSensor obj1(1,"Test1",10);
 SensorList.push_back(obj1);
 CSensor obj2(2,"Test2",11);
 SensorList.push_back(obj2);
 CSensor obj3(3,"Test3",12);
 SensorList.push_back(obj3);

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



// map erase
    std::map<int ,CSensor> ListSensor;
CSensor obj11(1,"Test1",10);    
CSensor obj12(2,"Test2",11);
CSensor obj13(3,"Test3",12);

ListSensor.insert(std::pair<int,CSensor>(1,obj11));
ListSensor.insert(std::pair<int,CSensor>(2,obj12));
ListSensor.insert(std::pair<int,CSensor>(3,obj13));

ListSensor.erase(2);

I debugged both the case.
In both the cases i am deleting the second element.In case of vector
it is copying 3 element to 2nd poition and than it it deleting the 3 rd location.

So when u say

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

it is calling assignment operator(CSensor=) and then calling destructor.

In case of map when i do

   ListSensor.erase(2);

it only calls the destructor.

I have gone through STL vector vs map erase.
It talks Iterator,couldn’t explain the behavior.

My question is why erase behaves differently for these two STL container??

  • 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-24T23:26:27+00:00Added an answer on May 24, 2026 at 11:26 pm

    This is not a behaviour of .erase per se, but a consequence of how each respective container works.

    Deleting from a vector

    When you delete from a vector (List is a really poor name for a vector, btw), its contents must be shuffled along to fill the gap because the elements of a vector are always stored contiguously in memory.

    This is generally done by copying (or moving) elements then chopping off the remainder:

    • Vector elements in memory:

      +---+---+---+---+---+---+---+---+---+
      | a | b | c | d | e | f | g | h | i |
      +---+---+---+---+---+---+---+---+---+
      
    • Erase ‘e’:

      +---+---+---+---+---+---+---+---+---+
      | a | b | c | d |   | f | g | h | i |
      +---+---+---+---+---+---+---+---+---+
      
    • Fill the gap by copying/moving across:

                         <--
      +---+---+---+---+---+---+---+---+---+
      | a | b | c | d | f | f | g | h | i |
      +---+---+---+---+---+---+---+---+---+
      
                             <--
      +---+---+---+---+---+---+---+---+---+
      | a | b | c | d | f | g | g | h | i |
      +---+---+---+---+---+---+---+---+---+
      
                                 <--
      +---+---+---+---+---+---+---+---+---+
      | a | b | c | d | f | g | h | h | i |
      +---+---+---+---+---+---+---+---+---+
      
                                     <--
      +---+---+---+---+---+---+---+---+---+
      | a | b | c | d | f | g | h | i | i |
      +---+---+---+---+---+---+---+---+---+
      
    • Resize the container:

      +---+---+---+---+---+---+---+---+
      | a | b | c | d | f | g | h | i |
      +---+---+---+---+---+---+---+---+
      

    Deleting from a map

    This is not the case for a map, whose contents are not necessarily stored contiguously in memory (in fact, the complexity requirements mean it’s almost always a tree structure filled with pointers to discontiguous data).

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

Sidebar

Related Questions

class MyClass { public: void setVar(const char *str); private: std::string mStr; int maxLength; //we
class foo { public: void set(const int a) {b=a;} private: int b; }; Here
class Array { double *mx; int mn; public: Array(); ~Array(){delete []mx}; Array& operator-(Array& b);
class Base { public: virtual void foo() const { std::cout << Base; } };
class Equipment { std::vector<Armor*> vEquip; Weapon* mainWeapon; int totalDefense; int totalAttack; public: unsigned int
class A; class B { public: B(A& a) : a(a) {} private: A& a;
class B{ private: int a; } class D: public B{ private: int b; }
class Position { private double x,y; private int id; public String toString(Position a){ String
class foo { public void bar(int i) { ... }; public void bar(long i)
class A { public: void eat(){ cout<<A;} }; class B: virtual public A {

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.