<?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>Talk Binary &#187; container</title> <atom:link href="http://talkbinary.com/tag/container/feed/" rel="self" type="application/rss+xml" /><link>http://talkbinary.com</link> <description>Programming Resources, Technology, Computers</description> <lastBuildDate>Wed, 08 Sep 2010 14:00:51 +0000</lastBuildDate> <language>en</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>http://wordpress.org/?v=3.0.1</generator> <item><title>Binary Search in C++</title><link>http://talkbinary.com/programming/c/algorithms/binary-search-in-c/</link> <comments>http://talkbinary.com/programming/c/algorithms/binary-search-in-c/#comments</comments> <pubDate>Sun, 28 Dec 2008 08:53:55 +0000</pubDate> <dc:creator>Diego</dc:creator> <category><![CDATA[Algorithms]]></category> <category><![CDATA[algorithm]]></category> <category><![CDATA[binary search]]></category> <category><![CDATA[C++]]></category> <category><![CDATA[code]]></category> <category><![CDATA[container]]></category> <category><![CDATA[pseudo-code]]></category> <category><![CDATA[pseudocode]]></category> <category><![CDATA[search]]></category> <category><![CDATA[source code]]></category><guid
isPermaLink="false">http://talkbinary.com/?p=1393</guid> <description><![CDATA[Binary Search is a method to search through a sorted container in order to find a particular value. Remember that one &#8220;Guess the Number&#8221; game? Let&#8217;s look at the following scenario by applying the Binary Search algorithm in order for you to understand how Binary Search works. The Guessing Game Pick a number from 0 [...]]]></description> <content:encoded><![CDATA[<p>Binary Search is a method to search through a sorted container in order to find a particular value.</p><p>Remember that one &#8220;Guess the Number&#8221; game? Let&#8217;s look at the following scenario by applying the Binary Search algorithm in order for you to understand how Binary Search works.</p><blockquote><h3>The Guessing Game</h3><p>Pick a number from 0 &#8211; 100 and depending on your guess, I&#8217;ll either say Correct, Higher, or Lower. What would you choose?</p><table><tr><td>0</td><td>1</td><td>&#8230;</td><td>49</td><td>50</td><td>51</td><td>&#8230;</td><td>99</td><td>100</td></tr></table><p>Well, 50 of course! Why? Best case you get it correct. Worst case you&#8217;ll either get a &#8220;Higher&#8221; or &#8220;Lower&#8221;. Now think about the following, if you got &#8220;Higher&#8221; you eliminated 0-49, or 51-100 if &#8220;Lower&#8221;, in other words, you eliminate HALF the possibilities.</p><table><tr><td>50</td><td>51</td><td>&#8230;</td><td>74</td><td>75</td><td>76</td><td>&#8230;</td><td>99</td><td>100</td></tr></table><p>Well, let&#8217;s say it was &#8220;Higher&#8221;. What would you guess after? 75. Since it&#8217;s in between 50 &#8211; 100. If you didn&#8217;t guess correctly, you&#8217;ll be facing the similar scenario as before. You&#8217;ll end up eliminating half the possibilities!</p></blockquote><p><span
id="more-1393"></span></p><h3>Binary Search Algorithm</h3><p>The following is the recursive implementation.</p><pre lang="c++">int binarysearch(vector <int> vec, int low, int high, int key)
{
    //Not found
    if ( low > high )
    {
          return -1;
    }

    //Finding the middle index between high and low
    int mid = (low + high)/2;

    //Returning the index if key is found
    if ( vec[mid] == key )
    {
          return mid;
    }
    //If middle index value is larger than our key, we search the lower half of the vector
    //else we search the upper half of the vector
    else if ( vec[mid] > key )
    {
          return binarysearch(vec, low, mid-1, key);
    }
    else
    {
          return binarysearch(vec, mid+1, high, key);
    }
}</pre><h3>Example of Binary Search</h3><p>Let&#8217;s say our container held the values 0-100 in its respective index values. Let&#8217;s say the value we are looking for is 57.</p><h4>First iteration</h4><table
width="90%"><tr><td>Index:</td><td>0</td><td>1</td><td>&#8230;</td><td>49</td><td>50</td><td>51</td><td>&#8230;</td><td>99</td><td>100</td></tr><tr><td>Value</td><td>0</td><td>1</td><td>&#8230;</td><td>49</td><td>50</td><td>51</td><td>&#8230;</td><td>99</td><td>100</td></tr><tr><td>Parameters:</td><td>low</td><td></td><td></td><td></td><td>mid</td><td></td><td></td><td></td><td>high</td></tr></table><p>50 < 57, so search through the upper half of our vector.</p><h4>Second iteration</h4><table
width="90%"><tr><td>Index:</td><td>51</td><td>52</td><td>&#8230;</td><td>74</td><td>75</td><td>76</td><td>&#8230;</td><td>99</td><td>100</td></tr><tr><td>Value</td><td>51</td><td>52</td><td>&#8230;</td><td>74</td><td>75</td><td>76</td><td>&#8230;</td><td>99</td><td>100</td></tr><tr><td>Parameters:</td><td>low</td><td></td><td></td><td></td><td>mid</td><td></td><td></td><td></td><td>high</td></tr></table><p>75 > 57, so search through the lower half of our vector.</p><h4>Third iteration</h4><table
width="90%"><tr><td>Index:</td><td>51</td><td>52</td><td>&#8230;</td><td>62</td><td>63</td><td>64</td><td>&#8230;</td><td>73</td><td>74</td></tr><tr><td>Value</td><td>51</td><td>52</td><td>&#8230;</td><td>62</td><td>63</td><td>64</td><td>&#8230;</td><td>73</td><td>74</td></tr><tr><td>Parameters:</td><td>low</td><td></td><td></td><td></td><td>mid</td><td></td><td></td><td></td><td>high</td></tr></table><p>63 > 57, so search through the lower half of our vector.</p><h4>Fourth iteration</h4><table
width="90%"><tr><td>Index:</td><td>51</td><td>52</td><td>&#8230;</td><td>56</td><td>57</td><td>58</td><td>&#8230;</td><td>61</td><td>62</td></tr><tr><td>Value</td><td>51</td><td>52</td><td>&#8230;</td><td>56</td><td>57</td><td>58</td><td>&#8230;</td><td>61</td><td>62</td></tr><tr><td>Parameters:</td><td>low</td><td></td><td></td><td></td><td>mid</td><td></td><td></td><td></td><td>high</td></tr></table><p>57 == 57!</p> <iframe
class="me-likey" src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Ftalkbinary.com%2Fprogramming%2Fc%2Falgorithms%2Fbinary-search-in-c%2F&amp;layout=standard&amp;show_faces=true&amp;width=475&amp;height=25&amp;action=like&amp;font=arial&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="true" style="border:none; overflow:hidden; width:475px; height:25px"></iframe>]]></content:encoded> <wfw:commentRss>http://talkbinary.com/programming/c/algorithms/binary-search-in-c/feed/</wfw:commentRss> <slash:comments>3</slash:comments> </item> <item><title>Simple Data Structure &#8211; The Vector</title><link>http://talkbinary.com/programming/c/simple-data-structure-the-vector/</link> <comments>http://talkbinary.com/programming/c/simple-data-structure-the-vector/#comments</comments> <pubDate>Fri, 18 Jul 2008 19:32:01 +0000</pubDate> <dc:creator>Diego</dc:creator> <category><![CDATA[C++]]></category> <category><![CDATA[container]]></category> <category><![CDATA[data structure]]></category> <category><![CDATA[vector]]></category><guid
isPermaLink="false">http://talkbinary.com/?p=297</guid> <description><![CDATA[The vector is a simple data structure that allows you to store values with the same data type in one container. Introduction Without the use of a data structure, how would you store multiple variables? Let&#8217;s say we wanted to store 5 names into strings. How would this be done? string name1,name2,name3,name4,name5; cin >> name1 [...]]]></description> <content:encoded><![CDATA[<p>The vector is a simple data structure that allows you to store values with the same data type in one container.</p><h3>Introduction</h3><p>Without the use of a data structure, how would you store multiple variables? Let&#8217;s say we wanted to store 5 names into strings. How would this be done?</p><pre lang="c++">
string name1,name2,name3,name4,name5;
cin >> name1 >> name2 >> name3 >> name4 >> name5;</pre><p>That looks a bit ugly doesn&#8217;t it? What if we wanted to store the number of names the user wanted to input? That is where the vector comes in.<br
/> <span
id="more-297"></span></p><h3>The Vector</h3><pre lang="c++">#include <vector> //You need to include this library to use the vector

//Syntax:
vector <data_type> name;</pre><p>To populate our vector with 5 names, we would simply do the following:</p><pre lang="c++">vector <string> names;

string input;
for ( int i = 0; i < 5; i++ ) {
     cin >> input;
     names.push_back(input);
}</pre><p>In other words, we simply ask the user five times with our for loop, to type in the terminal a name, and it will be <b>push_back</b>&#8216;ed onto our vector. Every single time we use the push_back <b>member function</b> of the vector, we simply push the newly inserted element to the back. Let&#8217;s use the following visual example of a vector.<br
/><center><a
href="http://talkbinary.com/wp-content/uploads/2008/07/vector.jpg"><img
src="http://talkbinary.com/wp-content/uploads/2008/07/vector.jpg" alt="" title="vector" width="441" height="90" class="alignnone size-full wp-image-302" /></a></center><br
/> Notice how the <strong>positions</strong> are labeled. The <strong>size</strong> of the vector is the number of elements in the vector. To create this vector, we would do the following.</p><pre lang="c++">vector <string> names;
names.push_back("Joe");
names.push_back("Beth");
names.push_back("Mary");
names.push_back("Jon");
names.push_back("Sue");</pre><p>How would we access and print out a variable?</p><h3>Printing out elements of the vector</h3><p>To print out an element we would simply do the following:</p><pre lang="c++">cout << names[0];</pre><p>This allows us to print out the value that is contained in position zero. Therefore, "Joe".<br
/> <br/><br
/> To find out the size of our vector we would simply do:</p><pre lang="c++">cout << names.size();</pre><p>This would print out <b>5</b> since our vector contains 5 elements.<br
/> <br/><br
/> To <b>remove</b> an element from our vector we would simply do:</p><pre lang="c++">names.pop_back();</pre><p>This would remove "Sue" from our vector, giving it a size of 4.</p><h3>Doing more with the vector</h3><p><b>Challenge 1:</b> Populate a vector with 5 integer values, and print them all out.<br
/> <b>Challenge 2:</b> Print out all the elements of a vector using a for loop.<br
/> <br/></p><h3>What now?</h3><p>Join our forums and discuss your newly discovered vector! Remember, there is still so much more to learn with how to use a vector, this is simply a tutorial for beginners and isn't meant for advanced users.</p> <iframe
class="me-likey" src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Ftalkbinary.com%2Fprogramming%2Fc%2Fsimple-data-structure-the-vector%2F&amp;layout=standard&amp;show_faces=true&amp;width=475&amp;height=25&amp;action=like&amp;font=arial&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="true" style="border:none; overflow:hidden; width:475px; height:25px"></iframe>]]></content:encoded> <wfw:commentRss>http://talkbinary.com/programming/c/simple-data-structure-the-vector/feed/</wfw:commentRss> <slash:comments>6</slash:comments> </item> </channel> </rss>
<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk
Page Caching using disk (enhanced) (user agent is rejected)
Database Caching 19/40 queries in 0.232 seconds using disk

Served from: talkbinary.com @ 2010-09-09 16:20:31 -->