title: How to convert a string of integer numbers into an array in c++?
body: <h3>The problem:</h3>
  <p>A function which gets <strong>degrees</strong> and <strong>factors</strong> as inputs and returns a equation as output.</p>
  <h3>The issue:</h3>
  <p>I did not know how to read an array of numbers in form of a string in c++ back then in 2016 when I was a super junior. I also did not know how to search <strong>good</strong> enough!</p>
  <h3>Update:</h3>
  <p>I answered my question and you can test this in this link: <a href="http://cpp.sh/42dwz" rel="nofollow noreferrer">http://cpp.sh/42dwz</a></p>
  <h3>Answer details:</h3>
  <ol>
  <li>Main part of the code will be like this:</li>
  </ol>
  <pre><code>int main()
  {
    Poly mypoly(&quot;2 -4 3&quot;, &quot;1 5 1&quot;);
    return 0;
    
  }
  </code></pre>
  <ol start="2">
  <li>Inputs are <code>2 -4 3</code> and <code>1 5 1</code>.</li>
  <li>Output should be <code>(2X) + (-4X5) + (3X)</code></li>
  <li>Class <code>Poly</code> has a built-in feature to print the result</li>
  <li>To make it easier we should convert <strong>degrees</strong> and <strong>factors</strong> from a single <code>string</code> into an array of <code>string</code>s.</li>
  <li>This means that a string like <code>2 -4 3</code> changes into <code>[2, -4, 3]</code> which makes it easy to iterate over items and create <em>equation sentences</em></li>
  <li>This action is called <em><strong>splitting a string into an array by a delimiter</strong></em> which I found here for c++ <a href="https://stackoverflow.com/a/16030594/5864034">https://stackoverflow.com/a/16030594/5864034</a></li>
  <li>Rest of the code is just looping over the array of <em><strong>degrees</strong></em> and <em><strong>factors</strong></em> to create sentences(which is pretty easy just check the answer link <a href="http://cpp.sh/42dwz" rel="nofollow noreferrer">http://cpp.sh/42dwz</a>)</li>
  </ol>
  <h4>The code:</h4>
  <pre><code>// Example program
  #include &lt;iostream&gt;
  #include &lt;string&gt;
  #include &lt;sstream&gt;
  #include &lt;iterator&gt;
  
  using namespace std;
  
  template &lt;size_t N&gt;
  void splitString(string (&amp;arr)[N], string str)
  {
      int n = 0;
      istringstream iss(str);
      for (auto it = istream_iterator&lt;string&gt;(iss); it != istream_iterator&lt;string&gt;() &amp;&amp; n &lt; N; ++it, ++n)
          arr[n] = *it;
  }
  
  class Poly {
    public:
      string degree[10];
      string factor[10];
      
      Poly(string input_degree, string input_factor) {
          splitString(degree, input_degree);
          splitString(factor, input_factor);
          
          for (int i = 0; i &lt; 10; i++){
              int this_degree = stoi(degree[i]);
              int this_factor = stoi(factor[i]);
              
              string this_sentence = &quot;&quot;;
              
              if(this_degree != 1 &amp;&amp; this_degree != 0 ){
                  this_sentence = this_sentence + degree[i];
                  
                  if(this_factor != 0){
                      if(this_factor != 1){
                          this_sentence = this_sentence + &quot;X&quot; + factor[i];
                      }else{
                          this_sentence = this_sentence + &quot;X&quot;;
                      }
                  }
              }
              
              if(this_sentence != &quot;&quot;){
                  cout &lt;&lt; &quot;(&quot; &lt;&lt; this_sentence &lt;&lt; &quot;)&quot;;
              }
              
              if(stoi(degree[i+1]) != 0 &amp;&amp; stoi(degree[i+1]) != 1){
                  cout &lt;&lt; &quot; + &quot;;
              }
          }
      }
  };
  
  int main()
  {
    Poly mypoly(&quot;2 -4 3&quot;, &quot;1 5 1&quot;);
    return 0;
    
  }
  
  </code></pre>