boost库之字符串与文本处理——lexical_cast
来源:互联网 发布:防护骷髅面具淘宝 编辑:程序博客网 时间:2024/06/06 13:19
#include <iostream>#include <string>#include <vector>#include <set>#include <map>#include <algorithm>#include <boost/lexical_cast.hpp>#include <boost/assert.hpp>using namespace boost;using namespace std;template<typename T>bool num_valid(const char* str){ try { lexical_cast<T>(str); //尝试转换数字 return true; }catch(bad_lexical_cast&){ return false; }}class demo_class{friend std::ostream& operator<<(std::ostream& os,const demo_class& x){ os << "demo_class's Name"; return os;}};int main( int argc,char **argv){ /*常用操作*/ int x = lexical_cast<int>("100"); long y = lexical_cast<long>("2000"); float pai = lexical_cast<float>("3.14159e5"); double e = lexical_cast<double>("2.71828"); cout << x << y << pai << e << endl; string str = lexical_cast<string>(456);; cout << str << endl; cout << lexical_cast<string>(0.618) << endl; cout << lexical_cast<string>(0x10) << endl; /*抛出异常*/ try { cout << lexical_cast<int>("0x100"); cout << lexical_cast<double>("helloworld"); cout << lexical_cast<bool>("false"); cout << lexical_cast<long>("1000L"); }catch (bad_lexical_cast& e) { cout <<"error:" << e.what()<< endl; } /*验证字符串的合法性*/ assert(num_valid<double>("3.14")); assert(!num_valid<int>("3.14")); assert(num_valid<int>("65535")); cout << lexical_cast<string>("demo_class") << endl; return (0);}/*注意: *1.lexical_cast转换成数字的字符串只能有数字和小数点,不能出现除(表示e/E除外)字母或其他非数字字符 *2.它不支持高级的格式控制,如有需要应使用std::stringstream或者boost::format *3.可以转换bool类型,不能使用true/false.只能使用1或0 *4.*//*对转换对象的要求: * 1.转换起点对象是可流输出的,即定义了operator<<; * 2.转换终点对象是可流输入的,即定义了operator>>; * 3.转换终点对象必须是可缺省构造和可拷贝的 * */
0 0
- boost库之字符串与文本处理——lexical_cast
- boost 字符串与文本处理 -> lexical_cast
- boost之lexical_cast库,数值/字符串转换及其异常处理
- boost——字符串与文本处理string_algo(一)
- boost——字符串与文本处理format
- boost——字符串与文本处理string_algo(二)
- boost——字符串与文本处理tokenizer
- [Boost基础]字符串和文本处理——lexical_cast词汇转换
- boost库学习之 lexical_cast
- boost split和lexical_cast配合使用处理文本
- 利用boost库进行字符串与文本处理
- Boost学习笔记 -- 字符串与文本处理
- BOOST库 之 lexical_cast类型转换
- 学习boost之lexical_cast
- boost学习之-lexical_cast
- boost之lexical_cast
- Boost 之 lexical_cast
- boost学习之lexical_cast
- 编写jQueryUI插件(widget)
- Yarn的组成部分
- redis.conf 配置详解
- Hydra的介绍
- MFC控件edit封装,只能输入数字、负号、小数点
- boost库之字符串与文本处理——lexical_cast
- 常用 Java 静态代码分析工具的分析与比较
- ExpandableListView--可展开的列表组件
- 3.1手写数字识别:MNIST,Perceptron
- Linux电源驱动-Linux Cpuidle Framework
- Head First设计模式——1、设计模式入门
- 【快速傅立叶变换fft&数论变换ntt学习小记】
- CCS加载mathlib的解决办法
- 程序员面试宝典--session和cookie