C++中int、char和string之间的互相转换

因为经常会用到数字和字符的相互转换,下面进行一点总结

char与int间的相互准换
 



int main(){
    //int类型转换为char类型
	int num = 6;
	char c = num + '0';
	cout<<c<<endl;
	
    //char类型转换为int类型
	char c1 = '4';
	int num1 = c1 - '0';
	cout<<num1<<endl;
	
}

二者转换的数字范围不能超过10

 

string、char *、char[]相互转换

string转char*

主要有三种方法可以将str转换为char*类型,分别是:data()、c_str()、copy()。
其中,copy()可能会报安全性错误。

#include<iostream>
#include <thread>
#include <algorithm>
#include <string>
using namespace std;


int main(void)
{
	
	//data()方法 
	string str = "hello";
    //使用char * p=(char*)str.data()效果相同
    const char* p1 = str.data();
    cout<<p1<<endl;
    
    
    //c_str()方法
    str = "world" ;
    //使用char * p=(char*)str.c_str()效果相同
	const char *p2 = str.c_str();
	cout<<p2<<endl;
	
	//copy()方法
	str="world";
	char p3[50];
	//s.copy(cstr, n, pos)       从字符数组cstr的pos位置开始,复制n个字符到字符串s中
	str.copy(p3, 5, 0);         
	*(p3+5)= '\0';                 //添加结束符
    cout<<p3<<endl;

}

char * 转string

可以直接赋值。

string s;
char *p = "helloworld";
s = p;

string转char[]

string pp = "helloworld";
char p[20];
int i;
for( i=0;i<pp.length();i++)
    p[i] = pp[i];
p[i] = '\0';                  //添加结束符

char[]转string

string s;
char p[20] = "helloworld";
s = p;

char[]转char*

char pp[20] = "helloworld";
char* p = pp;

char*转char[]

主要有两种方法可以将char*转换为char[]类型,分别是:strcpy()、循环遍历。
其中,strcpy()可能会报安全性错误,自行解决即可。

#include<iostream>
#include <thread>
#include <algorithm>
#include <string>
#include <string.h>
using namespace std;


int main(void)
{
	
	//利用<string.h>库的strcpy()方法 
	char arr1[20];
	char* tmp1 = "helloworld";
	strcpy(arr1, tmp1);
    cout<<arr1<<endl;
    
    //循环遍历
    char arr2[20];
	char* tmp2 = "helloworld";
	int i = 0;
	while (*tmp2 != '\0')
		arr2[i++] = *tmp2++;
	arr2[i] = '\0';             //添加结束符
	cout<<arr2<<endl;
	
}

string与int的转换

int 转 string

#include<iostream>
#include <algorithm>
#include <string>
using namespace std;


int main(void)
{
	
	int a = 7;
	float b = 3.14;
	double c = 1.415649363;
	//利用string库函数中函数to_string 
	string a_str = to_string(a);
	string b_str = to_string(b);
	string c_str = to_string(c);
	
	cout << "a" << endl;
	cout << "b" << endl;
	cout << "c" << endl;
	
}

string 转 int

#include<iostream>
#include <thread>
#include <algorithm>
#include <string>
using namespace std;


int main(void)
{
	
	string num_str = "2142";
	//利用string库中的stoi函数 
	int num_int = stoi(num_str);
	cout<<num_int<<endl;
	
	//利用标准库中的atoi函数
	string s = "51426";
	int value = atoi(s.c_str());
	cout << value << endl;

	
}

 

参考链接:

https://blog.csdn.net/qq_30534935/article/details/82683643

https://blog.csdn.net/qq_30534935/article/details/82459448

https://blog.csdn.net/qq_30534935/article/details/82086379

  • 5
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值