zoukankan      html  css  js  c++  java
  • boost之字符串与文本处理

    C++标准库中字符串转数值使用函数atoi(),数值转字符串使用printf系列函数。

    boost中使用转换函数操作符lexical_cast<>进行转换,实际上是模板函数。自定义类型,要进行转换必须支持输入输出操作符<< >>。

    #include <iostream>
    #include <boost/lexical_cast.hpp>
    using namespace std;
    using namespace boost;
    
    int main()
    try
    {
    	int x = lexical_cast<int>("100");
    	long y = lexical_cast<long>("2000L");
    	float pai = lexical_cast<float>("3.14159e5");
    	double e = lexical_cast<double>("2.71828");
    	cout << x <<endl<< y<<endl << endl<<pai <<endl<< e <<endl;
    
    	string str = lexical_cast<string>(456);
    	cout << str << endl;
    	
    }
    catch(bad_lexical_cast& e)
    {
    	cout <<"error:" << e.what() <<endl;
    }
    

     2.格式转换format,其实输入输出分三部分,一个类型转换,一个格式转换,一个输出。format用于格式转换接受用于转换的格式字符串产生format对象,使用重载操作符%进行格式转换。

    #include <iostream>
    #include <boost/format.hpp>
    using namespace std;
    using namespace boost;
    
    int main()
    {
    	format fmt("%05d
    %-8.3f
    %10s
    %05X
    ");
    	cout << fmt %62 %2.236%"123456789"%48;
    	return 0;
    }
    
  • 相关阅读:
    RedisDump安装报错
    安装mysql解压版时遇到的错误
    Docker 私有仓库 Harbor搭建与使用
    最好的6个Go语言Web框架
    安裝 drone CLI和使用drone
    使用 Kubernetes Helm 安装 Drone
    从ELK到EFK演进
    搭建helm私服ChartMuseum
    Helm3的使用
    Helm3部署安装
  • 原文地址:https://www.cnblogs.com/liuweilinlin/p/3261188.html
Copyright © 2011-2022 走看看