<?xml version="1.0"?>
<rss version="2.0">
	<channel>
		<title>std::vector, Where Have You Been All My Life?</title>
		<link>http://www.allegro.cc/forums/view/590253</link>
		<description>Allegro.cc Forum Thread</description>
		<webMaster>matthew@allegro.cc (Matthew Leverton)</webMaster>
		<lastBuildDate>Thu, 01 Mar 2007 22:20:18 +0000</lastBuildDate>
	</channel>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I&#39;ve known of and have been using the std::vector class for about a dozen weeks now, primarily while making my graphics application, and I&#39;m starting to wonder how I ever programmed without it!</p><p>For starters, making the file handler for my graphics program has been absolute <i>Hell</i>... specifically because of all the nuances in what the thing should do when you click on certain things, or type in certain file names, or switch your desired file format, etc. etc. But the process of actually creating the list of files from the current working directory was extremely simple, thanks to _stricmp() for sorting and the findfirst() and findnext() functions to get the filenames, but most importantly, the std::vector object that holds the list of filenames and file attributes without having to do messy memory allocation or whatnot.</p><p>I&#39;ve also got std::vector objects holding the brush history, undo history, and bitmap pages.</p><p>I&#39;ve been slowly losing my desire to work with static arrays, which, 12 weeks ago, I was convinced would <i>never</i> happen. As a result, I&#39;ve been going through the docs for my game engine and the first game I&#39;m going to make with it and altering many of their static-allocated features to be dynamic. (Some of the static-ness is still there, but only where it makes sense to have small, predefined limits.)</p><p>The new and delete commands make perfect sense to me now as well, and I haven&#39;t touched malloc() or calloc() since PixelShips Retro, though it took over half an hour for me to fix a bug in my graphics program that involved nothing more than changing &quot;BITMAP *bitmap&quot; in a function definition to a &quot;BITMAP *&amp;bitmap&quot;...</p><p>...so I guess stupid mistakes are still going to happen... <img src="http://www.allegro.cc/forums/smileys/rolleyes.gif" alt="::)" /></p><p>Anyways, I&#39;m fully confident in using dynamic arrays and std::vector objects now, and it&#39;s hard to imagine that I used to program without them not long ago...</p><p>--- Kris Asick (Gemini)<br />--- <a href="http://www.pixelships.com">http://www.pixelships.com</a>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Kris Asick)</author>
		<pubDate>Sun, 25 Feb 2007 13:05:50 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Amen brother.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Billybob)</author>
		<pubDate>Sun, 25 Feb 2007 13:43:12 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Don&#39;t stop at std::vector. Take a look at STL. STL offers a number of simple algorithms. std::copy, transform, find_max, etc. all make life much simpler. </p><p>And then when you&#39;re really serious take a look at boost. Boost is doing such a good job that most of the C++0x library changes (ie: the next c++ standard) is coming from the boost libraries. In fact, its the same way that the STL (including std::vector) managed to work its way into C++ everywhere.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Goalie Ca)</author>
		<pubDate>Sun, 25 Feb 2007 13:49:56 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>You bring back memories of when I first discovered the vector.  90% of all the programming I uses that in one way or another. <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Mark Oates)</author>
		<pubDate>Sun, 25 Feb 2007 14:15:12 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p><tt>std::map</tt> is also quite handy in certain cases.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Kibiz0r)</author>
		<pubDate>Mon, 26 Feb 2007 02:31:17 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I think I use <tt>std::list</tt> the most.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (CGamesPlay)</author>
		<pubDate>Mon, 26 Feb 2007 02:31:59 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Oh, and don&#39;t forget std::string. It isn&#39;t the best, by far, but with a handy to_string template function life is a breeze. <img src="http://www.allegro.cc/forums/smileys/cool.gif" alt="8-)" /></p><div class="source-code snippet"><div class="inner"><pre><span class="k1">template</span><span class="k3">&lt;</span><span class="k1">typename</span> T&gt;
std::string to_string<span class="k2">(</span><span class="k1">const</span> T <span class="k3">&amp;</span>value<span class="k2">)</span>
<span class="k2">{</span>
  std::ostringstream ret<span class="k2">;</span>
  ret <span class="k3">&lt;</span><span class="k3">&lt;</span> value<span class="k2">;</span>
  <span class="k1">return</span> ret.str<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
<span class="k2">}</span>
</pre></div></div><p>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Billybob)</author>
		<pubDate>Mon, 26 Feb 2007 04:43:05 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>std::vector is pretty much the only container I ever use. I remember using std::map for a couple of times to store some settings but that&#39;s it. It is not that I don&#39;t need know else, it&#39;s just that all my programs work perfectly fine with vectors <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" /></p><p>Of cource there are a lot more of those interesting headers. Algorithm and numeric come to my mind first. I suggest to at least read the <a href="http://www.sgi.com/tech/stl/table_of_contents.html">TOC</a> of SGI STL documentation to get some idea what functionality does STL have.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (HoHo)</author>
		<pubDate>Mon, 26 Feb 2007 05:05:21 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I very often use STD vector, though occasionally use an array for some things that are small, delicate, and have a defined max size.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Kikaru)</author>
		<pubDate>Mon, 26 Feb 2007 05:54:13 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
Don&#39;t stop at std::vector. Take a look at STL. STL offers a number of simple algorithms. std::copy, transform, find_max, etc. all make life much simpler.
</p></div></div><p>
Bookmark <a href="http://cppreference.com/cppalgorithm/index.html">this link</a>.</p><p>Simple example:
</p><div class="source-code snippet"><div class="inner"><pre><span class="c">// Before</span>
<span class="k1">for</span><span class="k2">(</span>vector<span class="k3">&lt;</span>string&gt;::iterator itr <span class="k3">=</span> strings.begin<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span> itr <span class="k3">!</span><span class="k3">=</span> strings.end<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span> <span class="k3">+</span><span class="k3">+</span>itr<span class="k2">)</span>
  itr-&gt;foobar<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
</pre></div></div><p>
</p><div class="source-code snippet"><div class="inner"><pre><span class="c">// After</span>
for_each<span class="k2">(</span>strings.begin<span class="k2">(</span><span class="k2">)</span>, strings.end<span class="k2">(</span><span class="k2">)</span>, mem_fun_ref<span class="k2">(</span><span class="k3">&amp;</span>string::foobar<span class="k2">)</span><span class="k2">)</span><span class="k2">;</span>
</pre></div></div><p>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (ImLeftFooted)</author>
		<pubDate>Mon, 26 Feb 2007 08:26:11 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="source-code snippet"><div class="inner"><pre><span class="c">// After after</span>
BOOST_FOREACH<span class="k2">(</span>string item, strings<span class="k2">)</span>
     item.foobar<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
</pre></div></div><p>

<img src="http://www.allegro.cc/forums/smileys/cool.gif" alt="8-)" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Billybob)</author>
		<pubDate>Mon, 26 Feb 2007 09:09:58 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>well billy bob, seeing as how you use boost and you gave a stringstream example you should probably like this:</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">void</span> log_errno<span class="k2">(</span><span class="k1">int</span> yoko<span class="k2">)</span>
<span class="k2">{</span>
    log_message<span class="k2">(</span><span class="s">"Error "</span> <span class="k3">+</span> boost::lexical_cast<span class="k3">&lt;</span>std::string&gt;<span class="k2">(</span>yoko<span class="k2">)</span> <span class="k3">+</span> <span class="s">": "</span> <span class="k3">+</span> <a href="http://www.delorie.com/djgpp/doc/libc/libc_759.html" target="_blank">strerror</a><span class="k2">(</span>yoko<span class="k2">)</span><span class="k2">)</span><span class="k2">;</span>
<span class="k2">}</span>
</pre></div></div><p>

If you read the hpp file, you&#39;ll notice it uses a stringstream (sstream or the boost implementation based on #defines). </p><p>Also, the idea of using for_each and transform has got me thinking about functional programming. That&#39;s where i discovered boost lambda (which will be in C++0x).</p><div class="source-code snippet"><div class="inner"><pre>for_each<span class="k2">(</span>a.begin<span class="k2">(</span><span class="k2">)</span>, a.end<span class="k2">(</span><span class="k2">)</span>, 
         if_<span class="k2">(</span>_1 % <span class="n">2</span> <span class="k3">=</span><span class="k3">=</span> <span class="n">0</span><span class="k2">)</span><span class="k2">[</span> cout <span class="k3">&lt;</span><span class="k3">&lt;</span> _1 <span class="k2">]</span><span class="k2">)</span>
</pre></div></div><p>

Being C++ it isn&#39;t half as elegant as python list comprehensions but its pretty good for a start.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Goalie Ca)</author>
		<pubDate>Mon, 26 Feb 2007 09:55:06 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
well billy bob, seeing as how you use boost and you gave a stringstream example you should probably like this:
</p></div></div><p>
Niiice. I don&#39;t actually use boost, just the BOOST_FOREACH header. But that&#39;s a neat trick anyway. <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Billybob)</author>
		<pubDate>Mon, 26 Feb 2007 10:52:39 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Kris Asick said:</div><div class="quote"><p>

Anyways, I&#39;m fully confident in using dynamic arrays and std::vector objects now, and it&#39;s hard to imagine that I used to program without them not long ago...
</p></div></div><p>

The path of illumination has opened before you. At the end of the corridor lies a door which says &quot;LISP&quot;, which, when you reach it, you can claim reaching the ultimate state of enlightenment as a programmer.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (axilmar)</author>
		<pubDate>Mon, 26 Feb 2007 16:26:27 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="source-code snippet"><div class="inner"><pre><span class="c">// After after</span>
BOOST_FOREACH<span class="k2">(</span>string item, strings<span class="k2">)</span>
     item.foobar<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
</pre></div></div><p>
Now make your code go over some arbitrary memory, or how about even a subset of the strings list...</p><p>Right.  And thats why I use the STL.  Boost is mostly crap anyway.  This sort of method: boost::lexical_cast should not be using stringstream anyway, thats tons of unnecessary overhead.  There are more efficient ways to do that.</p><p>Boost is generally lame anyway.  Its key &quot;popular&quot; features are just thin wrappers around C++ features.</p><p>And come on, macros?  I thought we were done with those.</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
</p><div class="source-code snippet"><div class="inner"><pre>for_each<span class="k2">(</span>a.begin<span class="k2">(</span><span class="k2">)</span>, a.end<span class="k2">(</span><span class="k2">)</span>, 
         if_<span class="k2">(</span>_1 % <span class="n">2</span> <span class="k3">=</span><span class="k3">=</span> <span class="n">0</span><span class="k2">)</span><span class="k2">[</span> cout <span class="k3">&lt;</span><span class="k3">&lt;</span> _1 <span class="k2">]</span><span class="k2">)</span>
</pre></div></div><p>
</p></div></div><p>
</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">template</span><span class="k3">&lt;</span><span class="k1">int</span> power, <span class="k1">typename</span> Parm&gt;
<span class="k1">bool</span> powerOf<span class="k2">(</span>Parm p<span class="k2">)</span> <span class="k2">{</span> <span class="k1">return</span> p % power <span class="k3">=</span><span class="k3">=</span> <span class="n">0</span><span class="k2">;</span> <span class="k2">}</span>
</pre></div></div><p>
^^ put that in a header somewhere, very reusable.<br />Now to do ugly lambada crap in a clean way:
</p><div class="source-code snippet"><div class="inner"><pre>copy_if<span class="k2">(</span>a.begin<span class="k2">(</span><span class="k2">)</span>, a.end<span class="k2">(</span><span class="k2">)</span>, ostream_iterator<span class="k3">&lt;</span>int&gt;<span class="k2">(</span>cout<span class="k2">)</span>, PowerOf<span class="k3">&lt;</span><span class="n">2</span><span class="k3">&gt;</span><span class="k2">)</span><span class="k2">;</span>
</pre></div></div><p>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (ImLeftFooted)</author>
		<pubDate>Tue, 27 Feb 2007 01:34:35 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>woa i have so much to learn. Is this used in the work place? example a database programing position.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (piccolo)</author>
		<pubDate>Tue, 27 Feb 2007 01:53:08 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Most of whats is being shown here are just neat tricks that help you stand out from being a good programmer to being a better programmer.</p><p>Now regarding the work place it really depends. For a database programming job, chances are you wont need to know anything here. Very rarely is database code done in C++, and if it is, it&#39;s going to be abstracted away some how.</p><p>.Net, Java etc are languages that you&#39;ll find most database jobs use and they wont need any of these clever hacks. Mostly because stuff like this is built into the language, or added in someway.(For each loops are the best example).</p><p>Now if you&#39;re developing a C++ program in your job, then it could be. However you want to avoid being too fancy with this kind of stuff since it might work nice, but some other people might not understand the code etc. But in the end its not necessary and clean and easy to understand and efficient is more important then using neat tricks to simplify things. In fact depending on the requirements of the project, you might not even be able to do some stuff you would like.</p><p>Now if your asking if you need to the STL to code C++ apps, and you better be familiar with STL and not just learning it for the first time when you walk in.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Hard Rock)</author>
		<pubDate>Tue, 27 Feb 2007 04:19:46 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
std::vector, Where Have You Been All My Life?
</p></div></div><p>
I just said this today, only it was &quot;PHP&quot; instead of &quot;std::vector&quot;.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Onewing)</author>
		<pubDate>Tue, 27 Feb 2007 06:18:21 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>You really want to use different STL containers for different purposes. Look into deque, set, map and list as well, not only vector, and get familiar with them, so you can pick the right thing for the task at hand.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (tobing)</author>
		<pubDate>Tue, 27 Feb 2007 14:24:23 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
And come on, macros? I thought we were done with those.
</p></div></div><p>

Agreed. The way its implemented is ugly. Lambda functions are really nice though. Your solution is nice but the problem is that you still have to declare something outside the function. </p><p>The thing that really irks me about c++ is that you cannot define a function inside of a function. SO you have to make a private member somewhere...</p><p>A lot of what i do is program math for applied problems like dt-mri imaging. C++ is sort of the general purpose does all language for me but i&#39;d still love to see more features of a functional language. There are many things that functional languages simply cannot do easily or cleanly enough.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Goalie Ca)</author>
		<pubDate>Wed, 28 Feb 2007 12:28:13 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
Your solution is nice but the problem is that you still have to declare something outside the function.
</p></div></div><p>
Yeah, but at least its reusable.
</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">if</span><span class="k2">(</span>powerOf<span class="k3">&lt;</span><span class="n">5</span><span class="k3">&gt;</span><span class="k2">(</span><span class="n">25</span><span class="k2">)</span><span class="k2">)</span>
  cout <span class="k3">&lt;</span><span class="k3">&lt;</span> <span class="s">"25 is a power of 5!\n"</span><span class="k2">;</span>
</pre></div></div><p>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (ImLeftFooted)</author>
		<pubDate>Wed, 28 Feb 2007 14:00:43 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
I just said this today, only it was &quot;PHP&quot; instead of &quot;std::vector&quot;.
</p></div></div><p>
OMG, I had the exact same thing just a week ago.<br />And again today, while diving into the PHP image functions. Woooo-hoo! It&#39;s almost like a full allegro built into the language! Well, the graphics part at least...</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
Agreed. The way its implemented is ugly. Lambda functions are really nice though. Your solution is nice but the problem is that you still have to declare something outside the function.
</p></div></div><p>
Yeah. I&#39;d love something like this:
</p><div class="source-code snippet"><div class="inner"><pre>vector<span class="k3">&lt;</span>string&gt; mystrlst<span class="k2">;</span>

foreach<span class="k2">(</span>mystrlst, lambda<span class="k2">(</span>string<span class="k3">&amp;</span> s<span class="k2">)</span> <span class="k2">{</span> cout <span class="k3">&lt;</span><span class="k3">&lt;</span> s<span class="k2">;</span> <span class="k2">}</span> <span class="k2">)</span><span class="k2">;</span>
</pre></div></div><p>
Generally, lambda functions could be soooo useful; for example, to pass as a callback...<br />Or php-like:
</p><div class="source-code snippet"><div class="inner"><pre>vector<span class="k3">&lt;</span>string&gt; mystrlst<span class="k2">;</span>

foreach<span class="k2">(</span>mystrlst as s<span class="k2">)</span> <span class="k2">{</span>
  cout <span class="k3">&lt;</span><span class="k3">&lt;</span> s<span class="k2">;</span>
<span class="k2">}</span>
</pre></div></div><p>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Tobias Dammers)</author>
		<pubDate>Wed, 28 Feb 2007 16:51:08 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Tobais, that already exists:</p><div class="source-code snippet"><div class="inner"><pre>vector<span class="k3">&lt;</span>string&gt; mystrlist<span class="k2">;</span>
std::copy<span class="k2">(</span>mystrlist.begin<span class="k2">(</span><span class="k2">)</span>, mystrlist.end<span class="k2">(</span><span class="k2">)</span>, ostream_iterator<span class="k3">&lt;</span>string&gt;<span class="k2">(</span>cout, <span class="s">"item seperator"</span><span class="k2">)</span><span class="k2">)</span><span class="k2">;</span>
</pre></div></div><p>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Carrus85)</author>
		<pubDate>Wed, 28 Feb 2007 16:58:07 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>The cout is just an example; my point is that where the cout statement sits, there could be arbitrary code; anything you like.<br />Something like the lambda thing is possible already, with the minor drawback that you cannot define the function on-the-fly, it must be a function or member function somewhere.<br />And templates make up for a lot, often allowing for more elegant solutions.</p><p>But I do sometimes miss a more convenient iterating mechanism in C++.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Tobias Dammers)</author>
		<pubDate>Wed, 28 Feb 2007 17:22:01 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>WOW, this article got me looking into vectors, I had heard about them before but never understood how to use them, now I&#39;m in the same boat as you and don&#39;t know how I got by without them!</p><p>I WUB WOOOOUU!!<br />sorry, read vgcats on vgcats.com to understand.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Taiko Keiji)</author>
		<pubDate>Wed, 28 Feb 2007 22:39:18 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I remember learning about vectors in my beginning c++ programming class. Long time ago,but they are awsome. Been developing in PHP for five years and I must admit, that was awsome too. I love them both. :-D
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Charles256)</author>
		<pubDate>Wed, 28 Feb 2007 23:05:26 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
std::copy(mystrlist.begin(), mystrlist.end(), ostream_iterator&lt;string&gt;(cout, &quot;item seperator&quot;));
</p></div></div><p>

Oh my God. I think I&#39;d prefer to write it out as a for loop. You know, so that my eyes don&#39;t bleed :-).
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Jakub Wasilewski)</author>
		<pubDate>Wed, 28 Feb 2007 23:11:13 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
You know, so that my eyes don&#39;t bleed :-).
</p></div></div><p>
Yes but its fukcing blazing fast.  That statement will be faster then writing out the for loop yourself!</p><p>For the kind of efficiency, thats the cleanest code I&#39;ve ever seen.</p><p>C++ was meant to be fast and elegant, not noobish.  Don&#39;t mistake the two.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (ImLeftFooted)</author>
		<pubDate>Thu, 01 Mar 2007 03:15:52 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
That statement will be faster then writing out the for loop yourself!
</p></div></div><p>Err... prove it.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (CGamesPlay)</author>
		<pubDate>Thu, 01 Mar 2007 03:18:23 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Oh, and lets not forget the opposite operation:</p><div class="source-code snippet"><div class="inner"><pre>vector<span class="k3">&lt;</span>string&gt; tokens<span class="k2">;</span>
copy<span class="k2">(</span>istream_iterator<span class="k3">&lt;</span>string&gt;<span class="k2">(</span>cin<span class="k2">)</span>, istream_iterator<span class="k3">&lt;</span>string&gt;<span class="k2">(</span><span class="k2">)</span>, back_inserter<span class="k2">(</span>tokens<span class="k2">)</span><span class="k2">)</span><span class="k2">;</span>
</pre></div></div><p> <img src="http://www.allegro.cc/forums/smileys/grin.gif" alt=";D" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Carrus85)</author>
		<pubDate>Thu, 01 Mar 2007 03:20:08 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">CGamesPlay said:</div><div class="quote"><p>

Err... prove it.
</p></div></div><p>
A compiler can unroll the loop, as well as a plethora of other tricks.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (ImLeftFooted)</author>
		<pubDate>Thu, 01 Mar 2007 03:20:54 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
A compiler can unroll the loop.
</p></div></div><p>

Really? The compiler has no prior knowledge of the number of elements in mystrlist, so it can&#39;t unroll it fully. It can partially unroll it (i.e., execute it in blocks of 8 iterations or whatever using the Duff&#39;s device for example), but that can be just as easily done to any for loop.</p><p>I&#39;m not saying it won&#39;t be faster - maybe it will be. But it&#39;s still butt ugly, and I&#39;m not convinced it actually does provide a benefit. Anyway, the point is moot in that case, because the majority of the time will be spent on output to cout.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Jakub Wasilewski)</author>
		<pubDate>Thu, 01 Mar 2007 03:33:16 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
Err... prove it.
</p></div></div><div class="source-code"><div class="toolbar"></div><div class="inner"><table width="100%"><tbody><tr><td class="number">1</td><td><span class="p">#include &lt;iostream&gt;</span></td></tr><tr><td class="number">2</td><td><span class="p">#include &lt;vector&gt;</span></td></tr><tr><td class="number">3</td><td><span class="p">#include &lt;algorithm&gt;</span></td></tr><tr><td class="number">4</td><td><span class="p">#include &lt;ctime&gt;</span></td></tr><tr><td class="number">5</td><td>&#160;</td></tr><tr><td class="number">6</td><td><span class="k1">using</span> <span class="k1">namespace</span> std<span class="k2">;</span></td></tr><tr><td class="number">7</td><td>&#160;</td></tr><tr><td class="number">8</td><td><span class="k1">const</span> <span class="k1">int</span> SIZE<span class="k3">=</span><span class="n">10000000</span><span class="k2">;</span></td></tr><tr><td class="number">9</td><td><span class="k1">const</span> <span class="k1">int</span> LOOPS<span class="k3">=</span><span class="n">100</span><span class="k2">;</span></td></tr><tr><td class="number">10</td><td><span class="k1">int</span> main<span class="k2">(</span><span class="k2">)</span><span class="k2">{</span></td></tr><tr><td class="number">11</td><td>  vector<span class="k3">&lt;</span>int&gt; intvec1<span class="k2">(</span>SIZE<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">12</td><td>  vector<span class="k3">&lt;</span>int&gt; intvec2<span class="k2">(</span>SIZE<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">13</td><td>  <span class="c">//warmup</span></td></tr><tr><td class="number">14</td><td>  copy<span class="k2">(</span>intvec1.begin<span class="k2">(</span><span class="k2">)</span>, intvec1.end<span class="k2">(</span><span class="k2">)</span>, intvec2.begin<span class="k2">(</span><span class="k2">)</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">15</td><td>  cout<span class="k3">&lt;</span><span class="k3">&lt;</span><span class="s">"Total time to copy "</span><span class="k3">&lt;</span><span class="k3">&lt;</span>SIZE<span class="k3">&lt;</span><span class="k3">&lt;</span><span class="s">" ints from one vector to other "</span><span class="k3">&lt;</span><span class="k3">&lt;</span>LOOPS<span class="k3">&lt;</span><span class="k3">&lt;</span><span class="s">" times:"</span><span class="k3">&lt;</span><span class="k3">&lt;</span>endl<span class="k2">;</span></td></tr><tr><td class="number">16</td><td>  clock_t before<span class="k3">=</span><a href="http://www.delorie.com/djgpp/doc/libc/libc_103.html" target="_blank">clock</a><span class="k2">(</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">17</td><td>  <span class="k1">for</span><span class="k2">(</span><span class="k1">int</span> i<span class="k3">=</span><span class="n">0</span><span class="k2">;</span>i<span class="k3">&lt;</span>LOOPS<span class="k2">;</span>i<span class="k3">+</span><span class="k3">+</span><span class="k2">)</span><span class="k2">{</span></td></tr><tr><td class="number">18</td><td>    copy<span class="k2">(</span>intvec1.begin<span class="k2">(</span><span class="k2">)</span>, intvec1.end<span class="k2">(</span><span class="k2">)</span>, intvec2.begin<span class="k2">(</span><span class="k2">)</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">19</td><td>  <span class="k2">}</span></td></tr><tr><td class="number">20</td><td>  clock_t after<span class="k3">=</span><a href="http://www.delorie.com/djgpp/doc/libc/libc_103.html" target="_blank">clock</a><span class="k2">(</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">21</td><td>  cout<span class="k3">&lt;</span><span class="k3">&lt;</span><span class="s">"std::copy:    "</span><span class="k3">&lt;</span><span class="k3">&lt;</span><span class="k2">(</span>after-before<span class="k2">)</span><span class="k3">&lt;</span><span class="k3">&lt;</span>endl<span class="k2">;</span></td></tr><tr><td class="number">22</td><td>&#160;</td></tr><tr><td class="number">23</td><td>  before<span class="k3">=</span><a href="http://www.delorie.com/djgpp/doc/libc/libc_103.html" target="_blank">clock</a><span class="k2">(</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">24</td><td>  <span class="k1">for</span><span class="k2">(</span><span class="k1">int</span> i<span class="k3">=</span><span class="n">0</span><span class="k2">;</span>i<span class="k3">&lt;</span>LOOPS<span class="k2">;</span>i<span class="k3">+</span><span class="k3">+</span><span class="k2">)</span><span class="k2">{</span></td></tr><tr><td class="number">25</td><td>    <span class="k1">for</span><span class="k2">(</span><span class="k1">int</span> j<span class="k3">=</span><span class="n">0</span><span class="k2">;</span>j<span class="k3">&lt;</span>SIZE<span class="k2">;</span>j<span class="k3">+</span><span class="k3">+</span><span class="k2">)</span><span class="k2">{</span></td></tr><tr><td class="number">26</td><td>      intvec2<span class="k2">[</span>j<span class="k2">]</span><span class="k3">=</span>intvec1<span class="k2">[</span>j<span class="k2">]</span><span class="k2">;</span></td></tr><tr><td class="number">27</td><td>    <span class="k2">}</span></td></tr><tr><td class="number">28</td><td>  <span class="k2">}</span></td></tr><tr><td class="number">29</td><td>  after<span class="k3">=</span><a href="http://www.delorie.com/djgpp/doc/libc/libc_103.html" target="_blank">clock</a><span class="k2">(</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">30</td><td>  cout<span class="k3">&lt;</span><span class="k3">&lt;</span><span class="s">"regular loop: "</span><span class="k3">&lt;</span><span class="k3">&lt;</span><span class="k2">(</span>after-before<span class="k2">)</span><span class="k3">&lt;</span><span class="k3">&lt;</span>endl<span class="k2">;</span></td></tr><tr><td class="number">31</td><td>  <span class="k1">return</span> <span class="n">0</span><span class="k2">;</span></td></tr><tr><td class="number">32</td><td>  </td></tr><tr><td class="number">33</td><td><span class="k2">}</span></td></tr></tbody></table></div></div><p>One sample output wit g++ -O2:<span class="source-code">Total <a href="http://www.delorie.com/djgpp/doc/libc/libc_821.html" target="_blank">time</a> to copy <span class="n">10000000</span> ints from one vector to other <span class="n">100</span> times:</span><br />std::copy:    2620000<br />regular loop: 2870000<br />&lt;/code&gt;Without optimizations:</p><div class="source-code snippet"><div class="inner"><pre>Total <a href="http://www.delorie.com/djgpp/doc/libc/libc_821.html" target="_blank">time</a> to copy <span class="n">10000000</span> ints from one vector to other <span class="n">100</span> times:
std::copy:    <span class="n">2830000</span>
regular loop: <span class="n">30390000</span>
</pre></div></div><p>Satisfied?<br /><img src="http://www.allegro.cc/forums/smileys/tongue.gif" alt=":P" /></p><p>Using cout would have been pointless as its overhead would have been way too high for measuring the actual looping process</p><p>[edit]<br />With vectors that fit into cache:<br />No optimizations:</p><div class="source-code snippet"><div class="inner"><pre>Total <a href="http://www.delorie.com/djgpp/doc/libc/libc_821.html" target="_blank">time</a> to copy <span class="n">100000</span> ints from one vector to other <span class="n">10000</span> times:
std::copy:    <span class="n">320000</span>
regular loop: <span class="n">29170000</span>
</pre></div></div><p>-O2</p><div class="source-code snippet"><div class="inner"><pre>Total <a href="http://www.delorie.com/djgpp/doc/libc/libc_821.html" target="_blank">time</a> to copy <span class="n">100000</span> ints from one vector to other <span class="n">10000</span> times:
std::copy:    <span class="n">350000</span>
regular loop: <span class="n">490000</span>
</pre></div></div><p>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (HoHo)</author>
		<pubDate>Thu, 01 Mar 2007 03:42:03 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Use iterators <img src="http://www.allegro.cc/forums/smileys/tongue.gif" alt=":P" /></p><div class="source-code"><div class="toolbar"></div><div class="inner"><table width="100%"><tbody><tr><td class="number">1</td><td><span class="p">#include &lt;iostream&gt;</span></td></tr><tr><td class="number">2</td><td><span class="p">#include &lt;vector&gt;</span></td></tr><tr><td class="number">3</td><td><span class="p">#include &lt;algorithm&gt;</span></td></tr><tr><td class="number">4</td><td><span class="p">#include &lt;ctime&gt;</span></td></tr><tr><td class="number">5</td><td>&#160;</td></tr><tr><td class="number">6</td><td><span class="k1">using</span> <span class="k1">namespace</span> std<span class="k2">;</span></td></tr><tr><td class="number">7</td><td>&#160;</td></tr><tr><td class="number">8</td><td><span class="k1">const</span> <span class="k1">int</span> SIZE<span class="k3">=</span><span class="n">100000</span><span class="k2">;</span></td></tr><tr><td class="number">9</td><td><span class="k1">const</span> <span class="k1">int</span> LOOPS<span class="k3">=</span><span class="n">10000</span><span class="k2">;</span></td></tr><tr><td class="number">10</td><td><span class="k1">int</span> main<span class="k2">(</span><span class="k2">)</span><span class="k2">{</span></td></tr><tr><td class="number">11</td><td>  vector<span class="k3">&lt;</span>int&gt; intvec1<span class="k2">(</span>SIZE<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">12</td><td>  vector<span class="k3">&lt;</span>int&gt; intvec2<span class="k2">(</span>SIZE<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">13</td><td>  <span class="c">//warmup</span></td></tr><tr><td class="number">14</td><td>  copy<span class="k2">(</span>intvec1.begin<span class="k2">(</span><span class="k2">)</span>, intvec1.end<span class="k2">(</span><span class="k2">)</span>, intvec2.begin<span class="k2">(</span><span class="k2">)</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">15</td><td>  cout<span class="k3">&lt;</span><span class="k3">&lt;</span><span class="s">"Total time to copy "</span><span class="k3">&lt;</span><span class="k3">&lt;</span>SIZE<span class="k3">&lt;</span><span class="k3">&lt;</span><span class="s">" ints from one vector to other "</span><span class="k3">&lt;</span><span class="k3">&lt;</span>LOOPS<span class="k3">&lt;</span><span class="k3">&lt;</span><span class="s">" times:"</span><span class="k3">&lt;</span><span class="k3">&lt;</span>endl<span class="k2">;</span></td></tr><tr><td class="number">16</td><td>  clock_t before<span class="k3">=</span><a href="http://www.delorie.com/djgpp/doc/libc/libc_103.html" target="_blank">clock</a><span class="k2">(</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">17</td><td>  <span class="k1">for</span><span class="k2">(</span><span class="k1">int</span> i<span class="k3">=</span><span class="n">0</span><span class="k2">;</span>i<span class="k3">&lt;</span>LOOPS<span class="k2">;</span>i<span class="k3">+</span><span class="k3">+</span><span class="k2">)</span><span class="k2">{</span></td></tr><tr><td class="number">18</td><td>    copy<span class="k2">(</span>intvec1.begin<span class="k2">(</span><span class="k2">)</span>, intvec1.end<span class="k2">(</span><span class="k2">)</span>, intvec2.begin<span class="k2">(</span><span class="k2">)</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">19</td><td>  <span class="k2">}</span></td></tr><tr><td class="number">20</td><td>  clock_t after<span class="k3">=</span><a href="http://www.delorie.com/djgpp/doc/libc/libc_103.html" target="_blank">clock</a><span class="k2">(</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">21</td><td>  cout<span class="k3">&lt;</span><span class="k3">&lt;</span><span class="s">"std::copy:     "</span><span class="k3">&lt;</span><span class="k3">&lt;</span><span class="k2">(</span>after-before<span class="k2">)</span><span class="k3">&lt;</span><span class="k3">&lt;</span>endl<span class="k2">;</span></td></tr><tr><td class="number">22</td><td>&#160;</td></tr><tr><td class="number">23</td><td>  before<span class="k3">=</span><a href="http://www.delorie.com/djgpp/doc/libc/libc_103.html" target="_blank">clock</a><span class="k2">(</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">24</td><td>  <span class="k1">for</span><span class="k2">(</span><span class="k1">int</span> i<span class="k3">=</span><span class="n">0</span><span class="k2">;</span>i<span class="k3">&lt;</span>LOOPS<span class="k2">;</span>i<span class="k3">+</span><span class="k3">+</span><span class="k2">)</span><span class="k2">{</span></td></tr><tr><td class="number">25</td><td>    <span class="k1">for</span><span class="k2">(</span><span class="k1">int</span> j<span class="k3">=</span><span class="n">0</span><span class="k2">;</span>j<span class="k3">&lt;</span>SIZE<span class="k2">;</span>j<span class="k3">+</span><span class="k3">+</span><span class="k2">)</span><span class="k2">{</span></td></tr><tr><td class="number">26</td><td>      intvec2<span class="k2">[</span>j<span class="k2">]</span><span class="k3">=</span>intvec1<span class="k2">[</span>j<span class="k2">]</span><span class="k2">;</span></td></tr><tr><td class="number">27</td><td>    <span class="k2">}</span></td></tr><tr><td class="number">28</td><td>  <span class="k2">}</span></td></tr><tr><td class="number">29</td><td>  after<span class="k3">=</span><a href="http://www.delorie.com/djgpp/doc/libc/libc_103.html" target="_blank">clock</a><span class="k2">(</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">30</td><td>  cout<span class="k3">&lt;</span><span class="k3">&lt;</span><span class="s">"regular loop:  "</span><span class="k3">&lt;</span><span class="k3">&lt;</span><span class="k2">(</span>after-before<span class="k2">)</span><span class="k3">&lt;</span><span class="k3">&lt;</span>endl<span class="k2">;</span></td></tr><tr><td class="number">31</td><td>&#160;</td></tr><tr><td class="number">32</td><td>  before<span class="k3">=</span><a href="http://www.delorie.com/djgpp/doc/libc/libc_103.html" target="_blank">clock</a><span class="k2">(</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">33</td><td>  <span class="k1">for</span><span class="k2">(</span><span class="k1">int</span> i<span class="k3">=</span><span class="n">0</span><span class="k2">;</span>i<span class="k3">&lt;</span>LOOPS<span class="k2">;</span>i<span class="k3">+</span><span class="k3">+</span><span class="k2">)</span><span class="k2">{</span></td></tr><tr><td class="number">34</td><td>    vector<span class="k3">&lt;</span>int&gt;::const_iterator j <span class="k3">=</span> intvec1.begin<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">35</td><td>    vector<span class="k3">&lt;</span>int&gt;::iterator k <span class="k3">=</span> intvec2.begin<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">36</td><td>    <span class="k1">while</span><span class="k2">(</span>j <span class="k3">!</span><span class="k3">=</span> intvec1.end<span class="k2">(</span><span class="k2">)</span><span class="k2">)</span></td></tr><tr><td class="number">37</td><td>    <span class="k2">{</span></td></tr><tr><td class="number">38</td><td>      <span class="k3">*</span>k<span class="k3">+</span><span class="k3">+</span> <span class="k3">=</span> <span class="k3">*</span>j<span class="k3">+</span><span class="k3">+</span><span class="k2">;</span></td></tr><tr><td class="number">39</td><td>    <span class="k2">}</span></td></tr><tr><td class="number">40</td><td>  <span class="k2">}</span></td></tr><tr><td class="number">41</td><td>  after<span class="k3">=</span><a href="http://www.delorie.com/djgpp/doc/libc/libc_103.html" target="_blank">clock</a><span class="k2">(</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">42</td><td>  cout<span class="k3">&lt;</span><span class="k3">&lt;</span><span class="s">"iterator loop: "</span><span class="k3">&lt;</span><span class="k3">&lt;</span><span class="k2">(</span>after-before<span class="k2">)</span><span class="k3">&lt;</span><span class="k3">&lt;</span>endl<span class="k2">;</span></td></tr><tr><td class="number">43</td><td>  <span class="k1">return</span> <span class="n">0</span><span class="k2">;</span></td></tr><tr><td class="number">44</td><td>  </td></tr><tr><td class="number">45</td><td><span class="k2">}</span></td></tr></tbody></table></div></div><p>

</p><pre>cgames@ryan ~ $ g++ test.cpp -O3 -funroll-loops
cgames@ryan ~ $ ./a.out 
Total time to copy 100000 ints from one vector to other 10000 times:
std::copy:     690000
regular loop:  970000
iterator loop: 900000
</pre><p>

Okay, so it&#39;s faster, and by a fair margin, but why?</p><p>[edit]<br />While loop is faster <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (CGamesPlay)</author>
		<pubDate>Thu, 01 Mar 2007 04:13:48 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Here is the loop I had in mind:
</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">for</span><span class="k2">(</span><span class="k1">int</span> i<span class="k3">=</span><span class="n">0</span><span class="k2">;</span>i<span class="k3">&lt;</span>LOOPS<span class="k2">;</span>i<span class="k3">+</span><span class="k3">+</span><span class="k2">)</span>
  <span class="k1">for</span><span class="k2">(</span>it1 <span class="k3">=</span> intvec1.begin<span class="k2">(</span><span class="k2">)</span>, it2 <span class="k3">=</span> intvec3.begin<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span> it1 <span class="k3">!</span><span class="k3">=</span> intvec1.end<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span> it1<span class="k3">+</span><span class="k3">+</span>, it2<span class="k3">+</span><span class="k3">+</span><span class="k2">)</span>
    <span class="k2">(</span><span class="k3">*</span>it2<span class="k2">)</span> <span class="k3">=</span> <span class="k2">(</span><span class="k3">*</span>it1<span class="k2">)</span><span class="k2">;</span>
</pre></div></div><p>

With regular loop changed to that, and -O2:
</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
Total time to copy 10000000 ints from one vector to other 1000 times:<br />std::copy:    34062<br />regular loop: 34641
</p></div></div><p>

Negligible 1.5% difference.</p><p>Anyway, I was just playing - if there is a good way to use &lt;algorithm&gt; by all means, you should use it. </p><p>But it&#39;s not all that important if the loop version will be more readable. This is not so in the case of std::copy, which is very straight-forward. But in the case of algorithms that require you to define a function (which will often be used only this once), it&#39;s not worth the hassle. Not unless we get lambda functions in C++.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Jakub Wasilewski)</author>
		<pubDate>Thu, 01 Mar 2007 04:20:14 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Here&#39;s a silly.</p><div class="source-code"><div class="toolbar"></div><div class="inner"><table width="100%"><tbody><tr><td class="number">1</td><td><span class="p">#define _str(foo) #foo</span></td></tr><tr><td class="number">2</td><td><span class="p">#define str(foo) _str(foo)</span></td></tr><tr><td class="number">3</td><td><span class="p">#define TYPE list</span></td></tr><tr><td class="number">4</td><td>&#160;</td></tr><tr><td class="number">5</td><td><span class="p">#include &lt;iostream&gt;</span></td></tr><tr><td class="number">6</td><td><span class="p">#include &lt;list&gt;</span></td></tr><tr><td class="number">7</td><td><span class="p">#include &lt;algorithm&gt;</span></td></tr><tr><td class="number">8</td><td><span class="p">#include &lt;ctime&gt;</span></td></tr><tr><td class="number">9</td><td>&#160;</td></tr><tr><td class="number">10</td><td><span class="k1">using</span> <span class="k1">namespace</span> std<span class="k2">;</span></td></tr><tr><td class="number">11</td><td>&#160;</td></tr><tr><td class="number">12</td><td><span class="k1">const</span> <span class="k1">int</span> SIZE<span class="k3">=</span><span class="n">10000</span><span class="k2">;</span></td></tr><tr><td class="number">13</td><td><span class="k1">const</span> <span class="k1">int</span> LOOPS<span class="k3">=</span><span class="n">100000</span><span class="k2">;</span></td></tr><tr><td class="number">14</td><td><span class="k1">int</span> main<span class="k2">(</span><span class="k2">)</span><span class="k2">{</span></td></tr><tr><td class="number">15</td><td>  TYPE<span class="k3">&lt;</span>int&gt; intvec1<span class="k2">(</span>SIZE<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">16</td><td>  TYPE<span class="k3">&lt;</span>int&gt; intvec2<span class="k2">(</span>SIZE<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">17</td><td>  <span class="c">//warmup</span></td></tr><tr><td class="number">18</td><td>  copy<span class="k2">(</span>intvec1.begin<span class="k2">(</span><span class="k2">)</span>, intvec1.end<span class="k2">(</span><span class="k2">)</span>, intvec2.begin<span class="k2">(</span><span class="k2">)</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">19</td><td>  cout<span class="k3">&lt;</span><span class="k3">&lt;</span><span class="s">"Total time to copy "</span><span class="k3">&lt;</span><span class="k3">&lt;</span>SIZE<span class="k3">&lt;</span><span class="k3">&lt;</span><span class="s">" ints from one "</span> str<span class="k2">(</span>TYPE<span class="k2">)</span> <span class="s">" to other "</span><span class="k3">&lt;</span><span class="k3">&lt;</span>LOOPS<span class="k3">&lt;</span><span class="k3">&lt;</span><span class="s">" times:"</span><span class="k3">&lt;</span><span class="k3">&lt;</span>endl<span class="k2">;</span></td></tr><tr><td class="number">20</td><td>  clock_t before<span class="k3">=</span><a href="http://www.delorie.com/djgpp/doc/libc/libc_103.html" target="_blank">clock</a><span class="k2">(</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">21</td><td>  <span class="k1">for</span><span class="k2">(</span><span class="k1">int</span> i<span class="k3">=</span><span class="n">0</span><span class="k2">;</span>i<span class="k3">&lt;</span>LOOPS<span class="k2">;</span>i<span class="k3">+</span><span class="k3">+</span><span class="k2">)</span><span class="k2">{</span></td></tr><tr><td class="number">22</td><td>    copy<span class="k2">(</span>intvec1.begin<span class="k2">(</span><span class="k2">)</span>, intvec1.end<span class="k2">(</span><span class="k2">)</span>, intvec2.begin<span class="k2">(</span><span class="k2">)</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">23</td><td>  <span class="k2">}</span></td></tr><tr><td class="number">24</td><td>  clock_t after<span class="k3">=</span><a href="http://www.delorie.com/djgpp/doc/libc/libc_103.html" target="_blank">clock</a><span class="k2">(</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">25</td><td>  cout<span class="k3">&lt;</span><span class="k3">&lt;</span><span class="s">"std::copy:     "</span><span class="k3">&lt;</span><span class="k3">&lt;</span><span class="k2">(</span>after-before<span class="k2">)</span><span class="k3">&lt;</span><span class="k3">&lt;</span>endl<span class="k2">;</span></td></tr><tr><td class="number">26</td><td>&#160;</td></tr><tr><td class="number">27</td><td>  before<span class="k3">=</span><a href="http://www.delorie.com/djgpp/doc/libc/libc_103.html" target="_blank">clock</a><span class="k2">(</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">28</td><td>  <span class="k1">for</span><span class="k2">(</span><span class="k1">int</span> i<span class="k3">=</span><span class="n">0</span><span class="k2">;</span>i<span class="k3">&lt;</span>LOOPS<span class="k2">;</span>i<span class="k3">+</span><span class="k3">+</span><span class="k2">)</span><span class="k2">{</span></td></tr><tr><td class="number">29</td><td>    TYPE<span class="k3">&lt;</span>int&gt;::const_iterator j <span class="k3">=</span> intvec1.begin<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">30</td><td>    TYPE<span class="k3">&lt;</span>int&gt;::iterator k <span class="k3">=</span> intvec2.begin<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">31</td><td>    <span class="k1">while</span><span class="k2">(</span>j <span class="k3">!</span><span class="k3">=</span> intvec1.end<span class="k2">(</span><span class="k2">)</span><span class="k2">)</span></td></tr><tr><td class="number">32</td><td>    <span class="k2">{</span></td></tr><tr><td class="number">33</td><td>      <span class="k3">*</span>k<span class="k3">+</span><span class="k3">+</span> <span class="k3">=</span> <span class="k3">*</span>j<span class="k3">+</span><span class="k3">+</span><span class="k2">;</span></td></tr><tr><td class="number">34</td><td>    <span class="k2">}</span></td></tr><tr><td class="number">35</td><td>  <span class="k2">}</span></td></tr><tr><td class="number">36</td><td>  after<span class="k3">=</span><a href="http://www.delorie.com/djgpp/doc/libc/libc_103.html" target="_blank">clock</a><span class="k2">(</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">37</td><td>  cout<span class="k3">&lt;</span><span class="k3">&lt;</span><span class="s">"iterator loop: "</span><span class="k3">&lt;</span><span class="k3">&lt;</span><span class="k2">(</span>after-before<span class="k2">)</span><span class="k3">&lt;</span><span class="k3">&lt;</span>endl<span class="k2">;</span></td></tr><tr><td class="number">38</td><td>  <span class="k1">return</span> <span class="n">0</span><span class="k2">;</span></td></tr><tr><td class="number">39</td><td>  </td></tr><tr><td class="number">40</td><td><span class="k2">}</span></td></tr></tbody></table></div></div><p>

</p><pre>$ g++ test.cpp -O3 -funroll-loops &amp;&amp; power ./a.out 
Total time to copy 10000 ints from one list to other 100000 times:
std::copy:     7220000
iterator loop: 6930000</pre><p>

&lt;hypothesis&gt;It seems copy is doing a memcpy internally, overridden for vectors. When it can&#39;t do so and must use a generic copy function, the overhead of the method pointers causes it to be slower.&lt;/hypothesis&gt;
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (CGamesPlay)</author>
		<pubDate>Thu, 01 Mar 2007 04:26:31 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Put all together and ran on my Core2 at 32bit:
</p><div class="source-code snippet"><div class="inner"><pre><span class="p">#g++ std_copy.cpp -O3 -funroll-loops &amp;&amp; ./a.out</span>

Total <a href="http://www.delorie.com/djgpp/doc/libc/libc_821.html" target="_blank">time</a> to copy <span class="n">100000</span> ints from one vector to other <span class="n">10000</span> times:
std::copy:     <span class="n">390000</span>
regular loop:  <span class="n">550000</span>
iterator loop: <span class="n">580000</span>
Jakub<span class="s">'s loop:  560000</span>
</pre></div></div><p>I ran it several times and there weren&#39;t much changes.</p><p>Different CPU&#39;s might give different results.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (HoHo)</author>
		<pubDate>Thu, 01 Mar 2007 04:29:17 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>You have a fast machine, HoHo <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" /> What clock/cache?</p><p>I&#39;m running a Core Duo at 3.0 GHz with I think 4 MB cache between the cores.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (CGamesPlay)</author>
		<pubDate>Thu, 01 Mar 2007 04:33:17 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Core2Duo e4300@2.93GHz at the moment. That means 2M L2 shared between two cores and ~62% OC. Could go higher when I could somehow give it more than default voltage <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" /><br />Your last code with -O3 -funroll-loops gave me this:
</p><div class="source-code snippet"><div class="inner"><pre>Total <a href="http://www.delorie.com/djgpp/doc/libc/libc_821.html" target="_blank">time</a> to copy <span class="n">10000</span> ints from one list to other <span class="n">100000</span> times:
std::copy:     <span class="n">1480000</span>
iterator loop: <span class="n">1520000</span>
</pre></div></div><p>When I changed list to vector then iterator loop actually got faster than std::copy <img src="http://www.allegro.cc/forums/smileys/shocked.gif" alt=":o" />
</p><div class="source-code snippet"><div class="inner"><pre>Total <a href="http://www.delorie.com/djgpp/doc/libc/libc_821.html" target="_blank">time</a> to copy <span class="n">10000</span> ints from one vector to other <span class="n">100000</span> times:
std::copy:     <span class="n">480000</span>
iterator loop: <span class="n">430000</span>
</pre></div></div><p>Well, all I can say is when in doubt, benchmark <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" /><br />Also please note the speed difference between lists and vectors.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (HoHo)</author>
		<pubDate>Thu, 01 Mar 2007 04:37:30 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Yeah lists are bloated and are almost completely useless because of it.  They&#39;re more useful for academic purposes then anything else.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (ImLeftFooted)</author>
		<pubDate>Thu, 01 Mar 2007 04:44:26 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
Yeah lists are bloated and are almost completely useless because of it. They&#39;re more useful for academic purposes then anything else.
</p></div></div><p>

Hm, lists have faster insertions that vectors. You can remove elements in constant time. If you sort a list of MyClass (using list::sort), it could seriously outperform std::sort on vector, because it doesn&#39;t need to copy actual data, just rearrange the pointers.<br />However, iterating through a list is slower, because vector uses a random_iterator.</p><p>Each STL container has its strenghts and weaknesses. Sometimes it might even be more efficient to use one type for one task, and then copy the data to a different container to do something else with the data. </p><p>For example, std::set is blazing fast to get you unique, sorted data. If you needed a lot of random access on the data later, you might copy it to a vector before continuing. (Benchmark, of course.)</p><p>***</p><p>By the way, they say that the container&#39;s &quot;native&quot; methods could be faster than the equivalent std::algorithm (optimized for this particular container). Anybody care to benchmark operator = ?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (anonymous)</author>
		<pubDate>Thu, 01 Mar 2007 05:09:58 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
Hm, lists have faster insertions that vectors You can remove elements in constant time
</p></div></div><p>If the vector doesn&#39;t need to be sorted you can add and remove stuff from there in constant time too.</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
If you sort a list of MyClass (using list::sort), it could seriously outperform std::sort on vector, because it doesn&#39;t need to copy actual data
</p></div></div><p>That depends on how you use it. If you have enormous list then iterating it over and over again will probably take more time than with vectors (need benchmarks!). If  you don&#39;t keep pointers in the container and the elements are big then lists may win.</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
Each STL container has its strenghts and weaknesses
</p></div></div><p>I agree. Though if you can alter your design just a little you may find that vectors perform better than lists for most things. Also in some cases deque may be a bit better than vector.</p><p>Last time I used lists was about three years ago in uni when we were asked to write a list class <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (HoHo)</author>
		<pubDate>Thu, 01 Mar 2007 13:08:35 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
If the vector doesn&#39;t need to be sorted you can add and remove stuff from there in constant time too.
</p></div></div><p>
That&#39;s a big &quot;if&quot;. And a vector may get copied using push_back(). And if you wanted to keep the container sorted, a multiset would probably be fastest.</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
If you sort a list of MyClass (using list::sort), it could seriously outperform std::sort on vector, because it doesn&#39;t need to copy actual data
</p></div></div><p>
That depends on how you use it. If you have enormous list then iterating it over and over again will probably take more time than with vectors (need benchmarks!). If you don&#39;t keep pointers in the container and the elements are big then lists may win.
</p></div></div><p>

List can win clearly, if the copy constructor is expensive. For example when sorting std::strings.</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
Each STL container has its strenghts and weaknesses
</p></div></div><p>
I agree. Though if you can alter your design just a little you may find that vectors perform better than lists for most things. Also in some cases deque may be a bit better than vector.</p><p>Last time I used lists was about three years ago in uni when we were asked to write a list class <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" />
</p></div></div><p>

Yes, a vector is a most general use container, because very often random access is what you need (replacement to single array). However, it is impossible to design a container that works remarkably well for all purposes, because you need a different structure for each purpose. Knowing the tools is good, also STL is designed in a way, that it is relatively easy to switch the container you are using and to move data to a different container.</p><p>I can also see how implementing your own list deters you from using std::list <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (anonymous)</author>
		<pubDate>Thu, 01 Mar 2007 13:52:47 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
If the vector doesn&#39;t need to be sorted you can add and remove stuff from there in constant time too.
</p></div></div><p>
That&#39;s a big &quot;if&quot;. And a vector may get copied using push_back(). And if you wanted to keep the container sorted, a multiset would probably be fastest.
</p></div></div><p>

Not if it is a compliant STL implementation and you have already reserved enough slots for your data set (two conditions which are easily satisfied).</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
If you sort a list of MyClass (using list::sort), it could seriously outperform std::sort on vector, because it doesn&#39;t need to copy actual data
</p></div></div><p>
That depends on how you use it. If you have enormous list then iterating it over and over again will probably take more time than with vectors (need benchmarks!). If you don&#39;t keep pointers in the container and the elements are big then lists may win.
</p></div></div><p>
List can win clearly, if the copy constructor is expensive. For example when sorting std::strings.
</p></div></div><p>

Of course, vectors can easily win if you don&#39;t sort the actual array contents (you generate an sorted index-&gt;actual index mapping that happens to return items in sorted order).  Completely avoids copy constructor overhead.  Granted, this is not what is done in any STL container as far as I know, but it is possible to sort a vector-like container with zero data copies of the actual container data this way... (it would be interesting though to have an STL wrapper object that does just this for all random access data types; call it something like sorted_wrapper.) <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Carrus85)</author>
		<pubDate>Thu, 01 Mar 2007 15:11:00 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
Of course, vectors can easily win if you don&#39;t sort the actual array contents (you generate an sorted index-&gt;actual index mapping that happens to return items in sorted order). Completely avoids copy constructor overhead. Granted, this is not what is done in any STL container as far as I know, but it is possible to sort a vector-like container with zero data copies of the actual container data this way... (it would be interesting though to have an STL wrapper object that does just this for all random access data types; call it something like sorted_wrapper.)
</p></div></div><p>

For copy-expensive objects, you can sort a vector of pointers to the object. Although it is still questionable if it is worth the effort if list can do this without any extra work. <br />(I created an expensive class consisting of two ints and a string containing &quot;Hello world&quot;, and overloaded comparison to sort it based on one of the randomly initialized ints. Then I sorted a vector of these objects, a vector of pointers and a list (identical data to the first vector). The results: sorting pointers ~3 x faster than first vector, sorting list ~5-6 x faster than a regular vector.)</p><p>Your idea is interesting, but wouldn&#39;t it produce massive overhead - no less than a list? Also, after sorting, wouldn&#39;t you still need to do the copying in one go (adds at least O(N) and seems tricky to do in-place)?</p><p>Anyway, I&#39;m not saying list is better than vector <i>per se</i> (or the other way round). But a simple contiguous block of memory just doesn&#39;t suit well for all kinds of manipulations. Nor does any other data structure. So you have to make choices.</p><p>Finally, here&#39;s <a href="http://www.tantalon.com/pete/cppopt/appendix.htm#AppendixA">a link</a> to compare the effectiveness of some operations with different containers. (Keeping in mind that O(N) can mean very different things with different containers.)</p><p>The rest of the site is interesting reading too!
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (anonymous)</author>
		<pubDate>Thu, 01 Mar 2007 20:56:03 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>...and after all, keep in mind that STL containers are designed to use the same interface wherever possible, so if all is well, changing the container type for a given application later on shouldn&#39;t be too much of a problem. Just profile them against each other and see what&#39;s faster.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Tobias Dammers)</author>
		<pubDate>Thu, 01 Mar 2007 22:20:18 +0000</pubDate>
	</item>
</rss>
