title: How do I replace all instances of a string with another string?
body: <p>I found this on another stack question: </p>
  
  <pre><code>//http://stackoverflow.com/questions/3418231/c-replace-part-of-a-string-with-another-string
  //
  void replaceAll(std::string&amp; str, const std::string&amp; from, const std::string&amp; to) {
      size_t start_pos = 0;
      while((start_pos = str.find(from, start_pos)) != std::string::npos) {
          size_t end_pos = start_pos + from.length();
          str.replace(start_pos, end_pos, to);
          start_pos += to.length(); // In case 'to' contains 'from', like replacing 'x' with 'yx'
      }
  }
  </code></pre>
  
  <p>and my method: </p>
  
  <pre><code>string convert_FANN_array_to_binary(string fann_array)
  {
      string result = fann_array;
      cout &lt;&lt; result &lt;&lt; "\n";
      replaceAll(result, "-1 ", "0");
      cout &lt;&lt; result &lt;&lt; "\n";
      replaceAll(result, "1 ", "1");
      return result;
  }
  </code></pre>
  
  <p>which, for this input:</p>
  
  <pre><code>cout &lt;&lt; convert_FANN_array_to_binary("1 1 -1 -1 1 1 ");
  </code></pre>
  
  <p>now, the output should be "110011"</p>
  
  <p>here is the output of the method: </p>
  
  <pre><code>1 1 -1 -1 1 1  // original
  1 1 0 1  // replacing -1's with 0's
  11 1  // result, as it was returned from convert_FANN_array_to_binary()
  </code></pre>
  
  <p>I've been looking at the replaceAll code, and, I'm really not sure why it is replacing consecutive -1's with one 0, and then not returning any 0's (and some 1's) in the final result. =\</p>