<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Paul Solt &#187; STL</title>
	<atom:link href="http://paulsolt.com/tag/stl/feed/" rel="self" type="application/rss+xml" />
	<link>http://paulsolt.com</link>
	<description>Putting the Inc back in Solt since 2005.</description>
	<lastBuildDate>Wed, 12 May 2010 19:34:29 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Sorting Object Pointers in C++</title>
		<link>http://paulsolt.com/2009/01/stl-pointers-objects-and-sorting/</link>
		<comments>http://paulsolt.com/2009/01/stl-pointers-objects-and-sorting/#comments</comments>
		<pubDate>Wed, 14 Jan 2009 01:48:09 +0000</pubDate>
		<dc:creator>Paul Solt</dc:creator>
				<category><![CDATA[Animation Project]]></category>
		<category><![CDATA[animation]]></category>
		<category><![CDATA[Boost]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[OpenGL]]></category>
		<category><![CDATA[STL]]></category>

		<guid isPermaLink="false">http://paulsolt.com/?p=90</guid>
		<description><![CDATA[Today I was working on my computer animation project using C++, Boost, STL, and OpenGL. I ran into a few hitches related to pointers and the STL (Standard Template Library) and I decided to blog about it. Problem: You can&#8217;t override the default operator&#60;  for the pointer type, so you have to make a class/structure [...]]]></description>
			<content:encoded><![CDATA[<p>Today I was working on my computer animation project using C++, Boost, STL, and OpenGL. I ran into a few hitches related to pointers and the STL (Standard Template Library) and I decided to blog about it.</p>
<p><strong>Problem:</strong> You can&#8217;t override the default operator&lt;  for the pointer type, so you have to make a class/structure that defines an operator() function, which is the function pointer. Any attempt to create a operator&lt; (const Frame *lhs, const Frame *rhs) will be ignored. You might find yourself wondering why objects are being sorted by memory address (pointer address), rather than your defined method. Here&#8217;s an excerpt from my code. (Thanks to <a href="http://www.jtleigh.com/people/colin/software/CopySourceAsHtml/">CopySourceAsHTML</a>)</p>
<p>// Frame.h</p>
<div style="background: white none repeat scroll 0% 0%; font-family: Courier New; font-size: 10pt; color: black; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
<p style="margin: 0px; padding-left: 30px;"><span style="color: green;">/** A compare class used for Frame pointers.*/</span></p>
<p style="margin: 0px; padding-left: 30px;"><span style="color: blue;">class</span> FrameComparer {</p>
<p style="margin: 0px; padding-left: 30px;"><span style="color: blue;">public</span>:</p>
<p style="margin: 0px; padding-left: 60px;"><span style="color: blue;">bool</span> <span style="color: blue;">operator</span>() ( <span style="color: blue;">const</span> Frame *lhs, <span style="color: blue;">const</span> Frame *rhs) {</p>
<p style="margin: 0px; padding-left: 90px;"><span style="color: blue;">return</span> (*lhs).getTime() &lt; (*rhs).getTime();</p>
<p style="margin: 0px; padding-left: 60px;">}</p>
<p style="margin: 0px; padding-left: 30px;">};</p>
</div>
<p style="padding-left: 30px;">
<p>&#8230;</p>
<p>//FrameViewer.cpp</p>
<div style="background: white none repeat scroll 0% 0%; font-family: Courier New; font-size: 10pt; color: black;">
<p style="margin: 0px; padding-left: 30px;"><span style="color: green;">/** Adds frames to be viewed at a later time */</span></p>
<p style="margin: 0px; padding-left: 30px;"><span style="color: blue;">void</span> FrameViewer::addFrames(std::vector&lt;Frame *&gt; frames) {</p>
<p style="margin: 0px; padding-left: 60px;"><span style="color: green;">// Append the new frames to current frames</span></p>
<p style="margin: 0px; padding-left: 60px;">_frames.insert(_frames.end(), frames.begin(), frames.end());</p>
<p style="margin: 0px; padding-left: 60px;">
<p style="margin: 0px; padding-left: 60px;"><span style="color: green;">// Sort</span></p>
<p style="margin: 0px; padding-left: 60px;">std::sort(_frames.begin(), _frames.end(), FrameComparer());</p>
<p style="margin: 0px; padding-left: 30px;">}</p>
</div>
<p><strong>Summary:</strong></p>
<p>I had a STL vector full of pointer objects, but the only way I was comparing was by pointer address until I created the FrameComparer class. With the class and an compare object defined I&#8217;m able to override the sort methods comparison behavior. This way will correctly sort the Frame objects by time value, rather than pointer address.</p>
<p><strong>References:</strong></p>
<p><a title="Function Pointers" href="http://www.cprogramming.com/tutorial/function-pointers.html">Function pointers</a></p>
]]></content:encoded>
			<wfw:commentRss>http://paulsolt.com/2009/01/stl-pointers-objects-and-sorting/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
<!-- WP Super Cache is installed but broken. The path to wp-cache-phase1.php in wp-content/advanced-cache.php must be fixed! -->