C++ char、string、int、float 相互【类型转换】
【摘要】
文章目录
char 数组 转 int 和 转 floatint 转 char 数组float 和 string 互转 | char* 和 string 互转char * 和 string 用法示例...
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;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 输出如下:
char 转 int -123
char 转 float 3.14
- 1
- 2
- 3
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;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 输出如下:
234
234
000234
- 1
- 2
- 3
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");
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 输出如下:
-------------- 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
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
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;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
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;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
文章来源: positive.blog.csdn.net,作者:墨理学AI,版权归原作者所有,如需转载,请联系作者。
原文链接:positive.blog.csdn.net/article/details/84501316
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)