lexical-cast

How do I use boost::lexical_cast and std::boolalpha? i.e. boost::lexical_cast< bool >(“true”)

落花浮王杯 提交于 2019-11-29 06:02:35
问题 I've seen some answers to other boost::lexical_cast questions that assert the following is possible: bool b = boost::lexical_cast< bool >("true"); This doesn't work for me with g++ 4.4.3 boost 1.43. (Maybe it's true that it works on a platform where std::boolalpha is set by default) This is a nice solution to the string to bool problem but it lacks input validation that boost::lexical_cast provides. 回答1: I'm posting the answer to my own question here for others who may be looking for

How can I extend a lexical cast to support enumerated types?

别说谁变了你拦得住时间么 提交于 2019-11-27 07:07:16
I have the following function that will convert a string into a numeric data type: template <typename T> bool ConvertString(const std::string& theString, T& theResult) { std::istringstream iss(theString); return !(iss >> theResult).fail(); } This does not work for enumerated types, however, so I have done something like this: template <typename T> bool ConvertStringToEnum(const std::string& theString, T& theResult) { std::istringstream iss(theString); unsigned int temp; const bool isValid = !(iss >> temp).fail(); theResult = static_cast<T>(temp); return isValid; } (I'm making the assumption

Very poor boost::lexical_cast performance

爷,独闯天下 提交于 2019-11-26 19:27:50
Windows XP SP3. Core 2 Duo 2.0 GHz. I'm finding the boost::lexical_cast performance to be extremely slow. Wanted to find out ways to speed up the code. Using /O2 optimizations on visual c++ 2008 and comparing with java 1.6 and python 2.6.2 I see the following results. Integer casting: c++: std::string s ; for(int i = 0; i < 10000000; ++i) { s = boost::lexical_cast<string>(i); } java: String s = new String(); for(int i = 0; i < 10000000; ++i) { s = new Integer(i).toString(); } python: for i in xrange(1,10000000): s = str(i) The times I'm seeing are c++: 6700 milliseconds java: 1178 milliseconds

How can I extend a lexical cast to support enumerated types?

与世无争的帅哥 提交于 2019-11-26 13:03:44
问题 I have the following function that will convert a string into a numeric data type: template <typename T> bool ConvertString(const std::string& theString, T& theResult) { std::istringstream iss(theString); return !(iss >> theResult).fail(); } This does not work for enumerated types, however, so I have done something like this: template <typename T> bool ConvertStringToEnum(const std::string& theString, T& theResult) { std::istringstream iss(theString); unsigned int temp; const bool isValid = !

Very poor boost::lexical_cast performance

 ̄綄美尐妖づ 提交于 2019-11-26 06:59:31
问题 Windows XP SP3. Core 2 Duo 2.0 GHz. I\'m finding the boost::lexical_cast performance to be extremely slow. Wanted to find out ways to speed up the code. Using /O2 optimizations on visual c++ 2008 and comparing with java 1.6 and python 2.6.2 I see the following results. Integer casting: c++: std::string s ; for(int i = 0; i < 10000000; ++i) { s = boost::lexical_cast<string>(i); } java: String s = new String(); for(int i = 0; i < 10000000; ++i) { s = new Integer(i).toString(); } python: for i