使用 str.erase() 的索引擦除字符串的元素?
Erasing an element of a string using an index with str.erase()?
如何使用erase()
方法以str.erase(str[i])
的形式擦除std::string
"str"的一部分?
当我运行下面的代码时,它只输出字符串减去第一个元素,然后是一些奇怪的字符。
#include<bits/stdc++.h>
using namespace std;
int main()
{
string s;
cout << "Please input the desired string : n";
cin >> s;
int sz = sizeof s;
int x = sz / 2;
if ((sizeof s)%2 != 0)
{
s.erase(s[x]);
}
else
{
s.erase(s[x-1], s[x]);
}
for (int j=1; j <= sz; j++)
{
cout << s[j];
}
}
std::string::erase()
方法有几个重载:
basic_string& erase( size_type index = 0, size_type count = npos );
iterator erase( iterator position );
iterator erase( const_iterator position );
iterator erase( iterator first, iterator last );
iterator erase( const_iterator first, const_iterator last );
s.erase(s[x])
调用第一个重载,s[x]
视为index
(因为char
可隐式转换为std::string::size_type
)。
首先,s[x]
不是有效的索引(不过,x
单独是),所以这是未定义的行为。
其次,您没有显式指定count
参数的值,因此使用默认值npos
,它告诉erase()
删除所有字符,包括指定index
之后的字符,这可能不是您想要的(或者它?您是要仅删除index
处的 1char
,还是index
后的所有char
?s.erase(s[x-1], s[x])
还调用第一个重载(隐式将 2char
秒转换为size_type
秒)。 同样,您传入的char
不是有效的索引或计数,因此这也是未定义的行为。
您的代码中还存在其他问题。
sizeof s
返回s
的字节大小(即std::string
类本身的大小),而不是s
包含的字符数据的长度。 这些字符存储在内存中的其他位置(除非您输入一个小字符串,并且您的 STL 实现std::string
采用小字符串优化)。使用s.length()
或s.size()
获取字符数据的长度。
您看到意外的输出std::string
因为索引从 0 开始,而不是从 1 开始。 你的for
循环跳过s
中的第一个字符,并越过s
的末尾进入周围的记忆,这也是未定义的行为。 更重要的是,当你调用s.erase()
时,你正在修改s
的长度,但你没有更新你的sz
变量来反映新的长度,所以你的for
循环可能会超过s
的结束。
话虽如此,请尝试更多类似的东西:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s;
cout << "Please input the desired string : n";
cin >> s;
size_t sz = s.length();
size_t x = sz / 2;
if ((sz % 2) != 0)
{
s.erase(x, 1);
}
else
{
s.erase(x-1, 2); // or (x-1, 1)? What do you want to erase exactly?
}
sz = s.length();
for (int j = 0; j < sz; ++j)
{
cout << s[j];
}
return 0;
}
- C++ 字符串类擦除成员函数的时空复杂性
- 在 stl 中的字符串中擦除关键字的工作?
- 无法使用"擦除"函数从 c++ 字符串中删除数字
- 当找到来自 RHS 的字符"\"时,从主字符串中获取单词或子字符串,然后擦除其余部分
- 使用 str.erase() 的索引擦除字符串的元素?
- 字符串附加会擦除字符串?
- C++ cout 在擦除换行符后打印字符串不正确
- 使用递归擦除字符串上的所有非数字
- 如何更快地擦除我的字符串
- 使用反向remove_if擦除字符串
- 用字符串::擦除在C 中提取数据
- 从 std::字符串中删除字符从 "(" 到 ")" 用擦除?
- 使用擦除和删除从字符串中删除字符
- 字符串的C ++向量,擦除字符的行为很奇怪
- 从字符串中擦除单词(在C++中)
- 找到字符串c++中的字符序列并将其擦除
- 如何擦除字符串中的字符
- 只擦除字符串(C++)中的一个元素
- 为什么我无法擦除字符串的数字字符?
- 为什么我不能在 C++ 中使用 str.erase(str.begin() + index) 擦除字符串中的字符?