从string中提取出数字

来源:互联网 发布:淘宝美白面膜排行榜 编辑:程序博客网 时间:2024/05/01 06:16

实例如下:

运行环境:VC6.0

//从string中提取出数字简单的方法,调用stringstream

#include <iostream>
#include <iomanip>
#include <string>
#include <sstream>

using namespace std;

int main( ) {
int a;
stringstream ss;

// ss << "There are "<< 9 <<" apples in my cart.";//<<操作符是内置类型流插入操作符的重载。
ss << 987654<< 'a'<<123;
cout << ss.str( )<< endl; //stringstream::str( ) 返回包含内容的字符串
ss >> a;//>>操作符是内置类型流输出操作符的重载。
cout << a<

ss.str("");//清空字符串
ss << showbase<< hex<< 16; //<< showbase<< hex表示以十六进制形式将16存入ss
cout << "ss = "<< ss.str( )<< endl;

ss.str("");
ss << 3.14;
cout << "ss = "<< ss.str( )<< endl;
return 0;

}

0 0
原创粉丝点击