title: C++ std::sregex_token_iterator dereference and cast to int
body: <p>I'm doing <a href="https://leetcode.com/problems/serialize-and-deserialize-binary-tree/" rel="nofollow noreferrer">leetcode 297. Serialize and Deserialize Binary Tree</a>, and I'm trying to solve it with <code>std::sregex_token_iterator</code> (other than using <code>std::istringstream</code>/<code>std::ostringstream</code>).</p>
  <p>My solution is to append a whitespace when reading(serializing) a TreeNode value into the string, and using whitespace as delimieter when deserializing.</p>
  <p>But due to &quot;Definition for a binary tree node&quot; (<code>TreeNode(int x) : val(x), left(NULL), right(NULL) {}</code>), I have to cast elements pointed by <code>sregex_token_iterator</code> to <code>int</code>s.</p>
  <p>I have read <a href="https://en.cppreference.com/w/cpp/regex/regex_token_iteratorhttps://en.cppreference.com/w/cpp/regex/regex_token_iterator" rel="nofollow noreferrer">cppreference std::regex_token_iterator</a>, and I'm still not sure if the type of the element dereferenced by <code>sregex_token_iterator</code> is <code>std::string</code>. (At first I thought it is definitely <code>std::string</code> type.)</p>
  <p>But error occured when I tried to do some attempts, like:</p>
  <pre><code>int val = stoi(*it);
  TreeNode* node = new TreeNode(val);
  </code></pre>
  <p>or</p>
  <pre><code>string item = *it;
  int val = stoi(item);
  TreeNode* node = new TreeNode(val);
  </code></pre>
  <p>which all result in error</p>
  <pre><code>terminate called after throwing an instance of 'std::invalid_argument'
    what():  stoi
  </code></pre>
  <p>And I've googled, but I didn't find question simiar to mine.</p>
  <p>Below is my code:</p>
  <pre><code>/*
   * @lc app=leetcode id=297 lang=cpp
   *
   * [297] Serialize and Deserialize Binary Tree
   */
  
  // @lc code=start
  /**
   * Definition for a binary tree node.
   * struct TreeNode {
   *     int val;
   *     TreeNode *left;
   *     TreeNode *right;
   *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
   * };
   */
  class Codec {
  public:
      // Encodes a tree to a single string.
      string serialize(TreeNode* root) {
          string data;
          serialize(root, data);
          return data;
      }
     
      // Decodes your encoded data to tree.
      TreeNode* deserialize(string data) {
          // whitespace
          regex rgx {&quot;\\s+&quot;};
          auto it = sregex_token_iterator {data.begin(), data.end(), rgx, -1};
          auto it_end = sregex_token_iterator();
          TreeNode* root = deserialize(data, rgx, it, it_end);
          return root;
      }
  private:
      void serialize(TreeNode* node, string&amp; data) {
          if (!node) {
              data += &quot;# &quot;;
              return;
          }
          data += node-&gt;val;
          data += &quot; &quot;;
          serialize(node-&gt;left);
          serialize(node-&gt;right);
      }
  
      TreeNode* deserialize(string&amp; data, regex&amp; rgx, sregex_token_iterator&amp; it, sregex_token_iterator&amp; it_end) {
          if (*it == &quot;#&quot;) {
              return nullptr;
          }
          /*** Error Here: ***/
          // int val = stoi(*it);
          string item = *it;
          int val = stoi(item);
          TreeNode* node = new TreeNode(val);
  
          /* left tree */
          ++it;
          node-&gt;left = deserialize(data, rgx, it, it_end);
          /* right tree */
          ++it;
          node-&gt;right = deserialize(data, rgx, it, it_end);
          return node;
      }
  };
  // Your Codec object will be instantiated and called as such:
  // Codec ser, deser;
  // TreeNode* ans = deser.deserialize(ser.serialize(root));
  // @lc code=end
  </code></pre>
  <p>Is there any way to solve this casting problem without changing much of the code above?
  (i.e. do not change deserialize function from recursive one to iterative one.)</p>
  <p>/*** Edit ***/</p>
  <p>But after I revised it according to @Cubbi with <code>data += to_string(node-&gt;val)</code>, another error occured:</p>
  <pre><code>Line 2778: Char 16: runtime error: reference binding to null pointer of type
  'const std::__cxx11::regex_token_iterator&lt;__gnu_cxx::__normal_iterator&lt;
  const char *, std::__cxx11::basic_string&lt;char&gt;&gt;, char, std::__cxx11::regex_traits&lt;char&gt;&gt;::
  value_type' (aka 'const sub_match&lt;__gnu_cxx::__normal_iterator&lt;const char *, std::__cxx11::
  basic_string&lt;char&gt;&gt;&gt;') (regex.h)
  </code></pre>
  <p>, but I have initialized <code>it</code> and <code>it_end</code> (while in my solution <code>it_end</code> is not necessary) and still cannot find any possible errors.</p>
  <p>Another try, adding:</p>
  <pre><code>if (it == it_end) {
     return nullptr;
  }
  </code></pre>
  <p>in <code>deserialize()</code> recursive function, but surprisingly get an error with respect to <code>vector</code> which I didn't use it in my code.</p>
  <pre><code>Line 1056: Char 34: runtime error: addition of unsigned offset to 0x6080000000a0 overflowed to 0x608000000058 (stl_vector.h)
  SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior /usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/stl_vector.h:1061:34
  </code></pre>