<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/"><channel><title>C++ on Lin's Blog</title><link>https://coder.tw/en/tags/c++/</link><description>Recent content in C++ on Lin's Blog</description><generator>Hugo</generator><language>en-US</language><lastBuildDate>Sat, 03 Sep 2011 11:03:57 +0000</lastBuildDate><atom:link href="https://coder.tw/en/tags/c++/index.xml" rel="self" type="application/rss+xml"/><item><title>Fixing Chinese Garbled Text in Code::Blocks</title><link>https://coder.tw/en/posts/fix-codeblocks-chinese-encoding/</link><pubDate>Sat, 03 Sep 2011 11:03:57 +0000</pubDate><guid>https://coder.tw/en/posts/fix-codeblocks-chinese-encoding/</guid><description>&lt;p&gt;Code::Blocks is a C/C++ IDE that I personally like to use, but I kept running into one problem: with Code::Blocks, I couldn&amp;rsquo;t use Chinese at all. Strings couldn&amp;rsquo;t contain Chinese, and even comments couldn&amp;rsquo;t. I originally thought it was a bug in the program itself, but a fellow user reminded me that it was actually a default encoding issue!&lt;/p&gt;</description></item><item><title>[C++] Sorting with the STL's Built-in sort() Function!</title><link>https://coder.tw/en/posts/c-stl-sort-function/</link><pubDate>Thu, 25 Aug 2011 10:30:09 +0000</pubDate><guid>https://coder.tw/en/posts/c-stl-sort-function/</guid><description>&lt;p&gt;&lt;a href="https://pub-6c026d2742d14927bfa2d850c31eac91.r2.dev/wp-content/uploads/2011/08/2011-08-25_102642.jpg"&gt;&lt;img class="alignnone size-large wp-image-724" title="2011-08-25_102642" src="https://pub-6c026d2742d14927bfa2d850c31eac91.r2.dev/wp-content/uploads/2011/08/2011-08-25_102642-540x352.jpg" alt="" width="540" height="352" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Sorting is something everyone who learns programming has to master, because a lot of data processing depends on it. Many programming languages include sorting functions built in, so developers can use them easily. But I don&amp;rsquo;t think many people know that C++ also has a built-in sorting function!&lt;/p&gt;</description></item><item><title>1A2B Code Explained</title><link>https://coder.tw/en/posts/1a2b-code-explained/</link><pubDate>Fri, 08 Jul 2011 18:00:28 +0000</pubDate><guid>https://coder.tw/en/posts/1a2b-code-explained/</guid><description>&lt;p&gt;&lt;a href="https://pub-6c026d2742d14927bfa2d850c31eac91.r2.dev/wp-content/uploads/2011/07/2011-07-08_175731.jpg"&gt;&lt;img class="alignnone size-large wp-image-550" title="2011-07-08_175731" src="https://pub-6c026d2742d14927bfa2d850c31eac91.r2.dev/wp-content/uploads/2011/07/2011-07-08_175731-540x352.jpg" alt="" width="540" height="352" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;This was an assignment from my teacher: design a 1A2B guessing game. Before this class, I always thought 1A2B was hard to write. After the class, I realized it wasn&amp;rsquo;t hard at all. I was just too much of a homebody and didn&amp;rsquo;t even know the rules of 1A2B&amp;hellip;.&lt;/p&gt;</description></item><item><title>Shuffle Algorithm (Swap Method)</title><link>https://coder.tw/en/posts/shuffle-algorithm-swap-method/</link><pubDate>Sun, 05 Jun 2011 01:00:07 +0000</pubDate><guid>https://coder.tw/en/posts/shuffle-algorithm-swap-method/</guid><description>&lt;p&gt;&lt;a href="https://pub-6c026d2742d14927bfa2d850c31eac91.r2.dev/wp-content/uploads/2011/06/2011-06-05_005920.jpg"&gt;&lt;img class="size-large wp-image-534 aligncenter" title="2011-06-05_005920" src="https://pub-6c026d2742d14927bfa2d850c31eac91.r2.dev/wp-content/uploads/2011/06/2011-06-05_005920-540x352.jpg" alt="" width="540" height="352" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;The shuffle algorithm is a way to generate random numbers that never repeat. For example, suppose you need to generate ten random numbers, and all ten must be &amp;ldquo;unique.&amp;rdquo; Many people&amp;rsquo;s first instinct is to call the rand function to get a random value, store it in an array, and compare it against the numbers already in the array. That method is simple, but its performance is very poor. The shuffle algorithm introduced here is worlds apart from that approach in terms of performance. The principle behind the shuffle algorithm is actually very simple: first store the numbers in an array, then scramble them.&lt;/p&gt;</description></item><item><title>wxDevC++ - The C++ IDE Every Dev C++ Fan Needs to Know About</title><link>https://coder.tw/en/posts/wxdevc-c-ide-for-devc-lovers/</link><pubDate>Wed, 18 May 2011 00:22:47 +0000</pubDate><guid>https://coder.tw/en/posts/wxdevc-c-ide-for-devc-lovers/</guid><description>&lt;p&gt;&lt;a href="https://pub-6c026d2742d14927bfa2d850c31eac91.r2.dev/wp-content/uploads/2011/05/2011-05-18_000748.jpg"&gt;&lt;img class="size-full wp-image-479 aligncenter" title="2011-05-18_000748" src="https://pub-6c026d2742d14927bfa2d850c31eac91.r2.dev/wp-content/uploads/2011/05/2011-05-18_000748.jpg" alt="" width="471" height="292" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;A lot of people who learn C++ actually start with Dev C++. VC++ is convenient for beginners, sure, but you can&amp;rsquo;t learn standard C (ANSI C) with it, because VC contains far too many functions that belong to .NET — compile that code with another compiler and you&amp;rsquo;ll run into trouble. So beginners generally go with Dev C++ or Code::Blocks. Unfortunately, Dev C++ hasn&amp;rsquo;t been updated for several years, and it no longer supports many newer features. Code::Blocks, on the other hand, has poor Chinese support: if there&amp;rsquo;s Chinese anywhere in your code you&amp;rsquo;ll run into problems, and that includes not being able to use Chinese in comments. The one I want to recommend here, wxDev C++, actually carries on Dev C++&amp;rsquo;s characteristics while adding some new features, unlike Dev C++ which has no end of problems&amp;hellip; So if you&amp;rsquo;re comfortable with Dev C++, I&amp;rsquo;d suggest giving wxDev C++ a try, because it&amp;rsquo;s almost identical to Dev C++! As for me, I&amp;rsquo;ll just keep using my Code::blocks～&lt;/p&gt;
&lt;h2&gt;</description></item><item><title>How to Convert a Decimal Number to Binary?</title><link>https://coder.tw/en/posts/decimal-to-binary-conversion/</link><pubDate>Sat, 16 Apr 2011 19:38:38 +0000</pubDate><guid>https://coder.tw/en/posts/decimal-to-binary-conversion/</guid><description>&lt;p&gt;&lt;a href="https://pub-6c026d2742d14927bfa2d850c31eac91.r2.dev/wp-content/uploads/2011/04/2011-04-16_185412.jpg"&gt;&lt;img title="2011-04-16_185412" src="https://pub-6c026d2742d14927bfa2d850c31eac91.r2.dev/wp-content/uploads/2011/04/2011-04-16_185412.jpg" alt="" width="205" height="237" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;del&gt;I expect someone will say this article is too simple again once I finish writing it...&lt;/del&gt; Without further ado: this week, our teacher gave our class a problem, which is how to convert a decimal number to binary. If you do it with math, it isn't hard. Just use short division and take the remainders.&lt;/p&gt;
&lt;p&gt;</description></item><item><title>Printing Triangles: Top-Left, Bottom-Left, Top-Right, Bottom-Right</title><link>https://coder.tw/en/posts/loop-practice-print-triangle/</link><pubDate>Mon, 21 Mar 2011 02:46:28 +0000</pubDate><guid>https://coder.tw/en/posts/loop-practice-print-triangle/</guid><description>&lt;p&gt;&lt;a href="https://pub-6c026d2742d14927bfa2d850c31eac91.r2.dev/wp-content/uploads/2011/03/2011-03-21_024015.jpg"&gt;&lt;img class="size-large wp-image-379 aligncenter" title="2011-03-21_024015" src="https://pub-6c026d2742d14927bfa2d850c31eac91.r2.dev/wp-content/uploads/2011/03/2011-03-21_024015-540x492.jpg" alt="" width="540" height="492" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;These were some examples my programming teacher used when teaching loops in class. I think the problems themselves aren't hard, but they're great for practicing loops and training your reaction speed. Also, my VB skills have gotten a lot rusty... So I'm attaching the C++ code for everyone to look at. Go study it yourselves～&lt;/p&gt;
&lt;p&gt;</description></item><item><title>[C++] How to Do Exponentiation and Square Roots (sqrt, pow)</title><link>https://coder.tw/en/posts/c-power-and-square-root-sqrt-pow/</link><pubDate>Fri, 18 Mar 2011 15:05:05 +0000</pubDate><guid>https://coder.tw/en/posts/c-power-and-square-root-sqrt-pow/</guid><description>&lt;p&gt;&lt;a href="https://pub-6c026d2742d14927bfa2d850c31eac91.r2.dev/wp-content/uploads/2011/03/2011-03-18_151805.jpg"&gt;&lt;img class="alignnone size-large wp-image-367" title="2011-03-18_151805" src="https://pub-6c026d2742d14927bfa2d850c31eac91.r2.dev/wp-content/uploads/2011/03/2011-03-18_151805-540x380.jpg" alt="" width="540" height="380" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;In some programming languages, you can do exponentiation directly with the ^ operator. But while working on a problem, I found out that in C++, you have to use the pow function to do exponentiation. This article introduces two functions: pow, which does exponentiation for you, and sqrt, which takes square roots.&lt;/p&gt;</description></item><item><title>[C++] Reversing Numbers and Strings (Applications: Type Conversion, String Handling, Loops)</title><link>https://coder.tw/en/posts/c-reverse-number-and-string/</link><pubDate>Sun, 06 Mar 2011 11:18:22 +0000</pubDate><guid>https://coder.tw/en/posts/c-reverse-number-and-string/</guid><description>&lt;p&gt;&lt;a href="https://pub-6c026d2742d14927bfa2d850c31eac91.r2.dev/wp-content/uploads/2011/03/2011-03-06_111510.jpg"&gt;&lt;img class="size-large wp-image-342 aligncenter" title="2011-03-06_111510" src="https://pub-6c026d2742d14927bfa2d850c31eac91.r2.dev/wp-content/uploads/2011/03/2011-03-06_111510-540x382.jpg" alt="" width="540" height="382" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;This is a fairly simple problem: it just reverses a number or a string. For example, input "linroex" and the output is "xeornil", or input 123 and get 321. The solution isn't hard. In C++, you can access a string like an array, so we set up a loop whose end value is the length of the input string. On each pass, we move the last remaining character of string X to the first position of string Y. When the loop finishes, the reversal is complete.&lt;/p&gt;
&lt;p&gt;</description></item><item><title>[PHP] [C++] A Concise Syntax for Explicit Type Casting</title><link>https://coder.tw/en/posts/php-c-explicit-type-cast/</link><pubDate>Tue, 15 Feb 2011 22:57:49 +0000</pubDate><guid>https://coder.tw/en/posts/php-c-explicit-type-cast/</guid><description>&lt;p style="text-align: center;"&gt;&lt;a href="https://pub-6c026d2742d14927bfa2d850c31eac91.r2.dev/wp-content/uploads/2011/02/2011-02-15_225730.jpg"&gt;&lt;img class="size-large wp-image-282 aligncenter" title="2011-02-15_225730" src="https://pub-6c026d2742d14927bfa2d850c31eac91.r2.dev/wp-content/uploads/2011/02/2011-02-15_225730-540x352.jpg" alt="" width="540" height="352" /&gt;&lt;/a&gt;&lt;/p&gt;
PHP and C++ have a very convenient way to cast types. You don't have to memorize a long list of conversion functions. You just put the target type in parentheses in front of the value. However, this approach seems to have some problems, though I'm not really sure what they are XD</description></item><item><title>Code::Blocks 10.05 - A C/C++ Compiler That Rivals Dev C++, Cross-Platform (Ubuntu, Windows, Mac)</title><link>https://coder.tw/en/posts/codeblocks-cross-platform-c-compiler/</link><pubDate>Fri, 11 Feb 2011 10:42:29 +0000</pubDate><guid>https://coder.tw/en/posts/codeblocks-cross-platform-c-compiler/</guid><description>&lt;p style="text-align: center;"&gt;&lt;a href="https://pub-6c026d2742d14927bfa2d850c31eac91.r2.dev/wp-content/uploads/2011/02/2010-10-28_182355.jpg"&gt;&lt;img class="alignnone size-full wp-image-191" title="2010-10-28_182355" src="https://pub-6c026d2742d14927bfa2d850c31eac91.r2.dev/wp-content/uploads/2011/02/2010-10-28_182355.jpg" alt="" width="440" height="217" /&gt;&lt;/a&gt;&lt;/p&gt;
I'm sure you know the Dev C++ compiler. Many schools use it as the platform for teaching C. For example, the computer classes at Taiwan's top high schools seem to use it. But here I want to introduce not Dev C++, but &lt;strong&gt;&lt;a href="http://www.codeblocks.org/"&gt;Code::Blocks&lt;/a&gt;&lt;/strong&gt;. Code::Blocks and Dev C++ are both compilers for C (both C and C++). However, if you have been following Dev C++, you may have noticed that it has not been updated in a long time. &lt;strong&gt;&lt;a href="http://www.codeblocks.org/"&gt;Code::Blocks&lt;/a&gt;&lt;/strong&gt; is free and open source, just like Dev C++, but it also supports multiple operating systems, including Windows and Linux. So if you want to write C programs on Ubuntu, you can try this software.
&lt;p&gt;This tutorial is for Windows only.&lt;/p&gt;</description></item><item><title>[VB] [C++] Calculating n! (Factorial)</title><link>https://coder.tw/en/posts/vb-c-factorial-n-2/</link><pubDate>Wed, 05 Jan 2011 05:58:10 +0000</pubDate><guid>https://coder.tw/en/posts/vb-c-factorial-n-2/</guid><description>&lt;p&gt;&lt;a href="https://pub-6c026d2742d14927bfa2d850c31eac91.r2.dev/wp-content/uploads/2011/01/2011-01-05_054100.jpg"&gt;&lt;img class="alignnone size-large wp-image-4369" title="2011-01-05_054100" src="https://pub-6c026d2742d14927bfa2d850c31eac91.r2.dev/wp-content/uploads/2011/01/2011-01-05_054100-520x253.jpg" alt="" width="520" height="253" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Calculating the factorial of n is very simple. I wrote this program for permutations and combinations: I wanted a rough idea of how many possible answers there would be, so I wrote an n! function to compute the factorial value. A factorial is 1&lt;em&gt;2&lt;/em&gt;3&amp;hellip;..&lt;em&gt;n. In a permutation of 5 items, the result is at most 5&lt;/em&gt;4&lt;em&gt;3&lt;/em&gt;2*1, which means there are at most 120 possible answers.&lt;/p&gt;
&lt;p&gt;Below, I&amp;rsquo;ve made n! into a Function, with a VB version and a C++ version. People who are just starting to learn loops can use this function to practice how loops work. Also, because I was worried that an integer wouldn&amp;rsquo;t be able to hold the result, I used a string to return the value. When you use this Function, you need to convert the return value to a numeric type first.&lt;/p&gt;
&lt;h2&gt;</description></item><item><title>[C++] Sending a File to a Console Application via CMD and Returning Data</title><link>https://coder.tw/en/posts/c-cmd-send-file-to-console-app-2/</link><pubDate>Sat, 04 Dec 2010 10:00:16 +0000</pubDate><guid>https://coder.tw/en/posts/c-cmd-send-file-to-console-app-2/</guid><description>&lt;p style="text-align: center;"&gt;&lt;a href="https://pub-6c026d2742d14927bfa2d850c31eac91.r2.dev/wp-content/uploads/2010/11/2010-11-26_232836.jpg"&gt;&lt;img class="size-large wp-image-4097 aligncenter" title="2010-11-26_232836" src="https://pub-6c026d2742d14927bfa2d850c31eac91.r2.dev/wp-content/uploads/2010/11/2010-11-26_232836-520x365.jpg" alt="" width="520" height="365" /&gt;&lt;/a&gt;&lt;/p&gt;
Actually, in C++ there's another way to read files besides simply using fopen, and it's much easier to implement than fopen: feeding the file to your C++ program through CMD. For example, say you have a program called test.exe and a text file called x.txt, and you want test.exe to read the data in x.txt and display it. You open CMD and type &lt;em&gt;&lt;strong&gt;test.exe&amp;lt;x.txt&lt;/strong&gt;&lt;/em&gt;, and it reads and displays the contents of x.txt. This article teaches you this way of reading files. Thanks also to a reader named Canghai, who taught me this technique.</description></item></channel></rss>