Answers for "how to erase element from multiset c++"

C++
2

how to erase element from multiset c++

#include <iostream>
#include <set>

int main()
{
	std::multiset<int>ms;
  	ms.insert(1);
  	ms.insert(1);
  	ms.insert(2);
  	ms.insert(2);
  	ms.insert(3);
  	ms.insert(3);
  	for(auto& it : ms) {
		std::cout<<it<<' ';
    }
  	std::cout<<'\n';
  	ms.erase(2); // erases all elements with value 2
  	for(auto& it : ms) {
		std::cout<<it<<' ';
    }
  	std::cout<<'\n';
  	ms.erase(ms.begin()); // erases first element
  	for(auto& it : ms) {
		std::cout<<it<<' ';
    }
  	std::cout<<'\n';
  	ms.erase(ms.find(3)); // erases only one element with value 3
  	for(auto& it : ms) {
		std::cout<<it<<' ';
    }
	return 0;
}
Posted by: Guest on May-23-2021

Browse Popular Code Answers by Language