-
Notifications
You must be signed in to change notification settings - Fork 0
/
C++数据类型转换
163 lines (131 loc) · 3.27 KB
/
C++数据类型转换
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
https://blog.csdn.net/thecentry/article/details/78477212
https://blog.csdn.net/LYH2277/article/details/82947649
1. c_str():生成一个const char*指针,指向以空字符终止的数组。
2. data():与c_str()类似,但是返回的数组不以空字符终止。
编程中出现char* 与string/wstring/CString之间的转换;
//char* string 与int float double之间的转换.
以下为程序头文件:
#inlcude<iostream>
#include<string>
#include<ssrtream>
using namespace std;
char* char[] 与string 类型之间的转换;
int cp1()
{
char *p = "abcd";
char t[] = "abcde";
string test;
test = p;
cout << test <<endl;
cout << test.length() <<endl;
return 0;
}
//string 到 char*的转换
int cp2()
{
string test = "abc";
char t[10];
strcpy(t, test.c_str());
cout << t << endl;
cout << strlen(t) <<endl;
char *te;
int number = test.length();
te = new char[number + 1];
cout << number <<endl;
strcpy(te, test.c_str());
cout << te << endl;
cout << strlen(te) << endl;
delete[] te;
return 0;
}
//string到const char*之间的转换:\
int CP3()
{
const char* c;
string test = "1234";
c = test.c_str();
cout << c <<endl;
const char* t;
t = test.data();
cout << t << endl;
}
//int到string的转换
int CP4()
{
int nt = 30;
char c[30];
//using C template to trans tfrom int to string
itoa(nt, c, 10);//以参数3位为进制,以几进制转换为 char
cout << c <<endl;
cout << strlen(c) <<endl;
//using string stream to trans from int to string
stringstream test;
test << nt;
string stest = test.str();
cout << stest <<endl;
cout << stest.length() << endl;
}
//sting到int的转换
int CP5()
{
string test = "17";
char* t;
int nt = static_cast<int>(strtol(test.c_str(), &t, 10));//10 表示转换进制
cout << nt <<endl;
int nte;
sscanf(test.c_str(), "%d", &nte);//%d 表示十进制
cout << nte <<endl;
//using stringstream to trans from string to int
stringstream stest;
stest << test;
int ntes;
stest >> ntes;
cout << ntes << endl;
//using C template to trans from string tio int
int ntest = atoi(test.c_str());
cout << ntest << endl;
//using C++ template to trans from string to int
int nnt;
nnt = stoi(test.c_str());
cout << nnt <<endl;
return 0;
}
//string to double 的转换
int cp6()
{
string test = "17.101";
//using C template to trans from double to string
double t = atof(test.c_str());
cout << t << endl;
return 0;
}
数值类型到string的转换可以有以下几种:
(1)函数模板+stringstream
int 型转换为string型
void int2str(const int &int_temp,string &string_temp)
{
stringstream stream(string_temp);
stream << int_temp;
}
string型转int型
void str2int(int &int_temp,const string &string_temp)
{
stringsrtream stream(string_temp);
stream >> int_temp;
}
使用标准库函数std::to_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);
使用C标准库函数:
atoi(str.c_str());
strtoul(str.c_str(), NULL, 10);
atof(str.c_str());
使用C++标准库函数:
stoi()