lower_bound

C++ Library  
 

Header

<set>
const_iterator upper_bound(const Key& key) const

Returns a const iterator pointing to the first element in the multiset that is not less than key. If all elements in the multiset are less than key, the function returns end().


Sample

#pragma warning(disable: 4786)

#include <set>
#include <iostream>


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

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

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

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

	//empty
	if(c1.empty())
	{
		std::cout << "multiset c1 is empty" << std::endl ;
	}
	else
	{
		std::cout << "multiset 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) ;

	c1.insert(1) ;

	//count
	std::cout << "Number elements in c1 that match 1 = " << 
		c1.count(1) << std::endl ;	

	//find
	std::multiset<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::multiset<int>::allocator_type a1 = c1.get_allocator() ;

	//key_compare
	std::multiset<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::multiset<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::multiset<int>::const_iterator, std::multiset<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

multiset c1 is empty
c2 (using begin, end)  = 0 0 1 1 2 2 3 4
c2 (using rbegin, rend) = 4 3 2 2 1 1 0 0
Number elements in c1 that match 1 = 3
c1.max_size() = 1073741823
c1.size() = 5
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)) = 4
*(pr1.first) = 4        *(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.