I wrote
String STR
If (STR), find (' + ')!=string. Npos)
{
The position=STR. Find (' + ');
.
}
Else if (STR), find (' - ')!=string. Npos)
{
The position=STR. Find (' - ');
.
}
Else if (STR), find ('/').=string. Npos)
{
The position=STR. Find ('/');
.
}
Else if (STR), find (' * ')!=string. Npos)
{
The position=STR. Find (' * ');
.
}
A symbol of the position, how to simplify
Not analytic expression, no prizes for guessing
CodePudding user response:
# include & lt; Iostream>are for reference only
#include
using namespace std;
Int main (void) {
String STR, target="+ - */";
Auto positoin=string: : npos;
For (char c: target) {
If ((positoin=STR. Find (c)).=the string: : npos)
break;
}
return 0;
}
CodePudding user response:
if (((position=STR. Find (' + '))!=the string: : npos) | |
((position=STR. Find (' - '))!=the string: : npos) | |
((position=STR. Find (' * '))!=the string: : npos) | |
(the position=STR. Find ('/'))!=the string: : npos)
) {
//other code...
}
Save query again the steps, at the same time shrink into an if performed
CodePudding user response:
This is the or relationship because of you, can consider to use hash, so only need to traverse the STR once can safely draw the conclusion that the
Char str_hash [256]={0};
Str_hash [(unsigned char) '+']=1;
Str_hash [(unsigned char) '-']=1;
Str_hash [(unsigned char) '*']=1;
Str_hash [(unsigned char) '/']=1;
String STR="abcd - + sdfajjj";
Int get_operator_pos (string & amp; STR)
{
for(int i=0; i{
Char ch=STR [I];
If (str_hash [(unsigned char) ch]!=0)
return i;
}
}
CodePudding user response:
With regular expressions, so you judge has serious shortcomings, if the condition is more will be repeated traversal, seriously affect the efficiencyCodePudding user response:
Find the source code for Linux is a dance judge, high efficiency more than doubled, so much,CodePudding user response:
Direct write debugging
Int to obtain position (string STR, char a)
{
If (STR), find (a)!=string. Npos)
Return the position=STR. Find (' + ');
}
CodePudding user response: