Using Visual C++ 5.0 Standard C++ Library with User-Defined Types

C++ Library  
 
 

Some general rules to follow when using STL containers to hold user-defined data types.

NOTE: The header file <utility> defines global versions of ‘<=’, ‘>’, ‘>=’, and ‘!=’ operators, which are all defined in terms of ‘<’ and ‘==’. Therefore, if only ‘<’ and ‘==’ operators are defined, the rest are defined "for free" (if any of the rest are explicitly defined for an object, the explicit definition will override the global definition).

In general, it is assumed that objects to be used with STL containers have at least the following defined.

Example: Storing a User-Defined Type in an STL Container

The following sample stores the user-defined type PhoneBook, in a map container.

The PhoneBook contains telephone numbers and addresses.

The sample creates a map of strings and Phonebook. The string contains a name, and the PhoneBook object contains the telephone number and address.

Compile the sample using the following.

		cl /GX /ML usingudt.cpp

//usingudt.cpp
#pragma warning(disable: 4786)

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

struct PhoneBook
{
	//constructors
	PhoneBook(long number, std::string address) ;
	PhoneBook(const PhoneBook& rPB) ;

	//assignment operator
	PhoneBook& operator=(const PhoneBook& rPB) ;

	//destructor
	~PhoneBook() ;
	
	//comparison operators
	bool operator<(const PhoneBook& rPB) ;
	bool operator==(const PhoneBook& rPB) ;

	//friend function
	friend std::ostream& operator<<(std::ostream& os, const PhoneBook& rPB) ;

	//data
	long m_Number ;
	std::string m_Address ;
} ;


PhoneBook::PhoneBook(long number, std::string address)
{
	m_Number = number ;
	m_Address = address ;
}

PhoneBook::PhoneBook(const PhoneBook& rPB)
{
	m_Number = rPB.m_Number ;
	m_Address = rPB.m_Address ;
}


PhoneBook::~PhoneBook()
{

}

PhoneBook& PhoneBook::operator=(const PhoneBook& rPB)
{
	m_Number = rPB.m_Number ;
	m_Address = rPB.m_Address ;
	return *this ;
}

bool PhoneBook::operator<(const PhoneBook& rPB)
{
	return (m_Number < rPB.m_Number) ;
}

bool PhoneBook::operator==(const PhoneBook& rPB)
{
	return ( (m_Address == rPB.m_Address) && (m_Number == rPB.m_Number) ) ;
}

std::ostream& operator<<(std::ostream& os, const PhoneBook& rPB) 
{
	os << rPB.m_Number << ", " ;
	os << rPB.m_Address << ", " ;
	return os ;
}


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


int main()
{
	//map of string and PhoneBook
	typedef std::map<std::string, PhoneBook> MyMAP ;

	//iterator for accessing elements of the map
	typedef MyMAP::iterator MyMAPIT ;

	//maps stores the key and data using the pair data type
	typedef std::pair<std::string, PhoneBook> MyPAIR ;

	//Create PhoneBook objects
	PhoneBook p1(8820589, "Shadow Brooks Apartment") ;
	PhoneBook p2(7390083, "The Park At Forbes Creek Drive") ;
	PhoneBook p3(7029241, "The Gallery Apartments") ;

		
	//create pairs to be inserted into the map
	MyPAIR pairs[3] = {	
	
		MyPAIR(std::string("Donald Duck"), p1),
		MyPAIR(std::string("Minnie Mouse"), p2),
		MyPAIR(std::string("Bugs Bunny"), p3) 
		};
	
	
	MyMAP phonemap ;

	phonemap.insert(pairs, pairs + 3) ;
	
	
	MyMAPIT Iter1 ;	
	
	//print contents of the map	
	for(Iter1 = phonemap.begin(); Iter1 != phonemap.end(); Iter1++)	{
		print_map_item(Iter1) ;	}
	
	return 0 ;
}

Program Output

Bugs Bunny, 7029241, The Gallery Apartments,
Donald Duck, 8820589, Shadow Brooks Apartment,
Minnie Mouse, 7390083, The Park At Forbes Creek Drive,

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