gpt4 book ai didi

c++ - 计数但小错误

转载 作者:行者123 更新时间:2023-11-28 06:00:43 24 4
gpt4 key购买 nike

我正在做单独的一个字符串,当看到分隔符时我确实添加了一个案例,它对最后一个分隔符工作正常。例如我的字符串是 "symbol control_line : std_logic:= '0' ; --example comment" 当看到第一个定界符时输出是正确的:但是当它看第二个时 := 它失败了。我不知道为什么会这样?代码应该适用于两个定界器,为什么只找出第一个,但第二个却失败了?

这个 prepareNextToken 函数是计算出第二个 Token 的 tokenLength 是多少。我可以使用这个函数来获取当前 token 。

void Tokenizer::prepareNextToken()
{
string real=*str;
if(offset==real.size())
complete=true;
else
{
if(ifcomment==false)
{
size_t length=0;
size_t index=offset;
size_t smallest=find_first_delimilater(vhdl_char);
while(index<real.size() )
{
length++;
if(index==smallest && real[index+1]==' ')
{
cout<<real[smallest]<<" ";
break;
}
else if(index==smallest && real[index+1]!=' ')
{
length++;
break;
}
else if(index==real.find(' ',offset))
{
break;
}
else if(index==real.find("--",offset))
{
length++;
break;
}
index++;
}
tokenLength=length;
}
else if(ifcomment==true)
tokenLength=real.size()-offset;
}
//cout<<tokenLength<<endl;
}

我的输出是

    signal            --which is correct
control_line --the current offset
: --which is right because I reach the first case in my
--prepareNextToken and ":" is first delimilator
std_logic:= --that is the wrong output because it should be std_logic
-- and in a separate line comes out ";=" which is another
--delimilator, and is a multiple delimilator no empty case
-- so that means I go to the second cases
-- -- which is also right which go to fourth case
sample comment -- which is right

我的问题是为什么当“:”出现在它自己的行中,但为什么“:=”出现时它以 std_logic 结尾?

最佳答案

substr 的第二个参数是要提取的字符数而不是结束位置(参见 http://www.cplusplus.com/reference/string/string/substr/ )。所以你的提取线应该是:

s=name.substr(offset,tokenLength);

关于c++ - 计数但小错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33332448/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com