c++ string和其他类型互转

C++数值类型与string的相互转换https://www.cnblogs.com/johngu/p/7878029.html

C++ string的万能转换,从long string 之间的转换来看看:https://blog.csdn.net/Vic___/article/details/9324897

c++11

1.数值类型转换为string

1.1使用函数模板+ostringstream

使用函数模板将基本数据类型(整型、字符型、实型、布尔型)转换成string。

//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

1.2使用标准库函数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);

2.string转换为数值类型

2.1使用函数模板+ 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(int argc, char* argv[])  { 
    string str("00801"); 
    cout << stringToNum<int>(str) << endl; 
 
    system("pause"); 
    return 0; 
} 

2.2使用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转换为uint64_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);

2.3使用C++标准库函数

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

名称原型说明
stoiint stoi (const string& str, size_t* idx = 0, int base = 10);
int stoi (const wstring& str, size_t* idx = 0, int base = 10);
Convert string to integer (function template )
stollong stol (const string& str, size_t* idx = 0, int base = 10);
long stol (const wstring& str, size_t* idx = 0, int base = 10);
Convert string to long int (function template)
stoulunsigned 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);
Convert string to unsigned integer (function template)
stolllong 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);
Convert string to long long (function template)
stoullunsigned 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);
Convert string to unsigned long long (function template)
stoffloat stof (const string& str, size_t* idx = 0);
float stof (const wstring& str, size_t* idx = 0);
Convert string to float (function template )
stoddouble stod (const string& str, size_t* idx = 0);
double stod (const wstring& str, size_t* idx = 0);
Convert string to double (function template )
stoldlong double stold (const string& str, size_t* idx = 0);
long double stold (const wstring& str, size_t* idx = 0);
Convert string to long double (function template)

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

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

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

string 转 *

* sto*(string str) //改一下函数名,变量类型,搞定
{
    * result;
    istringstream is(str);
    is >> result;
    return result;
}

* 转string

string *tos(* i)     //改一下函数名,改一下类型,搞定
{
    ostringstream os;
    os<<i;
    string result;
    istringstream is(os.str());
    is>>result;
    return result;
 
}

将*换成想要的类型就可以执行 *转string

 

string 转 int

int stoi(string str)
{
    int result;
    istringstream is(str);
    is >> result;
    return result;
}

string 转float 

float stof(string str)
{
    float result;
    istringstream is(str);
    is >> result;
    return result;
}

string 转double

double stod(string str)
{
    double result;
    istringstream is(str);
    is >> result;
    return result;
}

int 转 string

string itos(int i)
{
    ostringstream os;
    os<<i;
    string result;
    istringstream is(os.str());
    is>>result;
    return result;
 
}

float 转 string

string ftos(float f)
{
    ostringstream os;
    os<<f;
    string result;
    istringstream is(os.str());
    is>>result;
    return result;
 
}

double 转 string

string dtos(double d)
{
    ostringstream os;
    os<<d;
    string result;
    istringstream is(os.str());
    is>>result;
    return result;
 
}

string 转 long 

atoi()在<stdlib.h>,配合c_str()

#include <string>
#include <iostream>
#include <stdlib.h>
using namespace std;
int main ()
{
    string a = "1234567890";
    long b = atoi(a.c_str());
    cout<<b<<endl;
    return 0;
}

long 转 string 

string ltos(long l)
{
    ostringstream os;
    os<<l;
    string result;
    istringstream is(os.str());
    is>>result;
    return result;
 
}

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值