关于string字符串和char字符的拼接,运算及实例演示(简单易懂)

一、直接代码演示吧

#include<iostream> 
#include<algorithm>
using namespace std;
int main(){
	char a = '9';
	char b = '3';
	
	string s1 = "67";
	string s2 = "3";
	string a1 = "98";
	//数值运算操作 
	a1[0] = (a - b) + '0';
	//拼接操作 
	s1 = '1' + s1;//"1" + s1也行单引号双引号没关系,如果是多字符就必须使用双引号 
	cout << "a1 = " << a1 << endl;
	cout << "s1 = "<< s1 << endl;
	
	s2 = '1' + s2;
	cout << "s2 = "<< s2 << endl;
	
	char c = (a - b) + '0';//得到字符间的差值 
	printf("c = %c\n", c);
	
	//将数值转化为字符进性拼接 
	int num = 99;
	string str = "432";
	str = str + to_string(num);
	cout << "str = "<< str; 
	return 0;
}

posted @ 2020-06-16 07:40  睿晞  阅读(7197)  评论(0编辑  收藏  举报