<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>C++ Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/c/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/c/</link>
	<description></description>
	<lastBuildDate>Thu, 11 Jun 2020 00:59:04 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.0.8</generator>

<image>
	<url>https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/cropped-photo-32x32.jpg</url>
	<title>C++ Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/c/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>C++ 11 Smart Pointers 智能指针</title>
		<link>https://zxi.mytechroad.com/blog/c/cpp-11-smart-pointers/</link>
					<comments>https://zxi.mytechroad.com/blog/c/cpp-11-smart-pointers/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Thu, 11 Jun 2020 00:55:32 +0000</pubDate>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[smart pointers]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=6896</guid>

					<description><![CDATA[<p>Example code [crayon-663bf46e56615866787646/] Output [crayon-663bf46e5661b759197048/]</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/c/cpp-11-smart-pointers/">C++ 11 Smart Pointers 智能指针</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<figure class="wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-4-3 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
<iframe title="Smart Pointers 智能指针 - C++ Weekly EP3" width="500" height="375" src="https://www.youtube.com/embed/KQt3IjGdqL8?feature=oembed" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
</div></figure>



<p>Example code</p>



<div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">// Author: Huahua
#include &lt;iostream&gt;
#include &lt;memory&gt;

class Entity {
public:
  Entity() { puts(&quot;Entity created!&quot;); }
  ~Entity() { puts(&quot;Entity destroyed!&quot;); }
};

void ex1() {
  puts(&quot;--------&quot;);
  puts(&quot;Entering ex1&quot;);
  {
    puts(&quot;Entering ex1::scope1&quot;);
    auto e1 = std::make_unique&lt;Entity&gt;();    
    puts(&quot;Leaving ex1::scope1&quot;);
  }
  puts(&quot;Leaving ex1&quot;);
}

void foo(std::unique_ptr&lt;Entity&gt;) {
  puts(&quot;Entering foo&quot;);
  puts(&quot;Leaving foo&quot;);
}

void ex2() {
  puts(&quot;--------&quot;);
  puts(&quot;Entering ex2&quot;);
  auto e1 = std::make_unique&lt;Entity&gt;();  
  foo(std::move(e1));
  // e1 was destoried.
  puts(&quot;Leaving ex2&quot;);
}

void ex3() {
  puts(&quot;--------&quot;);
  puts(&quot;Entering ex3&quot;);
  auto e1 = std::make_shared&lt;Entity&gt;();
  std::cout &lt;&lt; e1.use_count() &lt;&lt; std::endl;
  {
    puts(&quot;Entering ex3::scope1&quot;);
    auto e2 = e1; // use_count ++
    std::cout &lt;&lt; e1.use_count() &lt;&lt; std::endl;
    auto e3 = std::move(e2); // use_count remains
    std::cout &lt;&lt; e1.use_count() &lt;&lt; std::endl;
    puts(&quot;Leaving ex3::scope1&quot;);
  }
  std::cout &lt;&lt; e1.use_count() &lt;&lt; std::endl;
  puts(&quot;Leaving ex3&quot;);
}

void observe(std::weak_ptr&lt;Entity&gt; ew) {
  if (std::shared_ptr&lt;Entity&gt; spt = ew.lock()) {
    std::cout &lt;&lt; spt.use_count() &lt;&lt; std::endl;
    std::cout &lt;&lt; &quot;entity still alive!&quot; &lt;&lt; std::endl;
  } else {
    std::cout &lt;&lt; &quot;entity was expired :(&quot; &lt;&lt; std::endl;
  }
}

void ex4() {
  puts(&quot;--------&quot;);
  puts(&quot;Entering ex4&quot;);
  std::weak_ptr&lt;Entity&gt; ew;  
  {
    puts(&quot;Entering ex4::scope1&quot;);
    auto e1 = std::make_shared&lt;Entity&gt;();
    std::cout &lt;&lt; e1.use_count() &lt;&lt; std::endl;
    ew = e1; // use_count remains
    std::cout &lt;&lt; e1.use_count() &lt;&lt; std::endl;
    observe(ew);
    puts(&quot;Leaving ex4::scope1&quot;);
  }
  observe(ew);
  puts(&quot;Leaving ex4&quot;);
}

int main(int argc, char** argv) {
  ex1();
  ex2();
  ex3();
  ex4();
  return 0;
}</pre>
</div></div>



<h2>Output</h2>



<pre class="crayon-plain-tag">--------
Entering ex1
Entering ex1::scope1
Entity created!
Leaving ex1::scope1
Entity destroyed!
Leaving ex1
--------
Entering ex2
Entity created!
Entering foo
Leaving foo
Entity destroyed!
Leaving ex2
--------
Entering ex3
Entity created!
1
Entering ex3::scope1
2
2
Leaving ex3::scope1
1
Leaving ex3
Entity destroyed!
--------
Entering ex4
Entering ex4::scope1
Entity created!
1
1
2
entity still alive!
Leaving ex4::scope1
Entity destroyed!
entity was expired :(
Leaving ex4</pre>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/c/cpp-11-smart-pointers/">C++ 11 Smart Pointers 智能指针</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/c/cpp-11-smart-pointers/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Python-Like enumerate() In C++17 &#8211; C++ Weekly EP1</title>
		<link>https://zxi.mytechroad.com/blog/c/python-like-enumerate-in-c17/</link>
					<comments>https://zxi.mytechroad.com/blog/c/python-like-enumerate-in-c17/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 17 May 2020 21:34:20 +0000</pubDate>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[auto]]></category>
		<category><![CDATA[c++17]]></category>
		<category><![CDATA[constexpr]]></category>
		<category><![CDATA[enumerate]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=6774</guid>

					<description><![CDATA[<p>[crayon-663bf46e56b65967860318/]</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/c/python-like-enumerate-in-c17/">Python-Like enumerate() In C++17 &#8211; C++ Weekly EP1</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<figure class="wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-4-3 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
<iframe title="Python-Like enumerate() In C++17 - C++ Weekly EP1" width="500" height="375" src="https://www.youtube.com/embed/ENpgbTrrebo?feature=oembed" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
</div></figure>



<div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">// Author: Huahua
#include &lt;iostream&gt;
#include &lt;string&gt;
#include &lt;vector&gt;
#include &lt;list&gt;
#include &lt;tuple&gt;
using namespace std;

// Python-Like enumerate() In C++17
// http://reedbeta.com/blog/python-like-enumerate-in-cpp17/
template &lt;typename T,
          typename TIter = decltype(std::begin(std::declval&lt;T&gt;())),
          typename = decltype(std::end(std::declval&lt;T&gt;()))&gt;
constexpr auto enumerate(T &amp;&amp; iterable) {
  struct iterator {
    int i;
    TIter iter;
    bool operator != (const iterator &amp; other) const { return iter != other.iter; }
    void operator ++ () { ++i; ++iter; }
    auto operator * () const { return std::tie(i, *iter); }
  };
  struct iterable_wrapper {
    T iterable;
    auto begin() { return iterator{ 0, std::begin(iterable) }; }
    auto end() { return iterator{ 0, std::end(iterable) }; }
  };
  // return iterable_wrapper{ iterable }; // this makes a copy if iterable is a rvalue.
  return iterable_wrapper{ std::forward&lt;T&gt;(iterable) };  
}

struct A {
  int val;
  A(int val): val(val) { cout &lt;&lt; &quot;A(int)&quot; &lt;&lt; endl; }
  A(const A&amp; a): val(a.val) { cout &lt;&lt; &quot;A(A&amp;)&quot; &lt;&lt; endl; }
  A(A&amp;&amp; a): val(a.val) { cout &lt;&lt; &quot;A(A&amp;&amp;)&quot; &lt;&lt; endl; }    
};

int main(int argc, char** argv) {
  vector&lt;int&gt; arr{1, 2, 4};  
  
  for (size_t i = 0; i &lt; arr.size(); ++i)
    cout &lt;&lt; i &lt;&lt; &quot; &quot; &lt;&lt; arr[i] &lt;&lt; endl;

  size_t idx = 0;
  for (int v : arr)
    cout &lt;&lt; idx++ &lt;&lt; &quot; &quot; &lt;&lt; v &lt;&lt; endl;

  for (auto it = begin(arr); it != end(arr); ++it)
    cout &lt;&lt; distance(begin(arr), it) &lt;&lt; &quot; &quot; &lt;&lt; *it &lt;&lt; endl;
  

  for (const auto&amp; [i, v] : enumerate(arr))
    cout &lt;&lt; i &lt;&lt; &quot; &quot; &lt;&lt; v &lt;&lt; endl;

  list&lt;string&gt; lst{&quot;hello&quot;, &quot;world!&quot;};  

  for (const auto&amp; [i, v] : enumerate(lst))
    cout &lt;&lt; i &lt;&lt; &quot; &quot; &lt;&lt; v &lt;&lt; endl;

  for (const auto&amp; [i, v] : enumerate(string(&quot;abcde&quot;)))
    cout &lt;&lt; i &lt;&lt; &quot; &quot; &lt;&lt; v &lt;&lt; endl;

  vector&lt;A&gt; vec;
  vec.reserve(3);
  vec.emplace_back(1);
  vec.emplace_back(2);
  vec.emplace_back(4);

  for (const auto&amp; [i, v] : enumerate(vec))
    cout &lt;&lt; i &lt;&lt; &quot; &quot; &lt;&lt; v.val &lt;&lt; endl;

  for (const auto&amp; [i, v] : enumerate(vector&lt;A&gt;{A(1), A(2), A(4)}))
    cout &lt;&lt; i &lt;&lt; &quot; &quot; &lt;&lt; v.val &lt;&lt; endl;

  return 0;
}</pre>
</div></div>



<p></p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/c/python-like-enumerate-in-c17/">Python-Like enumerate() In C++17 &#8211; C++ Weekly EP1</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/c/python-like-enumerate-in-c17/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 669. Trim a Binary Search Tree</title>
		<link>https://zxi.mytechroad.com/blog/leetcode/leetcode-669-trim-a-binary-search-tree/</link>
					<comments>https://zxi.mytechroad.com/blog/leetcode/leetcode-669-trim-a-binary-search-tree/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Tue, 05 Sep 2017 01:13:36 +0000</pubDate>
				<category><![CDATA[Leetcode]]></category>
		<category><![CDATA[Tree]]></category>
		<category><![CDATA[binary]]></category>
		<category><![CDATA[binary search tree]]></category>
		<category><![CDATA[binary tree]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[memory leak]]></category>
		<category><![CDATA[recursion]]></category>
		<category><![CDATA[trim]]></category>
		<guid isPermaLink="false">http://zxi.mytechroad.com/blog/?p=90</guid>

					<description><![CDATA[<p>Given a binary search tree and the lowest and highest boundaries as L and R, trim the tree so that all its elements lies in [L, R] (R &#62;= L).&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/leetcode/leetcode-669-trim-a-binary-search-tree/">花花酱 LeetCode 669. Trim a Binary Search Tree</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p><iframe width="500" height="375" src="https://www.youtube.com/embed/L_t2x3nH61k?feature=oembed" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe></p>
<p>Given a binary search tree and the lowest and highest boundaries as <code>L</code> and <code>R</code>, trim the tree so that all its elements lies in <code>[L, R]</code> (R &gt;= L). You might need to change the root of the tree, so the result should return the new root of the trimmed binary search tree.</p>
<p><b>Example 1:</b></p><pre class="crayon-plain-tag">Input:
    1
   / \
  0   2

  L = 1
  R = 2

Output: 
    1
      \
       2</pre><p><b>Example 2:</b></p><pre class="crayon-plain-tag">Input:
    3
   / \
  0   4
   \
    2
   /
  1

  L = 1
  R = 3

Output:
      3
     / 
   2   
  /
 1</pre><p>This problem can be solved with recursion</p>
<p><a href="http://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/699-1-1.png"><img class="alignnone wp-image-100 size-full" src="http://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/699-1-1.png" alt="" width="960" height="540" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/699-1-1.png 960w, https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/699-1-1-300x169.png 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/699-1-1-768x432.png 768w, https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/699-1-1-624x351.png 624w" sizes="(max-width: 960px) 100vw, 960px" /></a></p>
<p>There 3 cases in total depends on the root value and L, R</p>
<p>Time complexity: O(n)</p>
<p>Space complexity: O(1)</p>
<p>Solution:</p><pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
    // No cleanup -&gt; memory leak 
    TreeNode* trimBST(TreeNode* root, int L, int R) {
        if(!root) return nullptr;
        // val not in range, return the left/right subtrees
        if(root-&gt;val &lt; L) return trimBST(root-&gt;right, L, R);
        if(root-&gt;val &gt; R) return trimBST(root-&gt;left, L, R);
        // val in [L, R], recusively trim left/right subtrees
        root-&gt;left = trimBST(root-&gt;left, L, R);
        root-&gt;right = trimBST(root-&gt;right, L, R);
        return root;
    }
};</pre><p>The previous solution has potential memory leak for languages without garbage collection.</p>
<p>Here&#8217;s the full program to delete trimmed nodes.</p><pre class="crayon-plain-tag">// Author: Huahua
#include &lt;iostream&gt;
using namespace std;

struct TreeNode {
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
 
class Solution {
public:
    
    // With cleanup -&gt; no memory leak
    TreeNode*&amp; trimBST(TreeNode*&amp; root, int L, int R) {
        if(!root) return root;
        
        if(root-&gt;val &lt; L) {            
            auto&amp; result = trimBST(root-&gt;right, L, R);
            deleteTree(root-&gt;left);
            delete root;
            root=nullptr;
            return result;
        } else if(root-&gt;val &gt; R) {
            auto&amp; result = trimBST(root-&gt;left, L, R);
            deleteTree(root-&gt;right);
            delete root;
            root=nullptr;
            return result;
        } else {
            // recusively trim left/right subtrees
            root-&gt;left = trimBST(root-&gt;left, L, R);
            root-&gt;right = trimBST(root-&gt;right, L, R);
            return root;
        }
    }
    
    void deleteTree(TreeNode* &amp;root) {
        if(!root) return;
        deleteTree(root-&gt;left);
        deleteTree(root-&gt;right);
        delete root;
        root=nullptr;
    }
};

void PrintTree(TreeNode* root) {
    if(!root) {
        cout&lt;&lt;"null ";
        return;
    };
    if(!root-&gt;left &amp;&amp; !root-&gt;right) {
        cout&lt;&lt;root-&gt;val&lt;&lt;" ";
    } else {
        cout&lt;&lt;root-&gt;val&lt;&lt;" ";
        PrintTree(root-&gt;left);
        PrintTree(root-&gt;right);
    }
}


int main()
{
    TreeNode* root=new TreeNode(2);
    root-&gt;left=new TreeNode(1);
    root-&gt;right=new TreeNode(3);
    PrintTree(root);
    std::cout&lt;&lt;std::endl;
    
    TreeNode* t = Solution().trimBST(root, 3, 4);
    PrintTree(t);
    std::cout&lt;&lt;std::endl;

    // Original root was deleted
    PrintTree(root);
    std::cout&lt;&lt;std::endl;
    
    return 0;
}</pre><p>Example output</p><pre class="crayon-plain-tag">2 1 3 
3 
null</pre><p>&nbsp;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/leetcode/leetcode-669-trim-a-binary-search-tree/">花花酱 LeetCode 669. Trim a Binary Search Tree</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/leetcode/leetcode-669-trim-a-binary-search-tree/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
