equal_range

C++ Library  
 

Header

<map>
pair<const_iterator, const_iterator> equal_range(const Key& key) const 

Returns a pair(lower_bound(key), upper_bound(key)). The iterator returned by lower_bound nad upper_bound in this case is a const iterator.

pair<iterator, iterator> equal_range(const Key& key) const 

Returns a pair(lower_bound(key), upper_bound(key)).


Sample

#pragma warning(disable: 4786)

#include <map>
#include <iostream>
#include <string>

typedef std::multimap<char, std::string> multimap_INT_STR ;
typedef multimap_INT_STR::iterator multimap_ITERATOR ;
typedef multimap_INT_STR::reverse_iterator multimap_REVERSE_ITERATOR ;
typedef std::pair<char, std::string> PAIR_INT_STR ;

template <class ITERATOR>
void print_multimap_item(ITERATOR it)
{
	std::cout << (*it).first << ", " << 
		(*it).second << std::endl ;
}

int main()
{
	//default constructor
	multimap_INT_STR c1 ;	

	PAIR_INT_STR pairs[5] = {	PAIR_INT_STR('a', std::string("ATL")),
								PAIR_INT_STR('a', std::string("ADO")),
								PAIR_INT_STR('b', std::string("BASIC")),
								PAIR_INT_STR('c', std::string("COM")),
								PAIR_INT_STR('d', std::string("DAO"))
							};

	//construct from a range
	multimap_INT_STR c2(pairs, pairs + 5) ;

	//copy constructor
	multimap_INT_STR c3(c2) ;

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

	//begin, end
	std::cout << "c2 (using begin, end) = " << std::endl ;
	multimap_ITERATOR Iter1 ;
	for(Iter1 = c2.begin(); Iter1 != c2.end(); Iter1++)
	{
		print_multimap_item(Iter1) ;
	}
	
	//rbegin, rend
	std::cout << "c2 (using rbegin, rend) = " << std::endl ;
	multimap_REVERSE_ITERATOR RevIter1 ;
	for(RevIter1 = c2.rbegin(); RevIter1 != c2.rend(); RevIter1++)
	{
		print_multimap_item(RevIter1) ;
	}
	
	//insert
	Iter1 = c1.insert(multimap_INT_STR::value_type('i', std::string("Internet"))) ;
	if(Iter1 != c1.end())
	{
		std::cout << "a pair of key/data was inserted in c1, *Iter1 = " ;
		print_multimap_item(Iter1); 
	}
	else
	{
		std::cout << "pair('i', \"Internet\") was not inserted in c1" << std::endl ;
	}

	c1.insert(pairs, pairs + 5) ;

	c1.insert(c1.begin(), PAIR_INT_STR('j', std::string("java"))) ;

	//find
	std::cout << "Does c1 contain any pair with key = j?" << std::endl ;
	Iter1 = c1.find('j') ;
	if(Iter1 != c1.end())
	{
		std::cout << "c1 contains pair:" ;
		print_multimap_item(Iter1) ;
	}
	else
	{
		std::cout << "c1 does not contain any element with key = j" << std::endl ;
	}

	//max_size
	std::cout << "max elements which c1 can hold uisng current allocator = "
		<< c1.max_size() << std::endl ;
	
	//size
	std::cout << "number of elements in c1 = " << c1.size() << std::endl ;
	
	//swap
	c1.swap(c2) ;
	std::cout << "Last key/data pair in c1 = " ;
	print_multimap_item(c1.rbegin()) ;
	
	//clear
	c3.clear() ;
	std::cout << "after calling c3.clear(), number of elements in c3 = " 
		<< c3.size() << std::endl ;
	
	//get_allocator
	multimap_INT_STR::allocator_type a1 = c3.get_allocator() ;
	
	//key_comp
	multimap_INT_STR::key_compare kc = c1.key_comp() ;
	std::cout << "use function object kc to find less of ('a', 'b')..." 
		<< std::endl ;
	if (kc('a', 'b') == true)
		std::cout << "kc('a', 'b') == true, which means 'a' < 'b'" << std::endl ;
	else
		std::cout << "kc('a', 'b') == false, which means 'a' > 'b'" << std::endl ;
	
	//value_comp
	multimap_INT_STR::value_compare vc = c1.value_comp() ;
	std::cout << "use function object vc to compare char-string pairs..." 
		<< std::endl ;
	std::cout << "pairs[0] = (" << pairs[0].first << ", " 
		<< pairs[0].second << ")" << std::endl ;
	std::cout << "pairs[1] = (" << pairs[1].first << ", " 
		<< pairs[1].second << ")" << std::endl ;
	if ( vc(pairs[0], pairs[1]) == true)
		std::cout << "pairs[0] < pairs[1]" << std::endl ;
	else
		std::cout << "pairs[0] > pairs[1]" << std::endl ;
	
	//upper_bound
	Iter1 = c2.upper_bound('c') ;
	std::cout << "first multimap element with key > 'c' = " ;
	print_multimap_item(Iter1) ;
	
	//lower_bound
	Iter1 = c2.lower_bound('c') ;
	std::cout << "first multimap element with key 'c' = " ;
	print_multimap_item(Iter1) ;
	
	//equal_range
	std::pair<multimap_ITERATOR, multimap_ITERATOR> pair2 = c2.equal_range('c') ;
	std::cout << "using c2.equal_range('c'),first multimap element with key > 'c' = " ;
	print_multimap_item(pair2.second) ;
	std::cout << "using c2.equal_range('c'), first multimap element with key = 'c' = " ;
	print_multimap_item(pair2.first) ;

	//count
	std::cout << "number of pairs in c2 with key 'a' = " << c2.count('a') << std::endl ;
	
	//erase
	c2.erase(c2.begin()) ;
	std::cout << "first key/data pair of c2 is: " ;
	print_multimap_item(c2.begin()) ;

	c1.erase(c1.begin(), c1.end()) ;
	std::cout << "after c1.erase(c1.begin(), c2.end()), number of elements in c1 = "
		<< c1.size() << std::endl ;
	
	if(c2.erase('j') == 1)
	{
		std::cout << "element with key 'j' in c2 was erased" << std::endl ;
	}
	else
	{
		std::cout << "c2 does not contain any element with key 'j'" << std::endl ;
	}	
	return 0 ;
}

Program Output

c1 is empty
c2 (using begin, end) =
a, ATL
a, ADO
b, BASIC
c, COM
d, DAO
c2 (using rbegin, rend) =
d, DAO
c, COM
b, BASIC
a, ADO
a, ATL
a pair of key/data was inserted in c1, *Iter1 = i, Internet
Does c1 contain any pair with key = j?
c1 contains pair:j, java
max elements which c1 can hold uisng current allocator = 268435455
number of elements in c1 = 7
Last key/data pair in c1 = d, DAO
after calling c3.clear(), number of elements in c3 = 0
use function object kc to find less of ('a', 'b')...
kc('a', 'b') == true, which means 'a' < 'b'
use function object vc to compare char-string pairs...
pairs[0] = (a, ATL)
pairs[1] = (a, ADO)
pairs[0] > pairs[1]
first multimap element with key > 'c' = d, DAO
first multimap element with key 'c' = c, COM
using c2.equal_range('c'),first multimap element with key > 'c' = d, DAO
using c2.equal_range('c'), first multimap element with key = 'c' = c, COM
number of pairs in c2 with key 'a' = 2
first key/data pair of c2 is: a, ADO
after c1.erase(c1.begin(), c2.end()), number of elements in c1 = 0
element with key 'j' in c2 was erased

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