C++ C++;:为什么赢了';t erase()是否删除字符串中的第一个字符?
我正在编写一个rational类,其中必须有以下可接受的输入形式: 3/4,1.4545,(7/8,(7/8),7/8,甚至(((5/4)),我的代码应该重写为5/4,让构造函数接受 此时,我将一个字符串参数传递给一个构造函数,该构造函数分别调用formatStringNumerator()和formatStringDenominator() 我还没有得到小数,但到目前为止,左括号中的输入以某种方式将分子值设置为0。我认为错误是“(”在数字之前。我已经注释了我试图删除左括号的尝试,但它们不会消失。我尝试使用substr()作为一种替代方法,但在我打印find()值时出现了错误。另外,当我打印出find()值时,当找不到参数时,我得到了一些大数字[18446744073709551615]。我觉得如果找不到,它应该返回-1(false?) 在第一条注释中,erase()应该删除字符串中的第一个字符,即括号,但它只是不删除自身,从字符串到int的转换变得混乱,像我前面提到的那样,默认为0C++ C++;:为什么赢了';t erase()是否删除字符串中的第一个字符?,c++,string,if-statement,erase,rational-numbers,C++,String,If Statement,Erase,Rational Numbers,我正在编写一个rational类,其中必须有以下可接受的输入形式: 3/4,1.4545,(7/8,(7/8),7/8,甚至(((5/4)),我的代码应该重写为5/4,让构造函数接受 此时,我将一个字符串参数传递给一个构造函数,该构造函数分别调用formatStringNumerator()和formatStringDenominator() 我还没有得到小数,但到目前为止,左括号中的输入以某种方式将分子值设置为0。我认为错误是“(”在数字之前。我已经注释了我试图删除左括号的尝试,但它们不会消失
int formatStringNumerator(string &rational){
string tmp;
int numerator;
// if(rational.find("(") != false){
// rational.erase(0, 1);
// } //This should have deleted an open parenthesis
if(rational.find("/") != false){
tmp = rational.substr(0, rational.find("/"));
cout << "\n" << tmp << endl; //cout to test if ran
} //Should assign numbers leading to the symbol("/") to tmp
istringstream is;
is.str(tmp); //Numerator should have int value with parenthesis removed
is >> numerator;
return numerator;
}
int formatStringDenominator(string &rational){
string tmp;
int denominator;
if(tmp.find("/") != false){
tmp = rational.substr((rational.find("/")+1));
} //assign value after symbol to tmp
// if(tmp.find(")") != false){
// tmp.erase(tmp.find(")")); //Successfully removes parenthesis
// cout << "\n" << tmp << endl; //Bad condition in if statement?
// } //cout to test if ran
istringstream is;
is.str(tmp);
is >> denominator;
return denominator;
}
int-formatStringNumerator(字符串和有理数){
串tmp;
整数分子;
//if(rational.find(“”!=false){
//有理。擦除(0,1);
//}//本应删除一个开括号
if(rational.find(“/”)=false){
tmp=rational.substr(0,rational.find(“/”);
cout您显示的函数是错误的。类std::string
的成员函数find
不返回bool值。它返回类型为std::string::size\u类型的对象,因此,例如,第一个函数可以按以下方式查看
int formatStringNumerator( const string &rational )
{
string tmp( rational, 0, rational.find("/") );
tmp.erase( 0, tmp.find_first_not_of( "(" ) );
return ( tmp.empty() ? 0 : std::stoi( tmp ) );
}
下面是使用该函数的示例
#include <iostream>
#include <string>
using namespace std;
int formatStringNumerator( const string &rational )
{
string tmp( rational, 0, rational.find("/") );
tmp.erase( 0, tmp.find_first_not_of( "(" ) );
return ( tmp.empty() ? 0 : std::stoi( tmp ) );
}
int main()
{
std::string s( "((((12/15" );
std::cout << formatStringNumerator( s ) << std::endl;
std::string t( "((((12" );
std::cout << formatStringNumerator( t ) << std::endl;
std::string u( "((((" );
std::cout << formatStringNumerator( u ) << std::endl;
return 0;
}
你用谷歌搜索过std::string
find
并阅读了结果文档吗?它没有返回bool
@Yakk,真不敢相信我错过了这个,但是如果find()函数失败,它应该返回npos,它等于-1。但是,我得到了这么大的数字。我将进行编辑,看看它是如何运行的npos
实际上不是-1
。它是-1
转换为无符号数,这是一个非常大的正数(您看到的那个)。您不必担心该值的实际值,只需使用npos
。您应该使用std::string::npos
而不是-1
。如果您确实使用-1
,请将其转换为大小。\u t
。但没有理由不使用npos
。
12
12
0