{"id":43,"date":"2021-05-24T17:18:31","date_gmt":"2021-05-24T17:18:31","guid":{"rendered":"https:\/\/blog.wordgeeks.net\/?page_id=43"},"modified":"2026-01-19T02:39:49","modified_gmt":"2026-01-19T02:39:49","slug":"programming-traps","status":"publish","type":"page","link":"https:\/\/blog.wordgeeks.net\/?page_id=43","title":{"rendered":"Programming Traps"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\"><code>partial_sum<\/code> and overflow in C++<\/h3>\n\n\n\n<p>If we have a <code>vector&lt;int&gt; a<\/code> and <code>vector&lt;int64_t&gt; b<\/code>, doing <code>partial_sum(a.begin(), a.end(), b.begin())<\/code> WILL cause potential overflow, because <a href=\"https:\/\/www.cplusplus.com\/reference\/numeric\/partial_sum\/\">the implementation<\/a> is maintaining a running sum of type <code>typename iterator_traits::value_type<\/code> (<code>int<\/code> in this case). Similarly, be careful of <a href=\"https:\/\/www.cplusplus.com\/reference\/numeric\/accumulate\/\">the initial value in <code>accumulate<\/code><\/a>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Initialization<\/h3>\n\n\n\n<p>I vaguely knew that you can use curly braces when invoking constructors in C++, but never dived into it.<\/p>\n\n\n\n<p>Things like <code>vector&lt;int&gt; a{3, 2};<\/code> contradict my intuition. Instead of creating a vector of 3 elements with the value of 2, it creates a vector of 2 elements, with value 3 and 2. That&#8217;s called <a href=\"https:\/\/en.cppreference.com\/w\/cpp\/language\/initialization\">list initialization<\/a> (see <a href=\"https:\/\/en.cppreference.com\/w\/cpp\/container\/vector\/vector\">constructor 10 for vector<\/a>). You can compare <a href=\"https:\/\/gcc.godbolt.org\/z\/j13hfocv8\">https:\/\/gcc.godbolt.org\/z\/j13hfocv8<\/a> and <a href=\"https:\/\/gcc.godbolt.org\/z\/1svqMGzcY\">https:\/\/gcc.godbolt.org\/z\/1svqMGzcY<\/a>, and pay attention to the actual constructor invoked. The same applies to <a href=\"https:\/\/en.cppreference.com\/w\/cpp\/string\/basic_string\/basic_string\">string<\/a> (<a href=\"https:\/\/gcc.godbolt.org\/z\/ed1sjKPMb\">https:\/\/gcc.godbolt.org\/z\/ed1sjKPMb<\/a>).<\/p>\n\n\n\n<p>Perhaps the most interesting part is, if a constructor with initializer list is missing, curly braces will <a href=\"https:\/\/gcc.godbolt.org\/z\/8Y99xsdY9\">match to other constructors<\/a>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Tuple Assignment in Python<\/h3>\n\n\n\n<p>When you write <code>a, b, c = x, y, z<\/code> in Python, the <strong>assignment<\/strong> happens from <strong>left to right<\/strong>.<\/p>\n\n\n\n<p>Most of the time, this won&#8217;t be a problem. Until I was dealing with some Linked List problem, and tried to reverse the list as I go. What I wrote was <code>prev, curr, curr.next = curr, curr.next, prev<\/code> (the <code>curr<\/code> moves to the next node BEFORE we update <code>curr.next<\/code>).<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">endl in C++<\/h3>\n\n\n\n<p>From <a href=\"https:\/\/en.cppreference.com\/w\/cpp\/io\/manip\/endl\">cppreference<\/a>:<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>Inserts a newline character into the output sequence <code>os<\/code> and flushes it as if by calling <code>os.put(os.widen('\\n'))<\/code> followed by <code>os.flush()<\/code>.&#8221;<\/p>\n<\/blockquote>\n\n\n\n<p>When a problem requires large amounts of output, the repeated flush can lead to slow execution.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">multiset in C++<\/h3>\n\n\n\n<p>The <code>erase<\/code> function <a href=\"https:\/\/en.cppreference.com\/w\/cpp\/container\/multiset\/erase.html\">removes <strong>all<\/strong> elements with the key equivalent to the given parameter if it&#8217;s of <code>Key<\/code> (const reference or r-value reference)<\/a>.<\/p>\n\n\n\n<p>To remove a single occurrence, <a href=\"https:\/\/stackoverflow.com\/questions\/9167745\/in-stdmultiset-is-there-a-function-or-algorithm-to-erase-just-one-sample-unic\">use an iterator<\/a>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Emplace with nested vector<\/h3>\n\n\n\n<p>What is wrong with this code:<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:c++ decode:true \" title=\"emplace\" >void f() {\n  int n;\n  cin &gt;&gt; n;\n  vector&lt;vector&lt;int&gt;&gt; vs(n);\n  for (int i = 0; i &lt; n; ++i) {\n    int m;\n    cin &gt;&gt; m;\n    vs.reserve(m);\n    for (int j = 0; j &lt; m; ++j) {\n      int k;\n      cin &gt;&gt; k;\n      vs.emplace_back(some_func_that_returns_int(k));\n    }\n  }\n}<\/pre><\/div>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>partial_sum and overflow in C++ If we have a vector&lt;int&gt; a and vector&lt;int64_t&gt; b, doing partial_sum(a.begin(), a.end(), b.begin()) WILL cause potential overflow, because the implementation is maintaining a running sum of type typename iterator_traits::value_type (int in this case). Similarly, be careful of the initial value in accumulate. Initialization I vaguely knew that you can use&hellip; <a class=\"more-link\" href=\"https:\/\/blog.wordgeeks.net\/?page_id=43\">Continue reading <span class=\"screen-reader-text\">Programming Traps<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":1,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-43","page","type-page","status-publish","hentry","entry"],"_links":{"self":[{"href":"https:\/\/blog.wordgeeks.net\/index.php?rest_route=\/wp\/v2\/pages\/43","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.wordgeeks.net\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/blog.wordgeeks.net\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/blog.wordgeeks.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.wordgeeks.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=43"}],"version-history":[{"count":8,"href":"https:\/\/blog.wordgeeks.net\/index.php?rest_route=\/wp\/v2\/pages\/43\/revisions"}],"predecessor-version":[{"id":274,"href":"https:\/\/blog.wordgeeks.net\/index.php?rest_route=\/wp\/v2\/pages\/43\/revisions\/274"}],"wp:attachment":[{"href":"https:\/\/blog.wordgeeks.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=43"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}