title: How to replace multiple sets of keywords in a string?
body: <p>So I have a file of strings that I am reading in, and I have to replace certain values in them with other values. The amount of possible replacements is variable. As in, it reads the patterns to replace with in from a file. Currently I'm storing in a <code>vector&lt;pair&lt;string,string&gt;&gt;</code> for the patterns to find and match. However I run into issues:</p>
  
  <blockquote>
    <p><strong>Example:</strong></p>
    
    <p>Input string: <code>abcd.eaef%afas&amp;333</code></p>
    
    <p>Delimiter patterns:</p>
    
    <blockquote>
      <p><code>. %%%</code></p>
      
      <p><code>% ###</code></p>
      
      <p><code>&amp; @@@</code></p>
    </blockquote>
    
    <p>Output I want: <code>abcd%%%eaef###afas@@@333</code></p>
    
    <p>Output I get: <code>abcd#########eaef###afas@@@333</code></p>
  </blockquote>
  
  <p>The issue being it ends up replacing the <code>%</code> sign or any other symbol that was already a replacement for something else, it should not be doing that.</p>
  
  <p>My code is (relevant portions):</p>
  
  <pre><code>std::string&amp; replace(std::string&amp; s, const std::string&amp; from, const std::string&amp; to){
      if(!from.empty())
          for(size_t pos = 0; (pos = s.find(from, pos)) != std::string::npos; pos += to.size()) s.replace(pos, from.size(), to);
      return s;
  }
  string line;
  vector&lt;pair&lt;string, string&gt;&gt; myset;
  while(getline(delimiterfile, line)){
      istringstream is(line);
      string delim, pattern;
      if(is &gt;&gt; delim &gt;&gt; pattern){
          myset.push_back(make_pair(delim, pattern));
      } else {
          throw runtime_error("Invalid pattern pair!");
      }
  }
  
  while(getline(input, line)){
      string temp = line;
      for(auto &amp;item : myset){
          replace(temp, item.first, item.second);
      }
      output &lt;&lt; temp &lt;&lt; endl;
  }
  </code></pre>
  
  <p>Can someone please tell me what I'm messing up and how to fix it?</p>