C++是一个大小为1但不包含任何内容的字符串
C++ a string which size is 1 but contains nothing
我正在尝试读取一个字符串并使用它来构建bool
表达式。
void Parser::parse(){
string temp="";
char c;
for(int i=0;i< _str.length()+1;i++){
c=_str[i];
if(c!='~' && c!='=' && c!='|' &&c!='&' && c!=' ' &&c!='>'){
temp+=c;}
else if(c==' '){
}
else{
if (temp.substr(0,5)=="false")
b.add_lit(false);
else if(temp.substr(0,4)=="true")
b.add_lit(true);
else if(temp=="");
else{
cout<<"push called"<<endl;
b.add_var(temp);
}
switch (c){
case '~':{
string name=get_next_var(_str,i);
i=get_next_index(_str,i);
cout<<"i is"<<i<<endl;
b.add_var(name);
b.add_op('~');
b.addparent();
temp="";
break;}
case '|':{
char lop=b.get_last_op();
if(lop=='&'||lop=='>'||lop=='='){
b.addparent();}
b.add_op('|');
temp="";
break;}
case '&':{
char lop=b.get_last_op();
if(lop=='>'||lop=='=')
b.addparent();
b.add_op('&');
temp="";
break;}
case '>':{
char lop=b.get_last_op();
if(lop=='='){
b.addparent();}
b.add_op('>');
temp="";
break;}
case'=':
b.add_op('=');
temp="";
}
}
}
if (temp.substr(0,5)=="false")
b.add_lit(false);
else if(temp.substr(0,4)=="true")
b.add_lit(true);
else if(temp==" " ) return;
else if(temp!="" ) {
cout<<"tail called, size:"<<temp.size()<<endl;
b.add_var(temp);
}
}
当我试图在循环中断后推送最后一个temp
时,我总是得到一个temp
,这真的很奇怪。在temp字符串的末尾总是有一些我不知道的东西。
假设我从逻辑上得到一个""
,也就是temp=""
,我的代码会显示temp.size()=1
,但当我尝试打印它时,它什么也不打印。
这也是为什么我需要减去temp以获得"true"answers"false"。
string get_next_var(string str,int i){
char c;
string temp="";
i++;
for(i;i< str.length();i++){
c=str[i];
if(c!='~' && c!='=' && c!='|' &&c!='&' && c!=' '&&c!='>')
temp+=c;
else if(c==' '){
}
else
break;
}
return temp;
}
int get_next_index(string str,int i){
char c;
string temp="";
i++;
for(i;i< str.length();i++){
c=str[i];
if(c!='~' && c!='=' && c!='|' &&c!='&' && c!=' '&&c!='>')
temp+=c;
else if(c==' '){
}
else
break;
}
return i-1;
}
在temp字符串的末尾总是有一些我不知道的东西。
我只是快速浏览了一下你的代码,但我猜这一行是错的:
for(int i=0;i< _str.length()+1;i++) {
.....
}
我怀疑这就是你想要的:
for(int i=0;i< _str.length();i++) {
.....
}
这就是为什么:
// Given this
string _str="abc";
// then this is what you will have
_str.length() // 3
_str[0] // 'a'
_str[1] // 'b'
_str[2] // 'c'
相关文章:
- 函数何时会在c++中包含stack_Unwind_Resume调用
- 通过复制包含目录的内容来安装 spdlog
- 我可以(不)在.cpp文件中包含哪些内容?
- 如果我从流中提取某些内容,该流是否不再包含我提取的内容?
- 遍历列表包含排除最后一个元素的内容
- 正则表达式,包含名称 + "string" + 数字,没有其他内容
- C++标准是否包括标准标头必须包含的内容?
- 为什么我可以在不链接任何额外库的情况下包含 sys/*.h
- 如何读取和输出文件的内容及其包含的字数?
- .c文件应该包含它的.h文件已经包含的内容吗?
- Visual Studio 2017 C++:克隆的 git 存储库中缺少包含的内容
- 计算包含以下内容的字符串的其他想法,例如 "5 + 3 / 2"
- CMake:C++包含相对于基目录的内容
- 将C++预处理器处理我当前 h 文件中包含的 h 文件的内容
- 我如何编写一个脚本来创建.cpp文件,其中包含所有标题内容
- 如何打印包含集合的地图的内容
- C++头文件 - 要包含的内容
- 将字符串解析为字符的指针数组:char[0] 包含完整的字符串,而 [1] 以后不包含任何内容
- Qt不会链接任何内容
- 带有向量的c++不同对象都包含相同的内容