C++ char、string、int、float 相互【类型转换】


char 数组 转 int 和 转 float

#include<iostream>
#include<string>
#include<sstream>
#include<cstring>
#include <stdlib.h>
#include <stdio.h>
using namespace std;
int main()
{

	char a[] = {'-','1','2','3'};
	int num1 = atoi(a);
	
	cout<<"char 转  int " << num1 <<endl;

	char b[] = {'3','.','1','4'};
	float fnum = atof(b);

	cout << "char 转 float " << fnum << endl;

}
  • 输出如下:
charint -123
charfloat 3.14

int 转 char 数组

#include<iostream>

using namespace std;

int main()
{
	int num3 = +234;
	char c1[40];
	sprintf(c1,"%d",num3);//按正常位数转换
	cout << c1 << endl;
 
	sprintf(c1, "%6d", num3);//指定6位,不足左边补空格
	cout << c1 << endl;
 
	sprintf(c1, "%06d", num3);//指定6位,不足左边补0
	cout << c1 << endl;
}

  • 输出如下:
234
   234
000234

float 和 string 互转 | char* 和 string 互转

#include<iostream>
#include<string>
#include<sstream>
#include<cstring>
#include <stdlib.h>
#include <stdio.h>
using namespace std;
int main()
{
	cout << "-------------- float  转 string ------------------" << endl << endl;
	float num5 = -2.35;
	ostringstream ss;
	ss << num5;
	string s1 = ss.str();
	cout << "num->string: " << s1 << endl;
 
	cout << "--------------- string 转 float ------------------" << endl << endl;
	
	istringstream iss("123.23");
	float num6;
	iss>>num6 ;
	cout << "string->num: " << num6 << endl;
	
	cout << "-----------------char*  转 string 直接赋值---------------------" << endl << endl;
	char *p1 = "zxcv"; 
	string s2(p1);
	cout << "string(const char*): " << s2 << endl;
	string s3;
	s3 = p1;
	cout << "直接=赋值 " << s3 << endl;
 
	cout << "-----------------string 转 char* 用.c_str()---------------------" << endl << endl;
	string s4 = "qwer";
	const char *p2 = s4.c_str();
	cout  << p2 << endl;

	char *p3 = (char*)s4.c_str();
	cout  << p3 << endl;

	system("pause");
} 


  • 输出如下:
-------------- float  转 string ------------------

num->string: -2.35
--------------- string 转 float ------------------

string->num: 123.23
-----------------char*  转 string 直接赋值---------------------

string(const char*): zxcv
直接=赋值 zxcv
-----------------string 转 char*.c_str()---------------------

qwer
qwer

char * 和 string 用法示例

#include <float.h>
#include <stdio.h>
#include <vector>
#include <string>
#include <iostream>

int main(int argc, char **argv)
{
	if (argc != 4)
	{
		fprintf(stderr, "Usage: %s [imagepath]\n", argv[0]);
		return -1;
	}

	const char *imagepath = argv[1];
	
	char *model_param = argv[2];
	char *model_bin = argv[3];
	printf("model_param % s \n", model_param);
	printf("model_bin % s \n", model_bin);
	
	# char * 转 string
	std::string MODEL_PARAM(model_param);
	std::string MODEL_BIN(model_bin);

	# string char *
	printf("MODEL_PARAM %s \n", MODEL_PARAM.c_str());
	printf("MODEL_BIN %s \n", MODEL_BIN.c_str());

	return 0;
}

cin 和 cout 用法示例

#include <iostream>
#include <cstring>

using namespace std;


typedef struct Books
{
   char  title[50];
   char  author[50];
   char  subject[100];
   int   book_id;
}Read;

int main()
{
	char str[] = "Hello ,C++";

	cout<< "Value is " << str << endl;
	
	char name[50];
	int age ;
	cin >> name >> age;
	cout << " Your name is " << name <<endl;
	cout<< " age = " << age <<endl;
	
	Read read;
	strcpy( read.title, "C++ 教程");
	cout << read.title <<endl;

	strcpy( read.author , "开心");
	cout << read.author << endl;

	
	return 0;

}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

墨理学AI

不必打赏,关注博主公众号即可

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值