C++ 从字符串中删除一些字符并复制它
我正试图用这种愚蠢的方法从字符串中删除非数字或字母的字符,因为我在获得其他方法时失败了(我是一个真正的初学者,“只是在尝试”) 我知道这种方法不正确,但我的问题是,它有什么问题,因为它不起作用,有什么错误:SC++ 从字符串中删除一些字符并复制它,c++,string,char,C++,String,Char,我正试图用这种愚蠢的方法从字符串中删除非数字或字母的字符,因为我在获得其他方法时失败了(我是一个真正的初学者,“只是在尝试”) 我知道这种方法不正确,但我的问题是,它有什么问题,因为它不起作用,有什么错误:S string g = "9-=p98u;iu8y76"; string y; int t = 0; for (int i = 0; i < g.length(); i++) { if (isdigit(g[i]) || isalpha(g[i])) {
string g = "9-=p98u;iu8y76";
string y;
int t = 0;
for (int i = 0; i < g.length(); i++)
{
if (isdigit(g[i]) || isalpha(g[i]))
{
y[t++] = g[i];
}
else
continue;
}
g = y;
cout << g;
string g=“9-=p98u;iu8y76”;
弦y;
int t=0;
对于(int i=0;i cout问题是y
的大小是0,它是空的。因此,访问它的元素(使用y[t++]
)会到达它的缓冲区溢出和未定义行为之后的字符串
您需要扩展y
。要做到这一点,只需对代码进行最小的更改,您需要执行以下操作:
string g = "9-=p98u;iu8y76";
string y;
for (int i = 0; i < g.length(); i++)
{
if (isdigit(g[i]) || isalpha(g[i]))
{
y.push_back(g[i]);
}
else
continue;
}
g = y;
cout << g;
string g=“9-=p98u;iu8y76”;
弦y;
对于(int i=0;i cout问题是y
的大小是0,它是空的。因此,访问它的元素(使用y[t++]
)会到达它的缓冲区溢出和未定义行为之后的字符串
您需要扩展y
。要做到这一点,只需对代码进行最小的更改,您需要执行以下操作:
string g = "9-=p98u;iu8y76";
string y;
for (int i = 0; i < g.length(); i++)
{
if (isdigit(g[i]) || isalpha(g[i]))
{
y.push_back(g[i]);
}
else
continue;
}
g = y;
cout << g;
string g=“9-=p98u;iu8y76”;
弦y;
对于(int i=0;i cout标准库合理地表达了这一点。差不多
auto digit_or_alpha = [](char c){ return isdigit(c) || isalpha(c); };
std::copy_if(g.begin(), g.end(), std::back_inserter(y), digit_or_alpha );
应该有用。背面插入器位于
中。Angew提供了您的不工作的原因。标准库合理地表达了这一点。差不多
auto digit_or_alpha = [](char c){ return isdigit(c) || isalpha(c); };
std::copy_if(g.begin(), g.end(), std::back_inserter(y), digit_or_alpha );
应该有用。背面插入器位于
中。Angew提供了您的字符串无法工作的原因。问题在于您试图扩展字符串y
的方式。索引只能在字符串的域中应用(即,索引不能超过字符串的长度)
将y[t++]=g[i]
更改为y+=g[i]
此外,我想指出的是,您不需要else
分支。当执行到达循环范围的末尾时,它将“自动”继续执行,不需要显式表示
PS:它是经典C++,而不是C++ 11,我会接受Giraffe上尉的回答
< p>问题是你试图扩展字符串<代码> y < />代码。索引只能在字符串的域中应用(即,索引不能超过字符串的长度)
将y[t++]=g[i]
更改为y+=g[i]
此外,我想指出的是,您不需要else
分支。当执行到达循环范围的末尾时,它将“自动”继续执行,不需要显式表示
PS:它是经典C++,不是C++ 11,我会接受Giraffe船长的回答
< p>一般的方法是使用成员函数擦除标准算法<代码> STD::ReaveVII:< /Cult>在标题<代码> < /C> >/P>中声明。
比如说
#include <iostream>
#include <string>
#include <algorithm>
#include <cctype>
int main()
{
std::string s = "9-=p98u;iu8y76";
std::cout << s << std::endl;
s.erase( std::remove_if( s.begin(), s.end(),
[]( char c )
{
return !std::isalnum( ( unsigned char )c );
} ),
s.end() );
std::cout << s << std::endl;
return 0;
}
至于你的代码,那么你正在尝试使用下标操作符
y[t++] = g[i];
对于空字符串
string y;
考虑到您可以使用一个函数,而不是两个函数std::isalpha
和std::isdigit
,如我的演示程序所示
如果您想自己编写循环,那么程序可以如下所示
#include <iostream>
#include <string>
#include <cctype>
int main()
{
std::string s = "9-=p98u;iu8y76";
std::cout << s << std::endl;
std::string::size_type i = 0;
while ( i < s.length() && std::isalnum( ( unsigned char )s[i] ) ) ++i;
std::string::size_type j = i;
while ( i++ < s.length() )
{
if ( std::isalnum( ( unsigned char )s[i] ) ) s[j++] = s[i];
}
s.erase( j );
std::cout << s << std::endl;
return 0;
}
无需使用其他字符串来执行此操作。此类任务的一般方法是使用成员函数擦除以及标准算法std::remove\u(如果在标题
中声明)
比如说
#include <iostream>
#include <string>
#include <algorithm>
#include <cctype>
int main()
{
std::string s = "9-=p98u;iu8y76";
std::cout << s << std::endl;
s.erase( std::remove_if( s.begin(), s.end(),
[]( char c )
{
return !std::isalnum( ( unsigned char )c );
} ),
s.end() );
std::cout << s << std::endl;
return 0;
}
至于你的代码,那么你正在尝试使用下标操作符
y[t++] = g[i];
对于空字符串
string y;
考虑到您可以使用一个函数,而不是两个函数std::isalpha
和std::isdigit
,如我的演示程序所示
如果您想自己编写循环,那么程序可以如下所示
#include <iostream>
#include <string>
#include <cctype>
int main()
{
std::string s = "9-=p98u;iu8y76";
std::cout << s << std::endl;
std::string::size_type i = 0;
while ( i < s.length() && std::isalnum( ( unsigned char )s[i] ) ) ++i;
std::string::size_type j = i;
while ( i++ < s.length() )
{
if ( std::isalnum( ( unsigned char )s[i] ) ) s[j++] = s[i];
}
s.erase( j );
std::cout << s << std::endl;
return 0;
}
无需使用其他字符串来执行此操作。您会收到什么错误消息?它可以编译吗?你应该看看你得到了什么错误信息?它可以编译吗?你应该看看使用