Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR CPP

how to delete a certain amount of numbers of the same value in multiset c++

/*
So we have a multiset called 'ms' which contains several values of 10, and we 
want to delete at most 3 of them, we keep this value in the 'delete_this_many' 
integer.
*/
int value = 10;
int delete_this_many = 3;
//  first of all, we delete all of them and keep the amount of
// those deleted values in an integer called 'amt' with this command;
int amt = ms.erase(value);
// if this amount is more or equal to what we want to delete, (3 for this example), 
//then we "revive" amt - 3 of them, so the total deleted value is 3.
if(amt >= delete_this_many){
  int revive_this_many = amt - 3;
  while(revive_this_many > 0){
    ms.insert(value);
    revive_this_many--;
  }
/* you can also write this while loop like this : 

	while(revive_this_many--){
    	ms.insert(value);
	}
    it is basically the same, but it is just more smooth to write like this, and
    also faster if you are in the competition.
    'revive_this_many' is just getting decreased by 1 on each iteration.
*/
}
 
PREVIOUS NEXT
Tagged: #delete #amount #numbers #multiset
ADD COMMENT
Topic
Name
4+7 =