C++:替换字符时出现错误

4

我尝试用一个子字符串替换字符串中的";",然后我会在这个子字符串上拆分我的流。 现在的问题在于string::replace()。以下是代码:

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);
}

line字符串可以是这样的:word1 word2 word3; word1 word2 word3;...。 对于像word1 word2 word3;这样的字符串,它可以正常工作,但对于像word1 word2 word3; word1 word2 word3;这样的字符串,我得到了以下结果:

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

我不知道哪里出错了。我读到说当在string::replace(pos, len, substr)中给定的位置等于string::npos时,就会出现这个错误。那么为何我的循环条件不能避免它呢?

谢谢。


在循环开始之前,您需要将 start_pos 初始化为0。 - Alan Stokes
你在哪里初始化start_pos? - László Papp
是的,你说得对,我忘记粘贴那行代码了,但我不认为这是问题所在,错误仍然存在。无论如何,谢谢你 :) - Amina
2个回答

2

你似乎没有初始化 start_pos,所以需要更改这一行:

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

否则,你会得到未定义的行为,可能代表起始位置的一些垃圾值。
此外,请注意,在迭代时使用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 ....

实际上这已经在我的代码中完成了,但不是在这里。我编辑了我的帖子。 - Amina
尽管如果值超出范围,find 应该返回 npos,因此很难看出 replace 的参数可能无效。 - Alan Stokes
@Amina:看代码,它对我来说似乎是可以工作的。 :O 你声称这段代码对你抛出了异常? - László Papp
@Laszlo:它适用于类似“word1 word2 word3;”这样的内容,但不适用于“word1 word2 word3; word1 word2 word3;....”对吗? - Amina
@LaszloPapp 哦,我的错,它可以工作了!我不知道为什么前两次它没能工作 ^^ 谢谢! - Amina

2

您没有初始化变量 start_pos

std::size_t   start_pos;

所以这段代码存在未定义行为。
请写出。
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的定义。

此外,您可以使用头文件<algorithm>中定义的标准算法std::replace

例如:

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

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

   return (tmp);
}

编辑:如果替换内容由多个字符组成,则可以编写

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 );
} 

是的,我读到了有关替换的内容,但实际上我更喜欢用另一个字符串分隔符来替换它,而不是空格。 - Amina
@Amina 在这种情况下,函数必须有第二个参数来指定字符串的分隔符。 - Vlad from Moscow
在这种情况下,它是没有用的。这是一个小型解析器,我知道字符串分隔符。 - Amina
@Amina,请查看我的更新帖子。 - Vlad from Moscow

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接