【问题标题】:Exception when comparing an (int)double and (int)int [duplicate]比较 (int)double 和 (int)int 时出现异常 [重复]
【发布时间】:2023-03-12 20:12:01
【问题描述】:

可能重复:
Exception when comparing an (int)double and (int)int

忽略此帖。不小心转贴了

对不起

【问题讨论】:

  • 当您的调用堆栈清楚地表明它发生在某个std::string 操作中时,您从哪里得到if 语句正在发生这种奇怪的想法?
  • 另外,long float 应该是什么意思? C++ 中没有这种类型。
  • 只问一次就够了。
  • 我不确定instr((char*)_contents[r].c_str()) 做了什么。但如果它采用char*,请不要通过const char* typecasted。可能是它在内部被释放,当字符串超出范围时,它会再次被释放
  • @Griffin:你问同一个问题两次是因为......? stackoverflow.com/questions/6013585/…

标签: c++ exception if-statement double int


【解决方案1】:
if((int)time >= 600){   time_s.insert(4, sec);

应该是,

if((int)time >= 600){   time_s.insert(3, sec);  // digit 3 instead of 4

根据您的代码,我想字符串大小为6 个字符(0 到 4 和第 5 个字符为nul)。在第 4 位和第 5 位插入 2 位数字将覆盖 nul。

请进行交叉验证,因为我看到您的代码已经猜到了。

【讨论】:

  • 你在说什么nul?
  • 我想他的意思是null terminator
  • 但std::string 中没有空终止符。除此之外,insert 方法不会覆盖任何内容。顾名思义,它插入。
  • @AndreyT,克里斯托弗是对的。我的意思是null terminator(在许多约定中拼写为nul)。这只是最后一个 0 字符。
  • @iammilind:好吧,std::string 中没有“空终止符”。 std::string 是一个存储长度的字符串,而不是一个以 null 结尾的字符串。
猜你喜欢
  • 2011-08-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-06
  • 2022-01-24
相关资源
最近更新 更多