C++中将string类型转换为int, float, double类型 主要通过以下几种方式:
2015-08-22 13:41
696 查看
C++中将string类型转换为int, float, double类型 主要通过以下几种方式:
# 方法一: 使用stringstream
stringstream在int或float类型转换为string类型的方法中已经介绍过, 这里也能用作将string类型转换为常用的数值类型。
Demo:
[cpp] view
plaincopy
#include <iostream>
#include <sstream> //使用stringstream需要引入这个头文件
using namespace std;
//模板函数:将string类型变量转换为常用的数值类型(此方法具有普遍适用性)
template <class Type>
Type stringToNum(const string& str)
{
istringstream iss(str);
Type num;
iss >> num;
return num;
}
int main(int argc, char* argv[])
{
string str("00801");
cout << stringToNum<int>(str) << endl;
system("pause");
return 0;
}
输出结果:
#方法二:使用atoi()、 atil() 、atof()函数 -----------------实际上是char类型向数值类型的转换
注意:使用 atoi 的话,如果 string s 为空,返回值为0.则无法判断s是0还是空
1. atoi(): int atoi ( const char * str );
说明:Parses the C string str interpreting its content as an integral number, which is returned as an int value.
参数:str : C string beginning with the representation of an integral number.
返回值:1. 成功转换显示一个Int类型的值. 2. 不可转换的字符串返回0. 3.如果转换后缓冲区溢出,返回 INT_MAX orINT_MIN
Demo:
[cpp] view
plaincopy
#include <iostream>
using namespace std;
int main ()
{
int i;
char szInput [256];
cout<<"Enter a number: "<<endl;
fgets ( szInput, 256, stdin );
i = atoi (szInput);
cout<<"The value entered is :"<<szInput<<endl;
cout<<" The number convert is:"<<i<<endl;
return 0;
}
输出:
2.aotl(): long int atol ( const char * str );
说明:C string str interpreting its content as an integral number, which is returned as a long int value(用法和atoi函数类似,返回值为long int)
3.atof(): double atof ( const char * str );
参数:C string beginning with the representation of a floating-point number.
返回值:1. 转换成功返回doublel类型的值 2.不能转换,返回0.0。 3.越界,返回HUGE_VAL
Demo:
[cpp] view
plaincopy
/* atof example: sine calculator */
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main ()
{
double n,m;
double pi=3.1415926535;
char szInput [256];
printf ( "Enter degrees: " );
gets ( szInput );
//char类型转换为double类型
n = atof ( szInput );
m = sin (n*pi/180);
printf ( "The sine of %f degrees is %f\n" , n, m );
return 0;
}
C++中将string类型转换为int, float, double类型 主要通过以下几种方式:
# 方法一: 使用stringstream
stringstream在int或float类型转换为string类型的方法中已经介绍过, 这里也能用作将string类型转换为常用的数值类型。
Demo:
[cpp] view
plaincopy
#include <iostream>
#include <sstream> //使用stringstream需要引入这个头文件
using namespace std;
//模板函数:将string类型变量转换为常用的数值类型(此方法具有普遍适用性)
template <class Type>
Type stringToNum(const string& str)
{
istringstream iss(str);
Type num;
iss >> num;
return num;
}
int main(int argc, char* argv[])
{
string str("00801");
cout << stringToNum<int>(str) << endl;
system("pause");
return 0;
}
输出结果:
#方法二:使用atoi()、 atil() 、atof()函数 -----------------实际上是char类型向数值类型的转换
注意:使用 atoi 的话,如果 string s 为空,返回值为0.则无法判断s是0还是空
1. atoi(): int atoi ( const char * str );
说明:Parses the C string str interpreting its content as an integral number, which is returned as an int value.
参数:str : C string beginning with the representation of an integral number.
返回值:1. 成功转换显示一个Int类型的值. 2. 不可转换的字符串返回0. 3.如果转换后缓冲区溢出,返回 INT_MAX orINT_MIN
Demo:
[cpp] view
plaincopy
#include <iostream>
using namespace std;
int main ()
{
int i;
char szInput [256];
cout<<"Enter a number: "<<endl;
fgets ( szInput, 256, stdin );
i = atoi (szInput);
cout<<"The value entered is :"<<szInput<<endl;
cout<<" The number convert is:"<<i<<endl;
return 0;
}
输出:
2.aotl(): long int atol ( const char * str );
说明:C string str interpreting its content as an integral number, which is returned as a long int value(用法和atoi函数类似,返回值为long int)
3.atof(): double atof ( const char * str );
参数:C string beginning with the representation of a floating-point number.
返回值:1. 转换成功返回doublel类型的值 2.不能转换,返回0.0。 3.越界,返回HUGE_VAL
Demo:
[cpp] view
plaincopy
/* atof example: sine calculator */
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main ()
{
double n,m;
double pi=3.1415926535;
char szInput [256];
printf ( "Enter degrees: " );
gets ( szInput );
//char类型转换为double类型
n = atof ( szInput );
m = sin (n*pi/180);
printf ( "The sine of %f degrees is %f\n" , n, m );
return 0;
}
# 方法一: 使用stringstream
stringstream在int或float类型转换为string类型的方法中已经介绍过, 这里也能用作将string类型转换为常用的数值类型。
Demo:
[cpp] view
plaincopy
#include <iostream>
#include <sstream> //使用stringstream需要引入这个头文件
using namespace std;
//模板函数:将string类型变量转换为常用的数值类型(此方法具有普遍适用性)
template <class Type>
Type stringToNum(const string& str)
{
istringstream iss(str);
Type num;
iss >> num;
return num;
}
int main(int argc, char* argv[])
{
string str("00801");
cout << stringToNum<int>(str) << endl;
system("pause");
return 0;
}
输出结果:
801 请按任意键继续. . . |
注意:使用 atoi 的话,如果 string s 为空,返回值为0.则无法判断s是0还是空
1. atoi(): int atoi ( const char * str );
说明:Parses the C string str interpreting its content as an integral number, which is returned as an int value.
参数:str : C string beginning with the representation of an integral number.
返回值:1. 成功转换显示一个Int类型的值. 2. 不可转换的字符串返回0. 3.如果转换后缓冲区溢出,返回 INT_MAX orINT_MIN
Demo:
[cpp] view
plaincopy
#include <iostream>
using namespace std;
int main ()
{
int i;
char szInput [256];
cout<<"Enter a number: "<<endl;
fgets ( szInput, 256, stdin );
i = atoi (szInput);
cout<<"The value entered is :"<<szInput<<endl;
cout<<" The number convert is:"<<i<<endl;
return 0;
}
输出:
Enter a number: 48 The value entered is : 48 The number convert is: 48 |
说明:C string str interpreting its content as an integral number, which is returned as a long int value(用法和atoi函数类似,返回值为long int)
3.atof(): double atof ( const char * str );
参数:C string beginning with the representation of a floating-point number.
返回值:1. 转换成功返回doublel类型的值 2.不能转换,返回0.0。 3.越界,返回HUGE_VAL
Demo:
[cpp] view
plaincopy
/* atof example: sine calculator */
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main ()
{
double n,m;
double pi=3.1415926535;
char szInput [256];
printf ( "Enter degrees: " );
gets ( szInput );
//char类型转换为double类型
n = atof ( szInput );
m = sin (n*pi/180);
printf ( "The sine of %f degrees is %f\n" , n, m );
return 0;
}
C++中将string类型转换为int, float, double类型 主要通过以下几种方式:
# 方法一: 使用stringstream
stringstream在int或float类型转换为string类型的方法中已经介绍过, 这里也能用作将string类型转换为常用的数值类型。
Demo:
[cpp] view
plaincopy
#include <iostream>
#include <sstream> //使用stringstream需要引入这个头文件
using namespace std;
//模板函数:将string类型变量转换为常用的数值类型(此方法具有普遍适用性)
template <class Type>
Type stringToNum(const string& str)
{
istringstream iss(str);
Type num;
iss >> num;
return num;
}
int main(int argc, char* argv[])
{
string str("00801");
cout << stringToNum<int>(str) << endl;
system("pause");
return 0;
}
输出结果:
801 请按任意键继续. . . |
注意:使用 atoi 的话,如果 string s 为空,返回值为0.则无法判断s是0还是空
1. atoi(): int atoi ( const char * str );
说明:Parses the C string str interpreting its content as an integral number, which is returned as an int value.
参数:str : C string beginning with the representation of an integral number.
返回值:1. 成功转换显示一个Int类型的值. 2. 不可转换的字符串返回0. 3.如果转换后缓冲区溢出,返回 INT_MAX orINT_MIN
Demo:
[cpp] view
plaincopy
#include <iostream>
using namespace std;
int main ()
{
int i;
char szInput [256];
cout<<"Enter a number: "<<endl;
fgets ( szInput, 256, stdin );
i = atoi (szInput);
cout<<"The value entered is :"<<szInput<<endl;
cout<<" The number convert is:"<<i<<endl;
return 0;
}
输出:
Enter a number: 48 The value entered is : 48 The number convert is: 48 |
说明:C string str interpreting its content as an integral number, which is returned as a long int value(用法和atoi函数类似,返回值为long int)
3.atof(): double atof ( const char * str );
参数:C string beginning with the representation of a floating-point number.
返回值:1. 转换成功返回doublel类型的值 2.不能转换,返回0.0。 3.越界,返回HUGE_VAL
Demo:
[cpp] view
plaincopy
/* atof example: sine calculator */
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main ()
{
double n,m;
double pi=3.1415926535;
char szInput [256];
printf ( "Enter degrees: " );
gets ( szInput );
//char类型转换为double类型
n = atof ( szInput );
m = sin (n*pi/180);
printf ( "The sine of %f degrees is %f\n" , n, m );
return 0;
}
相关文章推荐
- C++ int类型转换string类型
- C++常见面试
- C++面试常见题目问与答(汇总一)
- POJ C++程序设计 编程题#4:魔兽世界之一:备战
- 图解C++
- C#在C++编写的DLL中注册函数并回调
- C语言快排
- C++ Primer 5e chapter 12
- C++ Primer 5e chapter 11
- C++ 与 c# 模块 之间相互调用
- 栈的应用2——超级计算器(中缀与后缀表达式)C语言
- 栈的应用1——超级计算器(中缀与后缀表达式)C语言
- C++ 编程个人总结
- C++中 explicit的用法
- GCC在C语言中内嵌汇编 asm __volatile__ (2)
- C语言编程技巧----如何定义全局变量----->提高代码可移植性
- C和C++中的文件读入写出区别
- C++联合
- Effective C++ 条款8 别让异常逃离析构函数
- 为何C语言(的函数调用)需要堆栈,而汇编语言却不需要堆栈