{"id":79,"date":"2018-12-24T22:27:07","date_gmt":"2018-12-25T01:27:07","guid":{"rendered":"http:\/\/35.243.195.209\/?p=79"},"modified":"2019-03-08T23:47:50","modified_gmt":"2019-03-09T07:47:50","slug":"easy-to-make-mistakes-in-c","status":"publish","type":"post","link":"https:\/\/nanzhou.cc\/index.php\/2018\/12\/24\/easy-to-make-mistakes-in-c\/","title":{"rendered":"Common Mistakes in C++"},"content":{"rendered":"<h2>Summary<\/h2>\n<p>This post will be updated from time to time. The post contains some mistakes that I will make in work and study when coding in C++.<\/p>\n<h2>Contents<\/h2>\n<ol>\n<li><a href=\"#1\">container.size() &#8211; 1 when the container is empty<\/a><\/li>\n<li><a href=\"#2\">negative % positive<\/a><\/li>\n<li><a href=\"#3\">&#8216;+&#8217; does work in &#8220;C_String&#8221;<\/a><\/li>\n<li><a href=\"#4\">remember to delete after new<\/a><\/li>\n<li><a href=\"#5\">to_string(&#8216;a&#8217;)<\/a><\/li>\n<li><a href=\"#6\">signed integers compared to unsinged integers in min or max<\/a><\/li>\n<li><a href=\"#7\">variable&#8211; when it has type size_t<\/a><\/li>\n<\/ol>\n<h2>Mistakes<\/h2>\n<h3>1. container.size() &#8211; 1 when the container is empty<\/h3>\n<p><a name=\"1\"><\/a><\/p>\n<pre><code class=\"c\">vector&lt;int&gt; nums;\n\/\/ correct version\nfor (size_t i = 0; i + 1 &lt; nums.size(); ++i) {\n    \/\/ ...\n}\n\n\/\/ wrong, nums.size() - 1 will overflow for unsigned int, which couses i &lt; (-xxx) hold forever\nfor (size_t i = 0; i &lt; nums.size() - 1; ++i) {\n    \/\/ ...\n}\n<\/code><\/pre>\n<h3>2. negative % positive<\/h3>\n<p><a name=\"2\"><\/a><\/p>\n<pre><code class=\"c\">cout &lt;&lt; -11 % 10 &lt;&lt; endl; \/\/ -1\ncout &lt;&lt; -1 % 10 &lt;&lt; endl; \/\/ -1\ncout &lt;&lt; 1 % 10 &lt;&lt; endl; \/\/ 1\n<\/code><\/pre>\n<h3>3. &#8216;+&#8217; does work in &#8220;C_String&#8221;<\/h3>\n<p><a name=\"3\"><\/a><br \/>\n&#8220;123&#8221; is a c_string in c++.<\/p>\n<pre><code class=\"c\">#include &lt;string&gt;\nstd::string wrong= \"1\" + \"1\"; \/\/ ERROR, + operator is overloaded for C++ string\nstd::string right= std::string(\"1\") + \"1\"; \/\/ 11\n\nstd::string result;\nchar a = 'a';\nresult += \"1221\" + a; \/\/ ERROR \n<\/code><\/pre>\n<h3>4. remember to delete after new<\/h3>\n<p><a name=\"4\"><\/a><br \/>\nIn projects containing only one thread, you may not meet a <code>killed<\/code>. It is different when you use multiple threads and without <code>delete<\/code> your program will be <code>killed<\/code> soon.<\/p>\n<h3>5. to_string(&#8216;a&#8217;)<\/h3>\n<p><a name=\"5\"><\/a><\/p>\n<pre><code class=\"c\">cout &lt;&lt; to_string('a') &lt;&lt; endl; \/\/ 97\ncout &lt;&lt; to_string(1, 'a') &lt;&lt; endl; \/\/ 'a' \n<\/code><\/pre>\n<h3>6. signed integers compared to unsinged integers in min or max<\/h3>\n<p><a name=\"6\"><\/a><\/p>\n<pre><code class=\"c\">vector&lt;int&gt; a {1,2,3};\nmin(a[0], a.size() - 1); \/\/ can not compile, arguments in min should have the same types\n<\/code><\/pre>\n<h3>7. variable&#8211; when it has type size_t<\/h3>\n<p><a name=\"7\"><\/a><\/p>\n<pre><code class=\"c\">vector&lt;int&gt; A {1, 2, 3};\nfor (size_t i = A.size() - 1; i &gt;= 0; --i) {\n        do_something();\n}\n<\/code><\/pre>\n<p>It is an endless loop similar to <a href=\"#1\">mistake 1<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary This post will be updated from time to time. The post contains some mistakes that I will make in work and study when coding in C++. Contents container.size() &#8211; 1 when the container is empty negative % positive &#8216;+&#8217; does work in &#8220;C_String&#8221; remember to delete after new to_string(&#8216;a&#8217;) signed integers compared to unsinged&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6,5],"tags":[],"class_list":["post-79","post","type-post","status-publish","format-standard","hentry","category-c","category-proglang"],"_links":{"self":[{"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/posts\/79","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/comments?post=79"}],"version-history":[{"count":10,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/posts\/79\/revisions"}],"predecessor-version":[{"id":343,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/posts\/79\/revisions\/343"}],"wp:attachment":[{"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/media?parent=79"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/categories?post=79"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/tags?post=79"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}