Search
 
SCRIPT & CODE EXAMPLE
 

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.
*/
}
Comment

PREVIOUS NEXT
Code Example
Cpp :: range of long long in c++ 
Cpp :: comment in c++ 
Cpp :: c++ type of a variable 
Cpp :: or in cpp 
Cpp :: save all output in log file c cpp 
Cpp :: spicoli 
Cpp :: c++ area of triangle 
Cpp :: findung the mode in c++ 
Cpp :: fabs() c++ 
Cpp :: c++ matrix as argument 
Cpp :: C++ generate a random letter 
Cpp :: c++ rand 
Cpp :: c++ check if string is empty 
Cpp :: c++ infinite for loop 
Cpp :: qt disable resizing window 
Cpp :: c++ compare time 
Cpp :: c++ hours minutes seconds 
Cpp :: Frequency of a substring in a string C++ 
Cpp :: string vector c++ 
Cpp :: sort vector using marge sorting in c++ 
Cpp :: C++ Swap 2 Variables Without Using 3rd Variable 
Cpp :: c++ fizzbuzz 
Cpp :: find primes in a range in c++ 
Cpp :: why are inline keyword in header c++ 
Cpp :: convert string to lpwstr 
Cpp :: how to create a vector in c++ 
Cpp :: how to find 2d vector length cpp 
Cpp :: cpp vector2 
Cpp :: use uint in c++ 
Cpp :: c++ int to char* 
ADD CONTENT
Topic
Content
Source link
Name
7+7 =