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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T00:16:39+00:00 2026-05-24T00:16:39+00:00

While inserting in a set, does the set internally delete some of the objects

  • 0

While inserting in a set, does the set internally delete some of the objects multiple times? I tried to insert two objects of type MyClass as in following program but to my surprise it calls the destructor of class with initially inserted value 2 times! I am not able to understand any logic behind this. Can anyone give some idea on the output?
(highlighted as bold)

#include<stdio.h>
#include<stdlib.h>
#include<set>

 using namespace std;

   struct MyClass 
   {
      double num;

      ~MyClass()
      {
         printf("Destructor called..for val: %lf\n", num);
      }
   };

   typedef int (*fun_comp)(MyClass, MyClass);   

  int
   comp(MyClass a, MyClass b)
   {
      return a.num-b.num;
   }

  int
   main()
   {
      fun_comp fptr;
      fptr = &comp;
      set<MyClass, int (*)(MyClass, MyClass)> b(fptr);

      for(int i=3; i< 5; i++)
      {
         printf("started with i: %d....\n\n", i);
         {
            MyClass m;
            m.num=i*1.134;
            b.insert(m);
            printf("Inserted val: %lf\n", m.num);
         }
         printf("ended....\n\n");
      }

      printf("Done with insert..\n");      
      return 0;
   }

output:
started with i: 3….

Inserted val: 3.402000

Destructor called..for val: 3.402000

ended….

started with i: 4….

Destructor called..for val: 4.536000 <——- why this is deallocated before insertion

Destructor called..for val: 3.402000 <——- multiple call to destructor for this valued object

Destructor called..for val: 4.536000 <——– ??

Destructor called..for val: 3.402000 <—— again!!

Inserted val: 4.536000

Destructor called..for val: 4.536000

ended….

Done with insert..

Destructor called..for val: 3.402000

Destructor called..for val: 4.536000

  • 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-24T00:16:40+00:00Added an answer on May 24, 2026 at 12:16 am

    The comparator

    int    
    comp(MyClass a, MyClass b)
    {
       return a.num-b.num;
    }  
    

    takes its parameters by value. This will create extra copies that are then destroyed.

    Passing by reference will work better.

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

Sidebar

Related Questions

The data I am trying to insert is from a database, and while inserting
First i gave all values of row in JTAble. While inserting table values into
While inspecting delegates in C# and .NET in general, I noticed some interesting facts:
While the C# spec does include a pre-processor and basic directives (#define, #if, etc),
Had to re-set up everything on my laptop recently, which included reinstalling XAMPP. While
I have two set of arrays in different MYSQL table. This what I want
While creating and inserting DOM element's, it seems that the functions used for the
Possible Duplicate: android calendar delete event Actually i write a code for inserting and
I have 2 objects as follows class A{ Set<B> bs ; } class B{
I just answered a question where I said that while string interning is good

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.