C ++: error when replacing a character

I am trying to replace ";" in a line with a substring on which I will split my stream later. The problem is now in string::replace(). Here is the code:

std::string Lexer::replace(const std::string &line) const
{
  std::size_t   start_pos;
  std::string   tmp(line);

  start_pos = 0;
  while ((start_pos = tmp.find(";", start_pos)) != std::string::npos)
  {
    tmp.replace(start_pos, 1, " ");
    start_pos += 1;
  }
  return (tmp);
}

The string linemay be like this: word1 word2 word3; word1 word2 word3;.... It works for a type string word1 word2 word3;, but here is what I get for a type string word1 word2 word3; word1 word2 word3;:

terminate called after throwing an instance of 'std::out_of_range'
  what():  basic_string::replace
Aborted

I do not see what I am doing wrong. I read that this error occurs when the given position in string::replace(pos, len, substr)is equal string::npos, so why the condition in my loop does not help to avoid this?

Thank.

+4
source share
2 answers

It seems you are not initializing start_pos, so you will need to change this line:

std::size_t   start_pos = 0;
//                     ^^^^

undefined , .

, string:: size_type, .

:

main.cpp

#include <string>
#include <iostream>

using namespace std;

string myreplace(const string &line)
{
    string::size_type   start_pos = 0;
    string   tmp(line);

    while ((start_pos = tmp.find(";", start_pos)) != string::npos)
    {
        tmp.replace(start_pos, 1, " ");
        start_pos += 1;
    }
    return tmp;
}

int main()
{
    string test_str1 = "word1 word2 word3;";
    string test_str2 = "word1 word2 word3; word1 word2 word3;";
    string test_str3 = "word1 word2 word3; word1 word2 word3;....";

    cout << myreplace(test_str1) << endl;
    cout << myreplace(test_str2) << endl;
    cout << myreplace(test_str3) << endl;

    return 0;
}

word1 word2 word3 
word1 word2 word3  word1 word2 word3 
word1 word2 word3  word1 word2 word3 ....

=============================================== ===

std :

#include <string>
#include <iostream>
#include <algorithm>

using namespace std;

int main()
{
    string test_str1 = "word1 word2 word3;";
    string test_str2 = "word1 word2 word3; word1 word2 word3;";
    string test_str3 = "word1 word2 word3; word1 word2 word3;....";

    string out_str1 = replace(test_str1.begin(), test_str1.end(), ';', ' ');
    string out_str2 = replace(test_str2.begin(), test_str2.end(), ';', ' ');
    string out_str3 = replace(test_str3.begin(), test_str3.end(), ';', ' ');

    cout << out_str1 << endl;
    cout << out_str2 << endl;
    cout << out_str3 << endl;
    return 0;
}

word1 word2 word3 
word1 word2 word3  word1 word2 word3 
word1 word2 word3  word1 word2 word3 ....
+2

start_pos

std::size_t   start_pos;

, undefined.

std::size_t   start_pos = 0;

, std::string.

std::string::size_type   start_pos = 0;

, size_t( -1 ) std::string::size_type( -1 ), std::string::npos.

std::replace, <algorithm>

std::string Lexer::replace(const std::string &line) const
{
   std::string   tmp(line);

   std::replace( tmp.begin(), tmp.end(), ';', ' ' );

   return (tmp);
}

EDIT: ,

std::string Lexer::replace(const std::string &line, const char *replacement ) const
{
   std::string tmp( line );
   size_t n = std::strlen( replacement ); 

   std::string::size_type start_pos = 0;
   while ( ( start_pos = s.find( ';', start_pos ) ) != std::string::npos )
   {
      line.replace( start_pos, 1, replacement );
      start_pos += n;
   }

   return ( tmp );
} 
+2

Source: https://habr.com/ru/post/1538192/


All Articles