954,170 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

What's wrong with string.replace()?

Anybody can help to explain why "m_line.replace(idx, m_search.size(), replace);" doesn't compile in cygwin and how to change it?
Thanks.

using namespace std;

int main(int argc, char *argv[])
{
  assert(argc==3 && 
    "Usage: <searchstring> replacestring> to process stdin");
  string m_line;
  string m_search(argv[1]);
  string m_replace(argv[2]);
  
  assert(m_search!=m_replace);
  
  while(getline(cin, m_line)) {
      while(true) {
          string::size_type idx = m_line.find(m_search);
          if(idx == string::npos) break;
          m_line.replace(idx, m_search.size(), replace);
      }
      cout << m_line << endl;
  }    
  
  system("PAUSE");	
  return 0;
}


<< moderator edit: fixed [code][/code] tags >>

banbangou
Newbie Poster
3 posts since Mar 2005
Reputation Points: 10
Solved Threads: 0
 

>Anybody can help to explain why "m_line.replace(idx, m_search.size(), replace);" doesn't compile in cygwin
Perhaps because replace isn't a valid third argument? How about:

m_line.replace(idx, m_search.size(), m_replace);
Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

Thanks, I am so stupid not seeing that.

banbangou
Newbie Poster
3 posts since Mar 2005
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You