【问题标题】:Boost: how to convert string like "true" or "1" to bool true and "0" or "false" to bool false?Boost:如何将“true”或“1”等字符串转换为bool true,将“0”或“false”转换为bool false?
【发布时间】:2011-12-16 23:05:36
【问题描述】:

boost::lexical_cast<bool, std::string>( string ); 是唯一的选择还是有更快的方法来做这样的事情,它是否适用于True 字符串,它是否适用于1 字符串,它是否适用于TRUE 字符串?

(因为对我来说它给出了bad lexical cast: source type value could not be interpreted as target 错误)

【问题讨论】:

  • 更快?您要与之比较的速度有多慢?
  • 主要是我想知道它是否适用于一般的字符串?
  • 你的性能瓶颈究竟是什么?
  • 在考虑 fast 之前先担心 正确。是什么让您认为boost::lexical_cast 可以将“真”转换为真? It doesn't.

标签: c++ string boost boolean


【解决方案1】:

一种快速的方法是使用预初始化的map<string,bool>,其中包含所有可接受的字符串及其布尔等价物。那么只需做find.

【讨论】:

    猜你喜欢
    • 2018-04-02
    • 2014-01-17
    • 2019-02-09
    • 1970-01-01
    • 2013-04-25
    • 1970-01-01
    • 2011-04-27
    • 2023-03-28
    • 2019-12-28
    相关资源
    最近更新 更多