<?xml-stylesheet type="text/xsl" href="https://community.element14.com/cfs-file/__key/system/syndication/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>C++ Tutorial - Hash Tables</title><link>/technologies/code_exchange/b/blog/posts/c-tutorial---hash-tables</link><description>IntroductionIn the previous tutorial, we talked about constants. By using a constant, you are letting everyone else know that a given value will not change. Constants can be applied to variables, function parameters and functions themselves.&amp;amp;nb...</description><dc:language>en-US</dc:language><generator>Telligent Community 12</generator><item><title>RE: C++ Tutorial - Hash Tables</title><link>https://community.element14.com/technologies/code_exchange/b/blog/posts/c-tutorial---hash-tables</link><pubDate>Thu, 10 May 2018 15:03:50 GMT</pubDate><guid isPermaLink="false">93d5dcb4-84c2-446f-b2cb-99731719e767:2a066040-ea52-4c69-a369-329ead898bdd</guid><dc:creator>pjcar2000</dc:creator><slash:comments>0</slash:comments><description>&lt;p&gt;Very helpful article.&amp;nbsp; FYI - the last example with the MakeHash class is not a complete program on its own. Here is the code that I put together using portions of the earlier code samples to complete it:&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;#include &amp;lt;iostream&amp;gt;&lt;/p&gt;&lt;p&gt;#include &amp;lt;string.h&amp;gt;&lt;/p&gt;&lt;p&gt;#include &amp;lt;unordered_map&amp;gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;using namespace std;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;class Make&lt;/p&gt;&lt;p&gt;{&lt;/p&gt;&lt;p&gt;&amp;nbsp; public:&lt;/p&gt;&lt;p&gt;&amp;nbsp; string Name;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;&amp;nbsp; Make(string name)&lt;/p&gt;&lt;p&gt;&amp;nbsp; {&lt;/p&gt;&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Name = name;&lt;/p&gt;&lt;p&gt;&amp;nbsp; }&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;&amp;nbsp; bool operator==(const Make &amp;amp;make) const&lt;/p&gt;&lt;p&gt;&amp;nbsp; {&lt;/p&gt;&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; return Name == make.Name;&lt;/p&gt;&lt;p&gt;&amp;nbsp; }&lt;/p&gt;&lt;p&gt;}; &lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;class Model&lt;/p&gt;&lt;p&gt;{&lt;/p&gt;&lt;p&gt;&amp;nbsp; public:&lt;/p&gt;&lt;p&gt;&amp;nbsp; string Name;&lt;/p&gt;&lt;p&gt;&amp;nbsp; int Year;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;&amp;nbsp; Model(string name, int year)&lt;/p&gt;&lt;p&gt;&amp;nbsp; {&lt;/p&gt;&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Name = name;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Year = year;&lt;/p&gt;&lt;p&gt;&amp;nbsp; }&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;&amp;nbsp; bool operator==(const Model &amp;amp;model) const&lt;/p&gt;&lt;p&gt;&amp;nbsp; {&lt;/p&gt;&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; return (Name == model.Name &amp;amp;&amp;amp; Year == model.Year);&lt;/p&gt;&lt;p&gt;&amp;nbsp; }&lt;/p&gt;&lt;p&gt;};&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;class MakeHash&lt;/p&gt;&lt;p&gt;{&lt;/p&gt;&lt;p&gt;&amp;nbsp; public:&lt;/p&gt;&lt;p&gt;&amp;nbsp; size_t operator()(const Make &amp;amp;make) const&lt;/p&gt;&lt;p&gt;&amp;nbsp; {&lt;/p&gt;&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; return hash&amp;lt;string&amp;gt;()(make.Name);&lt;/p&gt;&lt;p&gt;&amp;nbsp; }&lt;/p&gt;&lt;p&gt;};&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;class ModelHash&lt;/p&gt;&lt;p&gt;{&lt;/p&gt;&lt;p&gt;&amp;nbsp; public:&lt;/p&gt;&lt;p&gt;&amp;nbsp; size_t operator()(const Model &amp;amp;model) const&lt;/p&gt;&lt;p&gt;&amp;nbsp; {&lt;/p&gt;&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; return hash&amp;lt;string&amp;gt;()(model.Name) ^ hash&amp;lt;int&amp;gt;()(model.Year);&lt;/p&gt;&lt;p&gt;&amp;nbsp; }&lt;/p&gt;&lt;p&gt;};&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;int main()&lt;/p&gt;&lt;p&gt;{&lt;/p&gt;&lt;p&gt;&amp;nbsp; unordered_map&amp;lt;Make, vector&amp;lt;Model&amp;gt;, MakeHash&amp;gt; make2model;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;&amp;nbsp; Model camry2005(&amp;quot;Camry&amp;quot;, 2005);&lt;/p&gt;&lt;p&gt;&amp;nbsp; Model tercel1993(&amp;quot;Tercel&amp;quot;, 1993);&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;&amp;nbsp; Make toyota(&amp;quot;Toyota&amp;quot;);&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;&amp;nbsp; vector&amp;lt;Model&amp;gt; toyotaModels;&lt;/p&gt;&lt;p&gt;&amp;nbsp; toyotaModels.push_back(camry2005);&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;&amp;nbsp; make2model.emplace(toyota, toyotaModels);&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;&amp;nbsp; make2model[toyota].push_back(tercel1993);&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;&amp;nbsp; for (auto &amp;amp;make : make2model)&lt;/p&gt;&lt;p&gt;&amp;nbsp; {&lt;/p&gt;&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; cout &amp;lt;&amp;lt; make.first.Name &amp;lt;&amp;lt; endl;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; vector&amp;lt;Model&amp;gt; models = make.second;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; for(auto &amp;amp;model : models)&lt;/p&gt;&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; cout &amp;lt;&amp;lt; &amp;quot; &amp;quot; &amp;lt;&amp;lt; model.Name &amp;lt;&amp;lt; &amp;quot; &amp;quot; &amp;lt;&amp;lt; model.Year &amp;lt;&amp;lt; endl;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;&lt;p&gt;&amp;nbsp; }&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;&amp;nbsp; return 0;&lt;/p&gt;&lt;p&gt;}&lt;/p&gt;&lt;img src="https://community.element14.com/aggbug?PostID=15800&amp;AppID=74&amp;AppType=Weblog&amp;ContentType=0" width="1" height="1"&gt;</description></item><item><title>RE: C++ Tutorial - Hash Tables</title><link>https://community.element14.com/technologies/code_exchange/b/blog/posts/c-tutorial---hash-tables</link><pubDate>Mon, 09 Nov 2015 11:47:25 GMT</pubDate><guid isPermaLink="false">93d5dcb4-84c2-446f-b2cb-99731719e767:2a066040-ea52-4c69-a369-329ead898bdd</guid><dc:creator>Former Member</dc:creator><slash:comments>0</slash:comments><description>&lt;p&gt;thanks for solved this problem&lt;/p&gt;&lt;img src="https://community.element14.com/aggbug?PostID=15800&amp;AppID=74&amp;AppType=Weblog&amp;ContentType=0" width="1" height="1"&gt;</description></item><item><title>RE: C++ Tutorial - Hash Tables</title><link>https://community.element14.com/technologies/code_exchange/b/blog/posts/c-tutorial---hash-tables</link><pubDate>Sat, 23 May 2015 00:45:05 GMT</pubDate><guid isPermaLink="false">93d5dcb4-84c2-446f-b2cb-99731719e767:2a066040-ea52-4c69-a369-329ead898bdd</guid><dc:creator>Former Member</dc:creator><slash:comments>1</slash:comments><description>&lt;p&gt;Thanks for that. I found this blog on the topic of books for learning C++: &lt;a class="jive-link-external-small" href="http://stackoverflow.com/questions/388242/" rel="nofollow ugc noopener" target="_blank"&gt;http://stackoverflow.com/questions/388242/&lt;/a&gt;&lt;/p&gt;&lt;p&gt;Also, I&amp;#39;m discovering the difference between bringing Armadillo into Raspbian (Raspberry Pi) vs. into VS 2013 on Windows, 10 minutes (which was mostly just install time) vs. several hours and counting.&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;Any recommendations for learning that sort of stuff? Eg. how to correctly set up dependencies, libraries, .dlls, that sort of thing? Or is that wishful thinking? I encountered the same nightmare working Java in Netbeans or Eclipse a few years back. In other words, it&amp;#39;s one thing to learn the language, it&amp;#39;s another to learn how to put everyone else&amp;#39;s parts together so you don&amp;#39;t have to build everything yourself and refine it forever until you optimize your run time. In other words, I ain&amp;#39;t recreating Armadillo. Any points on how to learn that stuff? Or is that just the way it goes? I know a chief problem there is that unless you know just how to search, the answers you find apply only partly to your problem--and frequently partial solutions don&amp;#39;t work--you need the whole solution, which is almost always unique for everyone&amp;#39;s&amp;#39; uniquely composited problem.&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;That will be my last question here, I promise. It is just that it can be discouraging to learn to program only to find out you don&amp;#39;t get far with it in the real world of releasing software or building something outside of the confines of prototyping environments. I can do a lot in some nice environment like Octave, but if I have to compile and build anything using the wonderful work of others&amp;#39; libraries, flip a coin as to whether I am going to get anywhere in a given environment--at least not without extensive delays. If I have a method to attack that sort of problem, however, I should think it would be well worth the effort. Again, thanks.&lt;/p&gt;&lt;img src="https://community.element14.com/aggbug?PostID=15800&amp;AppID=74&amp;AppType=Weblog&amp;ContentType=0" width="1" height="1"&gt;</description></item><item><title>RE: C++ Tutorial - Hash Tables</title><link>https://community.element14.com/technologies/code_exchange/b/blog/posts/c-tutorial---hash-tables</link><pubDate>Wed, 13 May 2015 05:25:10 GMT</pubDate><guid isPermaLink="false">93d5dcb4-84c2-446f-b2cb-99731719e767:2a066040-ea52-4c69-a369-329ead898bdd</guid><dc:creator>Former Member</dc:creator><slash:comments>1</slash:comments><description>&lt;p&gt;Also, any chance you could explain a few things here:&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;1) Why the == after the return variable declaration and before the () parameter, and why the second mention of const after the () parameter in the operator functions, eg.&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;&lt;span&gt;bool&lt;/span&gt;&lt;span&gt; operator==(&lt;/span&gt;&lt;span&gt;const&lt;/span&gt; &lt;span&gt;Make&lt;/span&gt;&lt;span&gt; &amp;amp;&lt;/span&gt;&lt;span&gt;make&lt;/span&gt;&lt;span&gt;) &lt;/span&gt;&lt;span&gt;const&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;&lt;span&gt;2) and in this one why the initial set of () before the parameter in the parentheses?&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;&lt;span&gt;&lt;span&gt;size_t&lt;/span&gt;&lt;span&gt; operator()(&lt;/span&gt;&lt;span&gt;const&lt;/span&gt; &lt;span&gt;Model&lt;/span&gt;&lt;span&gt; &amp;amp;&lt;/span&gt;&lt;span&gt;model&lt;/span&gt;&lt;span&gt;) &lt;/span&gt;&lt;span&gt;const&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;Thanks for the tutorial. Edit: here&amp;#39;s a link for more info about point 1 (above), and of course, about C++ in general:&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;&lt;span&gt;&lt;a class="jive-link-external-small" href="http://en.cppreference.com/w/cpp/language/operator_comparison" rel="nofollow ugc noopener" target="_blank"&gt;http://en.cppreference.com/w/cpp/language/operator_comparison&lt;/a&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;img src="https://community.element14.com/aggbug?PostID=15800&amp;AppID=74&amp;AppType=Weblog&amp;ContentType=0" width="1" height="1"&gt;</description></item><item><title>RE: C++ Tutorial - Hash Tables</title><link>https://community.element14.com/technologies/code_exchange/b/blog/posts/c-tutorial---hash-tables</link><pubDate>Wed, 13 May 2015 04:02:52 GMT</pubDate><guid isPermaLink="false">93d5dcb4-84c2-446f-b2cb-99731719e767:2a066040-ea52-4c69-a369-329ead898bdd</guid><dc:creator>Former Member</dc:creator><slash:comments>0</slash:comments><description>&lt;p&gt;A few errors:&lt;/p&gt;&lt;p&gt;&lt;span&gt;include&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span&gt;&amp;lt;string&amp;gt; //needed to get code to work&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;&lt;span&gt;&lt;span&gt;Make&lt;/span&gt;&lt;span&gt; toyota(&lt;/span&gt;&lt;span&gt;&amp;quot;Toyota&amp;quot;&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span&gt;&amp;quot;Japan&amp;quot;&lt;/span&gt;&lt;span&gt;);//doesn&amp;#39;t compile. Two arguments given, only one accepted in constructor. Need to change to:&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span&gt;&lt;span&gt;Make&lt;/span&gt;&lt;span&gt; toyota(&lt;/span&gt;&lt;span&gt;&amp;quot;Toyota&amp;quot;&lt;/span&gt;&lt;span&gt;);//&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;img src="https://community.element14.com/aggbug?PostID=15800&amp;AppID=74&amp;AppType=Weblog&amp;ContentType=0" width="1" height="1"&gt;</description></item></channel></rss>