C++:替换字符时出错

C++ : Error while replacing character

本文关键字:出错 字符 替换 C++      更新时间:2023-10-16

我正在尝试将字符串中的";"替换为子字符串,稍后我将在其上拆分流。现在的问题是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,因此您需要更改此行:

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

否则,您将获得未定义的行为,其中某些垃圾值可能代表起始位置。

另外,请注意,最好使用 string::size_type,因为您在迭代时在此处使用字符串大小。

这段代码对我来说很好用:

主.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 ....

您没有初始化变量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 );
}