C++:替换字符时出错
C++ : Error while replacing character
我正在尝试将字符串中的";"替换为子字符串,稍后我将在其上拆分流。现在的问题是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 );
}
相关文章:
- 分段 使用 MPI_Gather 收集字符数组时出错
- 将常量字符* 转换为字符时出错
- 读取字符串C++的字符时出错
- 为什么常量字符*到常量字符转换出错?
- 读取字符串字符时出错 - 对于数组中的字符串值
- 通过字符串::插入插入字符时出错
- 读取字符串字符时出错
- 计算字符串中字符的出现次数时出错
- C++ 使用 Strtok 读取字符串字符时出错
- C++:使用 fgets() 读取字符输入时出错
- C ++ ifstream 无法读取字符串,并且在读取字符时出错
- 尝试将常量字符 [] static_cast<>为无符号字符 * 时出错
- 从字符串中读取字符时出错
- 英特尔 TBB,parallel_pipeline,当我在阶段之间传递常量字符*时出错
- 转换(常量字符*)变量出错
- 为什么我从字符转换为字符时出错
- 读取字符串C++的字符时出错
- wcscpy_s后读取字符串字符时出错
- 读取字符串 的字符时出错.分配内存
- 读取字符串的字符时出错-访问冲突错误C++