字符串类型的数字转为相应的数字
2013-10-13 18:34
330 查看
字符如何隐式的转换为数字:一个字符型的数字减去‘0’后就会隐式地转换为数字,一个数字加上‘0’后就会隐式地转换为字符。
intnum='9'-'0';
chara=9+'0';
int inta=9+'0';
cout<<"num:"<<num<<endl;
cout<<"a:"<<a<<endl;
cout<<"inta:"<<inta<<endl;
string to number
Simple functionsto do these conversions
缺点:
1) 内存风险
#include<cstdlib>
#include<iostream>
#include<sstream>
usingnamespace std;
intmain(int argc, char *argv[])
{
std::stringstream stream;
string str;
while(1)
{
//clear(),这个名字让很多人想当然地认为它会清除流的内容。
//实际上,它并不清空任何内容,它只是重置了流的状态标志而已!
stream.clear();
// 去掉下面这行注释,清空stringstream的缓冲,每次循环内存消耗将不再增加!
//stream.str("");
stream<<"sdfsdfdsfsadfsdafsdfsdgsdgsdgsadgdsgsdagasdgsdagsadgsdgsgdsagsadgs";
stream>>str;
// 去掉下面两行注释,看看每次循环,你的内存消耗增加了多少!
//cout<<"Size of stream= "<<stream.str().length()<<endl;
//system("PAUSE");
}
system("PAUSE");(不应该使用,可以用其他语句代替,如cin.get())。
return EXIT_SUCCESS;
} 把stream.str(""); 那一行的注释去掉,再运行程序,内存就正常了
看来stringstream似乎不打算主动释放内存(或许是为了提高效率),但如果你要在程序中用同一个流,反复读写大量的数据,将会造成大量的内存消耗,因些这时候,需要适时地清除一下缓冲 (用
stream.str("") )。
另外不要企图用stream.str().resize(0),或stream.str().clear() 来清除缓冲,使用它们似乎可以让stringstream的内存消耗不要增长得那么快,但仍然不能达到清除stringstream缓冲的效果(不信做个实验就知道了,内存的消耗还在缓慢的增长!),至于stream.flush(),则根本就起不到任何作用。
char *str)
{
long num =
0;
int neg =
0;
while (isspace(*str)) str++;
if (*str ==
'-')
{
neg=1;
str++;
}
while (isdigit(*str))
{
num = 10*num + (*str -
'0');
str++;
}
if (neg)
num = -num;
return num;
}
intnum='9'-'0';
chara=9+'0';
int inta=9+'0';
cout<<"num:"<<num<<endl;
cout<<"a:"<<a<<endl;
cout<<"inta:"<<inta<<endl;
1.1 使用stringstream
number to string1 2 3 4 5 6 7 8 9 10 | int Number = 123;//number to convert int a string string Result;//string which will contain the result stringstream convert; // stringstream used for the conversion convert << Number;//add the value of Number to the characters in the stream Result = convert.str();//set Result to the content of the stream //Result now is equal to "123" |
1 2 3 4 5 6 7 8 | string Text = "456";//string containing the number int Result;//number which will contain the result stringstream convert(Text); // stringstream used for the conversion initialized with the contents of Text if ( !(convert >> Result) )//give the value to Result using the characters in the string Result = 0;//if that fails set Result to 0 //Result now equal to 456 |
1 2 3 4 5 6 7 | template <typename T> string NumberToString ( T Number ) { stringstream ss; ss << Number; return ss.str(); } |
1 2 3 4 5 6 7 | template <typename T> T StringToNumber ( const string &Text )//Text not by const reference so that the function can be used with a { //character array as argument stringstream ss(Text); T result; return ss >> result ? result : 0; } |
1) 内存风险
#include<cstdlib>
#include<iostream>
#include<sstream>
usingnamespace std;
intmain(int argc, char *argv[])
{
std::stringstream stream;
string str;
while(1)
{
//clear(),这个名字让很多人想当然地认为它会清除流的内容。
//实际上,它并不清空任何内容,它只是重置了流的状态标志而已!
stream.clear();
// 去掉下面这行注释,清空stringstream的缓冲,每次循环内存消耗将不再增加!
//stream.str("");
stream<<"sdfsdfdsfsadfsdafsdfsdgsdgsdgsadgdsgsdagasdgsdagsadgsdgsgdsagsadgs";
stream>>str;
// 去掉下面两行注释,看看每次循环,你的内存消耗增加了多少!
//cout<<"Size of stream= "<<stream.str().length()<<endl;
//system("PAUSE");
}
system("PAUSE");(不应该使用,可以用其他语句代替,如cin.get())。
return EXIT_SUCCESS;
} 把stream.str(""); 那一行的注释去掉,再运行程序,内存就正常了
看来stringstream似乎不打算主动释放内存(或许是为了提高效率),但如果你要在程序中用同一个流,反复读写大量的数据,将会造成大量的内存消耗,因些这时候,需要适时地清除一下缓冲 (用
stream.str("") )。
另外不要企图用stream.str().resize(0),或stream.str().clear() 来清除缓冲,使用它们似乎可以让stringstream的内存消耗不要增长得那么快,但仍然不能达到清除stringstream缓冲的效果(不信做个实验就知道了,内存的消耗还在缓慢的增长!),至于stream.flush(),则根本就起不到任何作用。
1.2 自定义字符串转数字
long atoi(constchar *str)
{
long num =
0;
int neg =
0;
while (isspace(*str)) str++;
if (*str ==
'-')
{
neg=1;
str++;
}
while (isdigit(*str))
{
num = 10*num + (*str -
'0');
str++;
}
if (neg)
num = -num;
return num;
}
相关文章推荐
- Java 将数字转为16进制,然后转为字符串类型
- 7进制字符串转为10进制的int类型数字(H3C笔试题)
- 数字字符串转为相应的浮点数
- Java 将数字转为16进制,然后转为字符串类型 将空格去掉。终结版
- delphi实现字符串中数字的判断及相应数字累加
- python字符串与数字类型转化
- 已知:yi er san si wu liu qi ba jiu 分别对应123456789,对一段只含有这几种字符的字符串进行转 换,转换成相应的数字
- python实操(5):python数据类型及常用操作,数字、字符串、元组、列表、字典、集合
- 把一个字符串通过递归转换成相应数字
- 字符串转为Map类型:split()方法的应用
- 字符串转为数字
- 数字操作 转为false的类型 typeof操作符 isNaN函数
- java中数字与字符串类型间的转换
- 02-Python的核心数据类型-数字、字符串
- DataGrid、GridView等控件的Control无法转为相应类型的问题
- DB2数字类型转换成字符串类型,例:ORACLE与DB2
- java正则表达式——判断字符串为哪种类型的数字?
- VC中常用的字符类型及相应的字符串处理函数
- 判断C#中的字符串是否是数字,如果是转换成int类型
- java判断字符串是否可以转为数字