<?xml version="1.0"?>
<?xml-stylesheet type="text/css" href="http://www.programmingexamples.net/w/skins/common/feed.css?303"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
		<id>http://www.programmingexamples.net/w/index.php?action=history&amp;feed=atom&amp;title=CPP%2FKeepSortedList</id>
		<title>CPP/KeepSortedList - Revision history</title>
		<link rel="self" type="application/atom+xml" href="http://www.programmingexamples.net/w/index.php?action=history&amp;feed=atom&amp;title=CPP%2FKeepSortedList"/>
		<link rel="alternate" type="text/html" href="http://www.programmingexamples.net/w/index.php?title=CPP/KeepSortedList&amp;action=history"/>
		<updated>2026-06-13T18:35:31Z</updated>
		<subtitle>Revision history for this page on the wiki</subtitle>
		<generator>MediaWiki 1.23.5</generator>

	<entry>
		<id>http://www.programmingexamples.net/w/index.php?title=CPP/KeepSortedList&amp;diff=402&amp;oldid=prev</id>
		<title>Mikael.s.persson: Keeping a Sorted List of Custom Records</title>
		<link rel="alternate" type="text/html" href="http://www.programmingexamples.net/w/index.php?title=CPP/KeepSortedList&amp;diff=402&amp;oldid=prev"/>
				<updated>2010-12-05T04:28:05Z</updated>
		
		<summary type="html">&lt;p&gt;Keeping a Sorted List of Custom Records&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;==PhoneBook.cpp==&lt;br /&gt;
In the following example, a custom record class (a phone record) is kept in a sorted list (std::set) for fast look-ups (binary search). The example shows how to create the proper overloaded operators required by the STL container and how to use the set to add records and find records based on names.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;set&amp;gt;&lt;br /&gt;
#include &amp;lt;string&amp;gt;&lt;br /&gt;
#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class PhoneRecord {&lt;br /&gt;
  private:&lt;br /&gt;
    std::string last_name;&lt;br /&gt;
    std::string first_name;&lt;br /&gt;
    std::string phone_number;&lt;br /&gt;
  public:&lt;br /&gt;
    //using default arguments allows for both a default constructor and partial construction:&lt;br /&gt;
    PhoneRecord(const std::string&amp;amp; aLastName = &amp;quot;&amp;quot;,const std::string&amp;amp; aFirstName = &amp;quot;&amp;quot;,const std::string&amp;amp; aPhoneNumber = &amp;quot;&amp;quot;) : last_name(aLastName), first_name(aFirstName), phone_number(aPhoneNumber) { };&lt;br /&gt;
    ~PhoneRecord() { };&lt;br /&gt;
    //a copy-constructor is needed for the STL container:&lt;br /&gt;
    PhoneRecord(const PhoneRecord&amp;amp; aObj) : last_name(aObj.last_name), first_name(aObj.first_name), phone_number(aObj.phone_number) { };&lt;br /&gt;
&lt;br /&gt;
    //an assignment operator is needed for the STL container:&lt;br /&gt;
    PhoneRecord&amp;amp; operator=(const PhoneRecord&amp;amp; aObj) {&lt;br /&gt;
      last_name = aObj.last_name;&lt;br /&gt;
      first_name = aObj.first_name;&lt;br /&gt;
      phone_number = aObj.phone_number;&lt;br /&gt;
      return *this;&lt;br /&gt;
    };&lt;br /&gt;
    //this is the overloaded operators you need for having an ordered set:&lt;br /&gt;
    bool operator &amp;lt;(const PhoneRecord&amp;amp; aObj) const {&lt;br /&gt;
      if(last_name &amp;lt; aObj.last_name) //sort by last name first&lt;br /&gt;
        return true;&lt;br /&gt;
      else if(last_name == aObj.last_name)&lt;br /&gt;
        return (first_name &amp;lt; aObj.first_name); //then, sort by first names&lt;br /&gt;
      else&lt;br /&gt;
        return false;&lt;br /&gt;
    };&lt;br /&gt;
    bool operator ==(const PhoneRecord&amp;amp; aObj) const {&lt;br /&gt;
      return ((last_name == aObj.last_name) &amp;amp;&amp;amp; (first_name == aObj.first_name)); //only require the names to be the same for equality.&lt;br /&gt;
    };&lt;br /&gt;
&lt;br /&gt;
    //to be able to print, you need the following friend operator:&lt;br /&gt;
    friend std::ostream&amp;amp; operator&amp;lt;&amp;lt;(std::ostream&amp;amp; out, const PhoneRecord&amp;amp; aObj);&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
//Create a printing operator for the standard output stream (ostream).&lt;br /&gt;
std::ostream&amp;amp; operator&amp;lt;&amp;lt;(std::ostream&amp;amp; out, const PhoneRecord&amp;amp; aObj) {&lt;br /&gt;
  return out &amp;lt;&amp;lt; &amp;quot;Name: &amp;quot; &amp;lt;&amp;lt; aObj.last_name &amp;lt;&amp;lt; &amp;quot;, &amp;quot; &amp;lt;&amp;lt; aObj.first_name &amp;lt;&amp;lt; &amp;quot;\t Phone Number: &amp;quot; &amp;lt;&amp;lt; aObj.phone_number;&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
int main() {&lt;br /&gt;
  std::set&amp;lt;PhoneRecord&amp;gt; phone_book;&lt;br /&gt;
  //add a bunch of records, in random order:&lt;br /&gt;
  phone_book.insert(PhoneRecord(&amp;quot;Smith&amp;quot;,&amp;quot;James&amp;quot;,&amp;quot;555-7624&amp;quot;));&lt;br /&gt;
  phone_book.insert(PhoneRecord(&amp;quot;Doe&amp;quot;,&amp;quot;John&amp;quot;,&amp;quot;555-3424&amp;quot;));&lt;br /&gt;
  phone_book.insert(PhoneRecord(&amp;quot;Doe&amp;quot;,&amp;quot;Jane&amp;quot;,&amp;quot;555-9803&amp;quot;));&lt;br /&gt;
  &lt;br /&gt;
  //print the list out:&lt;br /&gt;
  std::set&amp;lt;PhoneRecord&amp;gt;::iterator it = phone_book.begin();&lt;br /&gt;
  std::cout &amp;lt;&amp;lt; &amp;quot;The phone book is now:&amp;quot; &amp;lt;&amp;lt; std::endl;&lt;br /&gt;
  for(;it != phone_book.end();++it)&lt;br /&gt;
    std::cout &amp;lt;&amp;lt; (*it) &amp;lt;&amp;lt; std::endl;&lt;br /&gt;
&lt;br /&gt;
  //find a particular entry by name:&lt;br /&gt;
  std::cout &amp;lt;&amp;lt; &amp;quot;Now looking for John Doe...&amp;quot; &amp;lt;&amp;lt; std::endl;&lt;br /&gt;
  it = phone_book.find(PhoneRecord(&amp;quot;Doe&amp;quot;,&amp;quot;John&amp;quot;)); //notice no need for phone number.&lt;br /&gt;
  if(it != phone_book.end())&lt;br /&gt;
    std::cout &amp;lt;&amp;lt; &amp;quot;Found: &amp;quot; &amp;lt;&amp;lt; (*it) &amp;lt;&amp;lt; std::endl;&lt;br /&gt;
  else&lt;br /&gt;
    std::cout &amp;lt;&amp;lt; &amp;quot;Could not find John Doe!&amp;quot; &amp;lt;&amp;lt; std::endl;&lt;br /&gt;
&lt;br /&gt;
  return 0;&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;/div&gt;</summary>
		<author><name>Mikael.s.persson</name></author>	</entry>

	</feed>