Popular C++ recipes http://code.activestate.com/recipes/langs/cpp/popular/2016-11-17T15:01:12-08:00ActiveState Code RecipesConvert from Html To Pdf in ASP.NET MVC C# with SelectPdf Free Community Edition (C++) 2016-11-17T15:01:12-08:00SelectPdfhttp://code.activestate.com/recipes/users/4193129/http://code.activestate.com/recipes/580719-convert-from-html-to-pdf-in-aspnet-mvc-c-with-sele/ <p style="color: grey"> C++ recipe 580719 by <a href="/recipes/users/4193129/">SelectPdf</a> (<a href="/recipes/tags/aspnet/">aspnet</a>, <a href="/recipes/tags/mvc/">mvc</a>, <a href="/recipes/tags/pdf/">pdf</a>, <a href="/recipes/tags/selectpdf/">selectpdf</a>). </p> <p>It’s very easy to use SelectPdf SDK for .NET in ASP.NET MVC applications. Take a look at the simple code below.</p> Hopfield Artificial Neural Network (C++) 2016-07-03T21:58:49-07:00Jay ballerhttp://code.activestate.com/recipes/users/4194368/http://code.activestate.com/recipes/580688-hopfield-artificial-neural-network/ <p style="color: grey"> C++ recipe 580688 by <a href="/recipes/users/4194368/">Jay baller</a> (<a href="/recipes/tags/ai/">ai</a>, <a href="/recipes/tags/algorithm/">algorithm</a>, <a href="/recipes/tags/algorithms/">algorithms</a>, <a href="/recipes/tags/artificial_intelligence/">artificial_intelligence</a>, <a href="/recipes/tags/neural_network/">neural_network</a>). </p> <p>(Discrete (Binary)) Hopfield Artificial Neural Network (ANN).</p> <p>For more info see:</p> <p><a href="http://en.wikipedia.org/wiki/Hopfield_net" rel="nofollow">http://en.wikipedia.org/wiki/Hopfield_net</a></p> <p><a href="http://www.scholarpedia.org/article/Hopfield_network" rel="nofollow">http://www.scholarpedia.org/article/Hopfield_network</a></p> MicroXml: Stand-alone library for basic XML features (C++) 2016-02-18T19:33:07-08:00Jack Trainorhttp://code.activestate.com/recipes/users/4076953/http://code.activestate.com/recipes/580612-microxml-stand-alone-library-for-basic-xml-feature/ <p style="color: grey"> C++ recipe 580612 by <a href="/recipes/users/4076953/">Jack Trainor</a> (<a href="/recipes/tags/parsing/">parsing</a>, <a href="/recipes/tags/xml/">xml</a>). </p> <p>MicroXml provides stand-alone support for the basic, most-used features of XML -- tags, attributes, and element values. It produces a DOM tree of XML nodes. MicroXml does not support DTDs, CDATAs and other advanced XML features.</p> <p>MicroXml is easy to use and provides easy access to view/navigate its nodes in a debugger.</p> Boost::Python callback triggered from Non-Python created threads (C++) 2014-11-20T20:00:27-08:00Tomáš Rampashttp://code.activestate.com/recipes/users/4179816/http://code.activestate.com/recipes/578967-boostpython-callback-triggered-from-non-python-cre/ <p style="color: grey"> C++ recipe 578967 by <a href="/recipes/users/4179816/">Tomáš Rampas</a> (<a href="/recipes/tags/boost/">boost</a>, <a href="/recipes/tags/python/">python</a>). </p> <p>Consider we have virtual/abstract C++ class that's fully implemented in Python. And for some sake of necessity we have a callback method (e.g. as some sort of event) that is being triggered from different thread on C++ side. In such case corresponding callback methods have to manage GIL state via PyGILState_STATE. So the resulting C++ callback class definition will look like follows (notice that Python method calls are wrapped up with GIL state handling code).</p> Numbers as Ranges - iterable integers (C++) 2014-10-02T12:13:52-07:00elazarhttp://code.activestate.com/recipes/users/4187847/http://code.activestate.com/recipes/578945-numbers-as-ranges-iterable-integers/ <p style="color: grey"> C++ recipe 578945 by <a href="/recipes/users/4187847/">elazar</a> (<a href="/recipes/tags/iterable/">iterable</a>, <a href="/recipes/tags/range/">range</a>). Revision 2. </p> <p>This header file makes simple integers iterable. Note that it works only on C++11 or above.</p> <p>Usage:</p> <pre class="prettyprint"><code>#include "num_range.h" for (int i : 3) cout &lt;&lt; i &lt;&lt; endl; </code></pre> <p>Output: 0 1 2</p> <p>Implementation note: This code is far too generic. We only need <code>DerefableInt</code>. The templates are there for no practical reason.</p> <p>Cons: pollutes namespace std; nonstandard idiom;</p> Tri-nary Tree: insertion and deletion (C++) 2014-02-07T04:21:50-08:00Chenhttp://code.activestate.com/recipes/users/4189170/http://code.activestate.com/recipes/578825-tri-nary-tree-insertion-and-deletion/ <p style="color: grey"> C++ recipe 578825 by <a href="/recipes/users/4189170/">Chen</a> (<a href="/recipes/tags/tree/">tree</a>). </p> <p>This contains the insertion and deletion implement for the tri-nary tree data structure.</p> Elementary regex without third party libraries (C++) 2013-07-13T12:58:14-07:00greg zakharovhttp://code.activestate.com/recipes/users/4184115/http://code.activestate.com/recipes/578602-elementary-regex-without-third-party-libraries/ <p style="color: grey"> C++ recipe 578602 by <a href="/recipes/users/4184115/">greg zakharov</a> (<a href="/recipes/tags/regex/">regex</a>, <a href="/recipes/tags/wininet/">wininet</a>). </p> <p>This is a dirty trick how to access primitive regular expression on Windows.</p> A-star Shortest Path Algorithm (C++) 2010-12-25T23:36:35-08:00FB36http://code.activestate.com/recipes/users/4172570/http://code.activestate.com/recipes/577457-a-star-shortest-path-algorithm/ <p style="color: grey"> C++ recipe 577457 by <a href="/recipes/users/4172570/">FB36</a> (<a href="/recipes/tags/algorithm/">algorithm</a>, <a href="/recipes/tags/algorithms/">algorithms</a>, <a href="/recipes/tags/game/">game</a>, <a href="/recipes/tags/graph/">graph</a>, <a href="/recipes/tags/graphs/">graphs</a>, <a href="/recipes/tags/routes/">routes</a>). Revision 4. </p> <p>A-star (A*) is a shortest path algorithm widely used for RTS games, GPS navigation etc.</p> Automatic Python PyObject ref-count management in C++ using a smart ptr (C++) 2011-12-19T15:24:03-08:00samhhttp://code.activestate.com/recipes/users/4180289/http://code.activestate.com/recipes/577985-automatic-python-pyobject-ref-count-management-in-/ <p style="color: grey"> C++ recipe 577985 by <a href="/recipes/users/4180289/">samh</a> (<a href="/recipes/tags/extending/">extending</a>). </p> <p>Managing ref-counting is a complex and error prone business. If you choose C++ to extend or embed Python, you can simply use a modification on <code>std::auto_ptr</code>. Instead of calling delete on the managed pointer, it will decref it.</p> <p>So now you can do:</p> <pre class="prettyprint"><code>auto_pyptr pyHelloStr(PyStr_FromString("Hello World!")); </code></pre> <p>and forget about having to decref it altogether! Just like <code>auto_ptr</code> you can get the <code>PyObject *</code> with <code>get()</code>, release it from managed control with <code>release()</code>, and rebind it with <code>reset(new_ptr)</code>. You can also incref it by calling <code>inc()</code>, but be cautious as you can easily create a leak by increfing once to much.</p> Even faster prime generator (C++) 2011-11-27T21:48:25-08:00Sumudu Fernandohttp://code.activestate.com/recipes/users/4180103/http://code.activestate.com/recipes/577966-even-faster-prime-generator/ <p style="color: grey"> C++ recipe 577966 by <a href="/recipes/users/4180103/">Sumudu Fernando</a> (<a href="/recipes/tags/primes/">primes</a>, <a href="/recipes/tags/prime_generator/">prime_generator</a>). </p> <p>A very quick (segmented) sieve of Eratosthenes.</p> <p>Takes ~6s on a midrange machine to hit all 50 847 534 primes less than 1 billion, ending with 999999937</p> <p>If you want to actually <em>do</em> anything with every prime (beyond counting them), there are three places to add a statement doing whatever is necessary with "lastP" -- one at the top (handles the special case '2'), one in the middle (handles "small" primes which are actually used to sieve), and one at the bottom (handles "large" primes which merely survive sieving).</p> <p>In principle one can use a function object as parameter to allow generic operations on the primes, so add that if you want more general-purpose code (perhaps I'll do that later)</p> <p>For higher limits you need to switch to wider types and follow the commented guidelines for the constants. For a fixed limit, changing <code>B_SIZE</code> may affect performance so if needed tune it (profile as you go, of course!). But this will get quite slow if you go to much higher numbers.</p> Faster prime generator (C++) 2011-10-08T23:26:24-07:00Mathijs Romanshttp://code.activestate.com/recipes/users/4179530/http://code.activestate.com/recipes/577899-faster-prime-generator/ <p style="color: grey"> C++ recipe 577899 by <a href="/recipes/users/4179530/">Mathijs Romans</a> (<a href="/recipes/tags/primes/">primes</a>, <a href="/recipes/tags/prime_generator/">prime_generator</a>). </p> <p>The sieve of Eratosthenes implemented in C++. I noticed <a href="http://code.activestate.com/recipes/576559-fast-prime-generator/">another recipe</a> that could be improved upon, making it faster and a bit prettier.</p> Binary Search Tree (C++) 2011-08-08T00:13:44-07:00sivarama sarmahttp://code.activestate.com/recipes/users/4178890/http://code.activestate.com/recipes/577828-binary-search-tree/ <p style="color: grey"> C++ recipe 577828 by <a href="/recipes/users/4178890/">sivarama sarma</a> (<a href="/recipes/tags/algorithm/">algorithm</a>, <a href="/recipes/tags/algorithms/">algorithms</a>, <a href="/recipes/tags/datastructures/">datastructures</a>). </p> <p>Binary Search Tree.</p> Volume Slicer (C++) 2011-08-19T01:39:03-07:00Zahari Dichevhttp://code.activestate.com/recipes/users/4179021/http://code.activestate.com/recipes/577849-volume-slicer/ <p style="color: grey"> C++ recipe 577849 by <a href="/recipes/users/4179021/">Zahari Dichev</a> . </p> <p>This code reads volume data from a file and puts it into a scalar data structure for further use , it can also save out different slices as png files</p> Binary Search Tree (C++) 2011-01-26T22:48:06-08:00FB36http://code.activestate.com/recipes/users/4172570/http://code.activestate.com/recipes/577552-binary-search-tree/ <p style="color: grey"> C++ recipe 577552 by <a href="/recipes/users/4172570/">FB36</a> (<a href="/recipes/tags/algorithm/">algorithm</a>, <a href="/recipes/tags/algorithms/">algorithms</a>, <a href="/recipes/tags/datastructures/">datastructures</a>). </p> <p>Binary Search Tree.</p> Huffman Data Compression (C++) 2010-12-01T02:47:09-08:00FB36http://code.activestate.com/recipes/users/4172570/http://code.activestate.com/recipes/577480-huffman-data-compression/ <p style="color: grey"> C++ recipe 577480 by <a href="/recipes/users/4172570/">FB36</a> (<a href="/recipes/tags/algorithm/">algorithm</a>, <a href="/recipes/tags/algorithms/">algorithms</a>, <a href="/recipes/tags/datastructures/">datastructures</a>, <a href="/recipes/tags/data_compression/">data_compression</a>). Revision 2. </p> <p>Huffman encoding (data compression). Usage: huffman -i [input file name] -o [output file name] [-e|d]</p> Hopfield Artificial Neural Network (C++) 2010-11-14T06:32:53-08:00FB36http://code.activestate.com/recipes/users/4172570/http://code.activestate.com/recipes/577461-hopfield-artificial-neural-network/ <p style="color: grey"> C++ recipe 577461 by <a href="/recipes/users/4172570/">FB36</a> (<a href="/recipes/tags/ai/">ai</a>, <a href="/recipes/tags/algorithm/">algorithm</a>, <a href="/recipes/tags/algorithms/">algorithms</a>, <a href="/recipes/tags/artificial_intelligence/">artificial_intelligence</a>, <a href="/recipes/tags/neural_network/">neural_network</a>). Revision 2. </p> <p>(Discrete (Binary)) Hopfield Artificial Neural Network (ANN).</p> <p>For more info see:</p> <p><a href="http://en.wikipedia.org/wiki/Hopfield_net" rel="nofollow">http://en.wikipedia.org/wiki/Hopfield_net</a></p> <p><a href="http://www.scholarpedia.org/article/Hopfield_network" rel="nofollow">http://www.scholarpedia.org/article/Hopfield_network</a></p> File Uniter (C++) 2010-12-01T03:10:13-08:00FB36http://code.activestate.com/recipes/users/4172570/http://code.activestate.com/recipes/577481-file-uniter/ <p style="color: grey"> C++ recipe 577481 by <a href="/recipes/users/4172570/">FB36</a> (<a href="/recipes/tags/data_compression/">data_compression</a>). </p> <p>Unites N files into a single file (or separates them back).</p> <p>This utility can be used together w/ my other post titled "Huffman Data Compression" to enable compression of multiple files.</p> Karatsuba algorithm (C++) 2009-07-02T14:23:17-07:00Guillermo Lópezhttp://code.activestate.com/recipes/users/4170956/http://code.activestate.com/recipes/576827-karatsuba-algorithm/ <p style="color: grey"> C++ recipe 576827 by <a href="/recipes/users/4170956/">Guillermo López</a> (<a href="/recipes/tags/integers/">integers</a>, <a href="/recipes/tags/karatsuba/">karatsuba</a>, <a href="/recipes/tags/multiply/">multiply</a>). </p> <p>Used to multiply big integers (visible difference with &gt;400bits).</p> Monitor standby (C++) 2009-05-14T02:23:53-07:00kjahdksadsahd ahdsahdandhttp://code.activestate.com/recipes/users/4170243/http://code.activestate.com/recipes/576752-monitor-standby/ <p style="color: grey"> C++ recipe 576752 by <a href="/recipes/users/4170243/">kjahdksadsahd ahdsahdand</a> (<a href="/recipes/tags/monitor/">monitor</a>, <a href="/recipes/tags/on_off/">on_off</a>). </p> <p>Monitor On/Off</p> Fast prime generator (C++) 2008-11-30T00:40:23-08:00Florian Mayerhttp://code.activestate.com/recipes/users/4165843/http://code.activestate.com/recipes/576559-fast-prime-generator/ <p style="color: grey"> C++ recipe 576559 by <a href="/recipes/users/4165843/">Florian Mayer</a> (<a href="/recipes/tags/primes/">primes</a>, <a href="/recipes/tags/prime_generator/">prime_generator</a>). Revision 2. </p> <p>This is the sieve of Eratosthenes implemented in C++.</p>