编程语言

img owl2008

laxical_cast and stringstream

发表于2004/9/28 15:38:00  2010人阅读

分类: boost/STL

#include <iostream>

#include <sstream>

#include <string>

#include <cstdlib>

#include <boost/lexical_cast.hpp>

using namespace std;

using namespace boost;

int main(void)

try{

   //以下是内置类型向string转换的解决方案

   //lexical_cast优势明显

       int ival;

       char cval;

       ostringstream out_string;

       string str0;

       string str1;

      

       ival = 100;

       cval = 'w';

       out_string << ival << " " << cval;

       str0 = out_string.str();

       str1 = lexical_cast<string>(ival)

                    + lexical_cast<string>(cval);

       cout << str0 << endl;

       cout << str1 << endl;

 

    //以下是string向内置类型转换的解决方案

    //几乎和stringstrem相比,尚有不足,

    //不知道各位有何想法>?    

       int itmpe;

       char ctmpe;

       str0 = "100k";

       str1 = "100h";

    istringstream in_string( str0 );

       in_string >> itmpe >> ctmpe;

       cout << itmpe << " " << ctmpe << endl;

      itmpe = lexical_cast<int>(str1);

      ctmpe = lexical_cast<char>(str1);

       system( "PAUSE" );

       return 0;

}

catch(bad_lexical_cast e)

{

    cout << e.what() << endl;

    cin.get();

}       

阅读全文
0 0

相关文章推荐

img
取 消
img