数字与 string ,四种指针转换,数字转字符,char\rightarrowstring

# 数字转 string

# 使用标准库函数 std::to_string()

std 命令空间下有一个 C++ 标准库函数 std::to_string() ,可用于将数值类型转换为 string 。使用时需要 include 头文件 <string>
函数原型申明如下:

string to_string (int val);
string to_string (long val);
string to_string (long long val);
string to_string (unsigned val);
string to_string (unsigned long val);
string to_string (unsigned long long val);
string to_string (float val);
string to_string (double val);
string to_string (long double val);

# 使用函数模板 + ostringstream

使用函数模板将基本数据类型(整型、字符型、实型、布尔型)转换成 string。
相当于自定义一个 tostring

//ostringstream 对象用来进行格式化的输出,常用于将各种类型转换为 string 类型
//ostringstream 只支持<<操作符
template<typename T> string toString(const T& t){
    ostringstream oss;  //创建一个格式化输出流
    oss<<t;             //把值传递如流中
    return oss.str();   
}
cout<<toString(14.2)<<endl;  //实型->string:输出 14.2
cout<<toString(12301)<<endl; //整型->string:输出 12301
cout<<toString(123456789785)<<endl;//长整型->string:输出 123456789785
cout<<toString(true)<<endl;  //布尔型->string:输出 1

# string 转为数字

# 使用 C 标准库函数

具体做法是先将 string 转换为 char * 字符串,再通过相应的类型转换函数转换为想要的数值类型。需要包含标准库函数 <stdlib.h>
(1)string 转换为 int32_t

string love="77";
int ilove=atoi(love.c_str());
//或者 16 位平台转换为 long int
int ilove=strtol(love.c_str(),NULL,10);

(2)string 转换为 uint32_t

//str: 待转换字符串
//endptr: 指向 str 中数字后第一个非数字字符
//base:转换基数(进制),范围从 2 至 36
unsigned long int strtoul (const char* str, char** endptr, int base);
#示例
string love="77";
unsigned long ul;
ul = strtoul(love.c_str(), NULL, 10);

(3)string 转换为 int64_t

string love="77";
long long llLove=atoll(love.c_str());

(4)string 转换为 uint64_t

unsigned long long int strtoull (const char* str, char** endptr, int base);
#示例
string love="77";
unsigned long long ull;
ull = strtoull (love.c_str(), NULL, 0);

(5)string 转换为 float 或 double

string love="77.77";
float fLove=atof(love.c_str());
double dLove=atof(love.c_str());

(6)string 转换为 long double

long double strtold (const char* str, char** endptr);

# 使用函数模板 + istringstream

stringstream 在 int 或 float 类型转换为 string 类型的方法中已经介绍过, 这里也能用作将 string 类型转换为常用的数值类型。

#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()  {  
    string str("00801");  
    cout << stringToNum<int>(str) << endl;  
    system("pause");  
    return 0;  
}  

# 使用 C++ 标准库函数

使用 C++11 引入的 C++ 库函数将 string 转换为数值类型,相应的库函数申明于头文件 <string> 中。

名称 原型 说明
stoi int stoi (const string& str, size_t* idx = 0, int base = 10);int stoi (const wstring& str, size_t* idx = 0, int base = 10); 将 string 转换为 int
stol long stol (const string& str, size_t* idx = 0, int base = 10);long stol (const wstring& str, size_t* idx = 0, int base = 10); 将 string 转为 long int
stoul unsigned long stoul (const string& str, size_t* idx = 0, int base = 10);unsigned long stoul (const wstring& str, size_t* idx = 0, int base = 10); unsigned int
stoll long long stoll (const string& str, size_t* idx = 0, int base = 10);long long stoll (const wstring& str, size_t* idx = 0, int base = 10); string to long long
stoull unsigned long long stoull (const string& str, size_t* idx = 0, int base = 10);unsigned long long stoull (const wstring& str, size_t* idx = 0, int base = 10); string to unsigned long long
stof float stof (const string& str, size_t* idx = 0);float stof (const wstring& str, size_t* idx = 0); string to float (function template )
stod double stod (const string& str, size_t* idx = 0);double stod (const wstring& str, size_t* idx = 0); string to double
stold long double stold (const string& str, size_t* idx = 0);long double stold (const wstring& str, size_t* idx = 0); string to long double (function template)

形参说明:

str: 重载了 string 和 wstring 版本,表示被转换的字符串。

base: 表示转换基准,默认是 10 进制。

idx: 表示一个 size_t* 的指针类型,默认为空值。不为空时,转换成功时获取第一个非数值字符的下标。一般情况下,因为它是直接 char 型指针把最后非数值字符的地址值和起始地址值相减,所以也表示成功转换的字符数量,如”10” 转成功为数值 10 时,*idx 的值为 2。

# 四种指针的转换方式

# static_cast:

可以实现 C++ 中内置基本数据类型之间的相互转换,enum、struct、 int、char、float 等。它不能进行无关类型(如非基类和子类)指针之间的转换。

int c=static_cast<int>(7.987);

如果涉及到类的话,static_cast 只能在有相互联系的类型中进行相互转换,不一定包含虚函数。

# const_cast:

const_cast 操作不能在不同的种类间转换。相反,它仅仅把一个它作用的表达式转换成常量。它可以使一个本来不是 const 类型的数据转换成 const 类型的,或者把 const 属性去掉。

# reinterpret_cast:

interpret 是解释的意思,reinterpret 即为重新解释,此标识符的意思即为数据的二进制形式重新解释,但是不改变其值。)有着和 C 风格的强制转换同样的能力。它可以转化任何内置的数据类型为其他任何的数据类型,也可以转化任何指针类型为其他的类型。它甚至可以转化内置的数据类型为指针,无须考虑类型安全或者常量的情形。不到万不得已绝对不用。

# dynamic_cast:

(1)其他三种都是编译时完成的,dynamic_cast 是运行时处理的,运行时要进行类型检查。
(2)不能用于内置的基本数据类型的强制转换。
(3)dynamic_cast 转换如果成功的话返回的是指向类的指针或引用,转换失败的话则会返回 NULL。
(4)使用 dynamic_cast 进行转换的,基类中一定要有虚函数,否则编译不通过。
需要检测有虚函数的原因:类中存在虚函数,就说明它有想要让基类指针或引用指向派生类对象的情况,此时转换才有意义。
这是由于运行时类型检查需要运行时类型信息,而这个信息存储在类的虚函数表(关于虚函数表的概念,详细可见 <Inside c++ object model>)中,
只有定义了虚函数的类才有虚函数表。
(5) 在类的转换时,在类层次间进行上行转换时,dynamic_cast 和 static_cast 的效果是一样的。在进行下行转换 时,dynamic_cast 具有类型检查的功能,比 static_cast 更安全。向上转换即为指向子类对象的向下转换,即将父类指针转化子类指针。向下转换的成功与否还与将要转换的类型有关,即要转换的指针指向的对象的实际类型与转换以后的对象类型一定要相同,否则转换失败。

# int 转 char

写 leetcode 1002 的时候遇到了字母表的映射

统计每一个字母出现的次数后,经过部分处理还要继续使用这些数字所对应的字符

所以就遇到了 int 转 char 的需求,方法如同 char 转 int 一样简单

int i=6;
char c = '0'+i;//now c is '6'
result.push_back(c);

# char 转 string

 const char c = 'a';
//1.使用 string 的构造函数
string s(1,c);
//2.声明string 后将char push_back
string s1;
s1.push_back(c);
//3.使用stringstream
stringstream ss;
ss << c;
string str2 = ss.str();
//注意 使用to_string 方法会转化为char对应的ascii码
//原因是 to_string 没有接受char型参数的函数原型,有一个参数类型
//为int 的函数原型,所以传入char型字符 实际是先将char 转化
//为int 型的ascii 码,然后再转变为string
//以下输出结果为 97
cout << to_string(c) << endl;