学习使用点滴-1-字符处理

来源:互联网 发布:ubuntu 安装 py2exe 编辑:程序博客网 时间:2024/04/29 11:06

1.字符转换
 1.1 相关类
   lexical_cast
   bad_lexical_cast
 1.2 定义文件#include <boost/lexical_cast.hpp>
 1.3 功能简介

   用来进行类型转换的,字符串与数值之间的转换
 1.4 使用举例:
  Eclipse 3.0, GUN G++ , Winxp测试通过。
  #include <iostream>
  #include <boost/lexical_cast.hpp>

  using namespace std;

  main()
  {
   int i=100;
   char* pstr = "10101";
 
   cout<<"Before convert i is:"<<i<<endl;
   i = boost::lexical_cast< int >(pstr);
   cout<<"After convert i is:"<<i<<endl;
 
   return 0;
  }
  //print: Before convert i is:100
  //     After convert i is:10101
 
 1.5 其他:
  转换的异常处理和一般的异常处理相同。
  如:try
        {
            //进行转换
        }
        catch(bad_lexical_cast &)
        {
            //处理异常
        }
   
 2.格式化字符串
 2.1 相关类
   format
 2.2 定义文件:#include <boost/format.hpp>
 2.3 功能简介:
   构建一个格式化的字符串,其中的某些字符是待定的,其可以以参数的形式写入。
 2.4 使用举例:
    Eclipse 3.0, GUN G++ , Winxp测试通过。
    #include <iostream>
    #include <boost/format.hpp>
    #include <string>

    using namespace std;
  
    using boost::format;
    using boost::io::group;
    int main()
    {
         format fmt("%1% %2% %3% %4% %5% %6%/n");
         //使用%1%进行参数替换,1代表第一个参数
         string str;
         int i=100;
         cout<<fmt  % 'g' % 'a' % 'l' % 'p' % 'h' % 'y'<<endl;
         //%操作符压入参数
         string s_tmp =boost::str(fmt);
         //str()自由函数,fomat->string
         //同样也可使用format.str();

         cout << s_tmp;
         return 0;
    }
 2.5 格式说明:
    “$“的使用:后面加printf的格式控制符
    “|“的使用:简化printf的格式控制符
    1.无参数顺序:
        format(“%5d %4s %5f“); //%后面直接加printf的格式控制符,参数顺序传入
        format(“%|5| %|4| %|5|);//效果同上,自动判断d,s,f等控制符号
    2.有参数顺序:
        format(“%1% %2% %1%“); 
        format(“%1$5d  %2$5s %1$5d);
        format(“%|1$5|  %|2$5| %|1$5|);//效果同上,自动判断d,s,f等控制符号
    3.具体格式:

        [ N$ ] [ flags ] [ 宽度] [ . 精度] type-char
                    

  • FlagMeaningeffect on internal stream
    '-'left alignmentN/A (applied later on the string)
    '='centered alignmentN/A (applied later on the string)
    - note : added feature, not in printf -
    '_'internal alignmentsets internal alignment
    - note : added feature, not in printf -
    '+'show sign even for positive numberssets showpos
    '#'show numerical base, and decimal pointsets showbase and showpoint
    '0'pad with 0's (inserted after sign or base indicator)if not left-aligned, calls setfill('0') and sets internal
    Extra actions are taken after stream conversion to handle user-defined output.
    ' 'if the string does not begin with + or -, insert a space before the converted stringN/A (applied later on the string)
    Different to printf's behaviour : it is not affected by internal alignment
  • Type-CharMeaningeffect on stream
    p or xhexadecimal outputsets hex
    ooctal outputsets oct
    escientific float formatsets floatfield bits to scientific
    ffixed float formatsets floatfield bits to fixed
    ggeneral -default- float formatunset all floatfield bits
    X, E or Gsame effect as their lowercase counterparts, but using uppercase letters for number outputs. (exponents, hex digits, ..)same effects as 'x', 'e', or 'g', plus uppercase
    d, i or udecimal type outputsets basefield bits to dec
    s or Sstring outputprecision specification is unset, and its value goes to an internal field for later 'truncation'. (see precision explanation above)
    c or C1-character outputonly the first character of the conversion string is used.
    %print the character %N/A
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 个税密码忘记了怎么办 地税网上申报密码忘记了怎么办 国税银行缴款凭证丢失了怎么办 税务局让法人缴税法人不缴会怎么办 税盘忘记清卡怎么办 7年没申报地税怎么办 已报税忘了清卡怎么办 国税报税密码忘了怎么办 有一个月忘报税了怎么办 地税报税密码忘了怎么办 电子税务账号忘记了怎么办 国税ukey密码忘了怎么办 电子税务局显示版本低怎么办 电子税务局2.0不显示打印怎么办 青海建筑三级资质初申是怎么办 欠钱败诉没钱还怎么办 学校退款卡丢了怎么办 学校发的卡掉了怎么办 汇款回执单丢了怎么办 大学交学费的卡丢了怎么办 交学费的银行卡丢了怎么办 学校补助卡丢了怎么办 学校交学费的卡丢了怎么办 采购零星材料无发票怎么办 租房合同弄丢了怎么办 买房的合同丢了怎么办 押金的收据丢了怎么办 房东的合同掉了怎么办 个人档案里单位没有放合同怎么办 签的合同掉了怎么办 一方合同弄丢了怎么办 合同丢了怎么办如何补 签了定金合同对方违约怎么办 医学出生证明丢了怎么办 易通行出站未刷怎么办 炭烧酸奶过期了怎么办 西安建行etc坏了怎么办 电机在设备壳体中拔不出来怎么办 公司变更股东不能亲临现场怎么办? 公司股东变更老股东不签字怎么办 公司变更地址股东不签字怎么办