end

C++ Library  
 

Header

<map>
const_iterator end()const  

Returns a const iterator which points just beyond the last object in the container. If the container is empty, the result is undefined.

iterator end()const  

Returns a iterator which points just beyond the last object in the container. If the container is empty, the result is undefined.


Sample

#pragma warning(disable: 4786)

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

typedef std::map<int, std::string> MAP_INT_STR ;
typedef MAP_INT_STR::iterator MAP_ITERATOR ;
typedef MAP_INT_STR::reverse_iterator MAP_REVERSE_ITERATOR ;
typedef std::pair<int, std::string> PAIR_INT_STR ;

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

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

	PAIR_INT_STR pairs[5] = {	PAIR_INT_STR(1, std::string("one")),
								PAIR_INT_STR(2, std::string("two")),
								PAIR_INT_STR(3, std::string("three")),
								PAIR_INT_STR(4, std::string("four")),
								PAIR_INT_STR(5, std::string("five"))
							};

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

	//copy constructor
	MAP_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 ;
	MAP_ITERATOR Iter1 ;
	for(Iter1 = c2.begin(); Iter1 != c2.end(); Iter1++)
	{
		print_map_item(Iter1) ;
	}
	
	//rbegin, rend
	std::cout << "c2 (using rbegin, rend) = " << std::endl ;
	MAP_REVERSE_ITERATOR RevIter1 ;
	for(RevIter1 = c2.rbegin(); RevIter1 != c2.rend(); RevIter1++)
	{
		print_map_item(RevIter1) ;
	}
	
	//insert
	std::pair<MAP_ITERATOR, bool> result ;
	result = c1.insert(MAP_INT_STR::value_type(6, std::string("six"))) ;
	if(result.second == true)
	{
		std::cout << "a pair of key/data was inserted in c1, *(result.first) = " ;
		print_map_item(result.first); 
	}
	else
	{
		std::cout << "pair(6, \"six\") was not inserted in c1" << std::endl ;
	}

	c1.insert(pairs, pairs + 5) ;

	c1.insert(c1.begin(), PAIR_INT_STR(0, std::string("zero"))) ;

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

	//operator[]
	c1[8] = "eight" ;
	std::cout << "Last key/data pair in c1 = " ;
	print_map_item(c1.rbegin()) ;
	
	//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_map_item(c1.rbegin()) ;
	
	//clear
	c3.clear() ;
	std::cout << "after calling c3.clear(), number of elements in c3 = " 
		<< c3.size() << std::endl ;
	
	//get_allocator
	MAP_INT_STR::allocator_type a1 = c3.get_allocator() ;
	
	//key_comp
	MAP_INT_STR::key_compare kc = c1.key_comp() ;
	std::cout << "use function object kc to find less of (10, 4)..." 
		<< std::endl ;
	if (kc(10, 4) == true)
		std::cout << "kc(10, 4) == true, which means 10 < 4" << std::endl ;
	else
		std::cout << "kc(10, 4) == false, which means 10 > 4" << std::endl ;
	
	//value_comp
	MAP_INT_STR::value_compare vc = c1.value_comp() ;
	std::cout << "use function object vc to compare int-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(6) ;
	std::cout << "first map element with key > 6 = " ;
	print_map_item(Iter1) ;
	
	//lower_bound
	Iter1 = c2.lower_bound(6) ;
	std::cout << "first map element with key 6 = " ;
	print_map_item(Iter1) ;
	
	//equal_range
	std::pair<MAP_ITERATOR, MAP_ITERATOR> pair2 = c2.equal_range(6) ;
	std::cout << "using c2.equal_range(6),first map element with key > 6 = " ;
	print_map_item(pair2.second) ;
	std::cout << "using c2.equal_range(6), first map element with key = 6 = " ;
	print_map_item(pair2.first) ;

	//count
	std::cout << "does c2 contain an element with key 8 ?" << std::endl ;
	if(c2.count(8) == 1)
	{
		std::cout << "c2 contains element with key 8" << std::endl ;
	}
	else
	{
		std::cout << "c2 does not contain element with key 8" << std::endl ;
	}
	
	//erase
	c2.erase(c2.begin()) ;
	std::cout << "first key/data pair of c2 is: " ;
	print_map_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(8) == 1)
	{
		std::cout << "element with key 8 in c2 was erased" << std::endl ;
	}
	else
	{
		std::cout << "c2 does not contain any element with key 8" << std::endl ;
	}	
	return 0 ;
}

Program Output

c1 is empty
c2 (using begin, end) =
1, one
2, two
3, three
4, four
5, five
c2 (using rbegin, rend) =
5, five
4, four
3, three
2, two
1, one
a pair of key/data was inserted in c1, *(result.first) = 6, six
Does c1 contain any pair with key = 6?
c1 contains pair:6, six
Last key/data pair in c1 = 8, eight
max elements which c1 can hold uisng current allocator = 268435455
number of elements in c1 = 8
Last key/data pair in c1 = 5, five
after calling c3.clear(), number of elements in c3 = 0
use function object kc to find less of (10, 4)...
kc(10, 4) == false, which means 10 > 4
use function object vc to compare int-string pairs...
pairs[0] = (1, one)
pairs[1] = (2, two)
pairs[0] < pairs[1]
first map element with key > 6 = 8, eight
first map element with key 6 = 6, six
using c2.equal_range(6),first map element with key > 6 = 8, eight
using c2.equal_range(6), first map element with key = 6 = 6, six
does c2 contain an element with key 8 ?
c2 contains element with key 8
first key/data pair of c2 is: 1, one
after c1.erase(c1.begin(), c2.end()), number of elements in c1 = 0
element with key 8 in c2 was erased

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