erase

C++ Library  
 

Header

<set>
iterator erase(iterator it) 

Removes from the set container, the element postioned at it.

iterator erase(iterator first, iterator last)

Removes from the set container, the elements in the range [first, last).

Both of the above return an iterator that designates the first element remaining beyond any elements removed, or end() if no such element exists.

size_type erase(const Key& key)

Removes the set elements which match key, and returns the number of elements removed. If none of the elements match key, the function returns 0.


Sample

#pragma warning(disable: 4786)

#include <set>
#include <iostream>


int main()
{
	//default constructor
	std::set<int> c1 ;

	int ai[] = {0, 1, 2, 3} ;
	
	//construct from a range
	std::set<int> c2(ai, ai + 4) ;

	//copy constructor
	std::set<int> c3(c2) ;

	std::set<int>::iterator Iter ;
	std::set<int>::reverse_iterator RevIter ;

	//empty
	if(c1.empty())
	{
		std::cout << "set c1 is empty" << std::endl ;
	}
	else
	{
		std::cout << "set c1 is not empty" << std::endl ;
	}

	//begin, end
	std::cout << "c2 (using begin, end)  = " ;
	for(Iter = c2.begin(); Iter != c2.end(); Iter++)
	{
		std::cout << *Iter << " " ;
	}
	std::cout << std::endl ;

	//rbegin, rend
	std::cout << "c2 (using rbegin, rend) = " ;
	for(RevIter = c2.rbegin(); RevIter != c2.rend(); RevIter++)
	{
		std::cout << *RevIter << " " ;
	}
	std::cout << std::endl ;

	//insert
	c1.insert(ai, ai+4) ;
	
	std::pair<std::set<int>::iterator, bool> pr ;

	pr = c1.insert(0) ;
	if(pr.second == true)
	{
		std::cout << "element 0 was inserted in c1 successfully" << std::endl ;
	}
	else
	{
		std::cout << "element 0 already exists in c1 and *(pr.first) = " 
			<< *(pr.first) << std::endl ;
	}

	//find
	std::set<int>::const_iterator constIter = c1.find(3) ;
	if(constIter != c1.end())
	{
		std::cout << "c1 contains element 3, *constIter = " 
			<< *constIter << std::endl ;
	}

	//max_size
	std::cout << "c1.max_size() = " << c1.max_size() << std::endl ;

	//size
	std::cout << "c1.size() = " << c1.size() << std::endl ;

	//swap
	c1.insert(4) ;
	c2.swap(c1) ;
	std::cout << "The last element of c2 = " << *(c2.rbegin())
		<< std::endl ;

	//clear
	c1.clear() ;
	std::cout << "After calling c1.clear(), c1.size() = " 
		<< c1.size() << std::endl ;

	//get_allocator
	std::set<int>::allocator_type a1 = c1.get_allocator() ;

	//key_compare
	std::set<int>::key_compare kc = c2.key_comp() ;
	bool result = kc(2, 3) ;
	if(result == true)
	{
		std::cout << "kc is function object used by c2.  kc(2,3) = true" << std::endl ;
	}
	else
	{
		std::cout << "kc is function object used by c2.  kc(2,3) = false" << std::endl ;
	}

	//value_comp
	std::set<int>::value_compare vc = c2.value_comp() ;
	result = vc(10, 4) ;
	if(result == true)
	{
		std::cout << "vc is function object used by c2.  vc(10,4) = true" << std::endl ;
	}
	else
	{
		std::cout << "vc is function object used by c2.  vc(10,4) = false" << std::endl ;
	}

	//upper_bound
	std::cout << "* (c2.upper_bound(3)) = " 
		<< *(c2.upper_bound(3)) << std::endl ;

	//lower_bound
	std::cout << "* (c2.lower_bound(3)) = " 
		<< *(c2.lower_bound(3)) << std::endl ;

	//equal_range
	std::pair<std::set<int>::const_iterator, std::set<int>::const_iterator> pr1 = c2.equal_range(3) ;
	std::cout << "*(pr1.first) = " << *(pr1.first) << "\t" 
		<< "*(pr1.second) = " << *(pr1.second) << std::endl ;

	//erase
	if(c3.erase(1) != 0)
	{
		std::cout << "c3 does not contain 1 any more" << std::endl ;
	}
	else
	{
		std::cout << "No elements in c3 match key 1" << std::endl ;
	}

	if((c2.erase(c2.begin())) != c2.end())
	{
		std::cout << "c2 does not contain 0 any more" << std::endl ;
	}
	else
	{
		std::cout << "No elements in c2 match key 0" << std::endl ;
	}

	c3.erase(c3.begin(), c3.end()) ;
	std::cout << "after c3.erase(c3.begin(), c3.end()), c3.size() = " 
		<< c3.size() << std::endl ;
	

	return 0 ;
}

Program Output

set c1 is empty
c2 (using begin, end)  = 0 1 2 3
c2 (using rbegin, rend) = 3 2 1 0
element 0 already exists in c1 and *(pr.first) = 0
c1 contains element 3, *constIter = 3
c1.max_size() = 1073741823
c1.size() = 4
The last element of c2 = 4
After calling c1.clear(), c1.size() = 0
kc is function object used by c2.  kc(2,3) = true
vc is function object used by c2.  vc(10,4) = false
* (c2.upper_bound(3)) = 4
* (c2.lower_bound(3)) = 3
*(pr1.first) = 3        *(pr1.second) = 4
c3 does not contain 1 any more
c2 does not contain 0 any more
after c3.erase(c3.begin(), c3.end()), c3.size() = 0

© 1997 Microsoft Corporation. All rights reserved. Terms of Use.