提升:如何将像“true”或“1”这样的字符串转换为bool true,将“0”或“false”转换为bool false?

时间:2011-10-28 00:32:21

标签: c++ string boost boolean

boost::lexical_cast<bool, std::string>( string );只有True选项,还是有更快的方式来执行此类操作,它是否适用于1字符串,它是否适用于TRUE字符串,它是否适用于{{1}字符串?

(因为对我而言,它会给出bad lexical cast: source type value could not be interpreted as target错误)

1 个答案:

答案 0 :(得分:5)

一种快速的方法是预先初始化map<string,bool>,其中包含所有可接受的字符串及其布尔值。那么这只是做find的问题。