一、 类型名获取
typeinfo库中,有工具函数typeid().name(),可以用来返回变量的数据类型:
(可不添加库函数typeinfo)
Example:
#include<iostream>
#include <typeinfo>
using namespace std;
int main(void)
{
int a = 7;
long b = 123456;
long long c = 111222333444555;
unsigned int d = 45;
unsigned long e = 654321;
unsigned long long f = 555444333222111;
float g = 3.14;
double h = 1.415649363;
long double i = 234.7264182536;
string j = "string";
cout << typeid(a).name() << endl;
cout << typeid(b).name() << endl;
cout << typeid(c).name() << endl;
cout << typeid(d).name() << endl;
cout << typeid(e).name() << endl;
cout << typeid(f).name() << endl;
cout << typeid(g).name() << endl;
cout << typeid(h).name() << endl;
cout << typeid(i).name() << endl;
cout << typeid(j).name() << endl;
}
Output:
int
long
__int64
unsigned int
unsigned long
unsigned __int64
float
double
long double
class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >
二、 int 转 string
string库函数中函数to_string,支持多种数据类型转换为string类:
string to_string (int value);
string to_string (long value);
string to_string (long long value);
string to_string (unsigned value);
string to_string (unsigned long value);
string to_string (unsigned long long value);
string to_string (float value);
string to_string (double value);
string to_string (long double value);
Example:
#include<iostream>
#include <typeinfo>
#include <string>
using namespace std;
int main(void)
{
int a = 7;
float b = 3.14;
double c = 1.415649363;
string a_str = to_string(a);
string b_str = to_string(b);
string c_str = to_string(c);
cout << "a: from " << typeid(a).name() << " to " << typeid(a_str).name() << endl;
cout << "b: from " << typeid(b).name() << " to " << typeid(b_str).name() << endl;
cout << "c: from " << typeid(c).name() << " to " << typeid(c_str).name() << endl;
}
Output:
a: from int to class std::basic_string<char,struct std::char_traits<char>,class
std::allocator<char> >
b: from float to class std::basic_string<char,struct std::char_traits<char>,clas
s std::allocator<char> >
c: from double to class std::basic_string<char,struct std::char_traits<char>,cla
ss std::allocator<char> >
三、 string 转 int
3.1 string库中stoi函数
int stoi (string str);
float stof(string str);
long stol(string str);
long long stoll(string str);
Example:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str_binary = "101001";
string str_octal = "54362";
string str_decimal = "65536";
string str_hexadecimal = "5ca2";
string str_auto = "0x432";
int int_binary = std::stoi(str_binary, nullptr, 2);
int int_octal = std::stoi(str_octal, nullptr, 8);
int int_decimal = std::stoi(str_decimal, nullptr, 10);
int int_hexadecimal = std::stoi(str_hexadecimal, nullptr, 16);
int int_auto = std::stoi(str_auto, nullptr, 0);
cout << "int_binary: " << int_binary << endl;
cout << "int_octal: " << int_octal << endl;
cout << "int_decimal: " << int_decimal << endl;
cout << "int_hexadecimal: " << int_hexadecimal << endl;
cout << "int_auto: " << int_auto << endl;
}
Output:
int_binary: 41
int_octal: 22770
int_decimal: 65536
int_hexadecimal: 23714
int_auto: 1074
Attention:
十进制:由0至9的数字组成 没有前缀,不能以0开始。
八进制:以0为前缀,其后由0至7的数字组成 。
十六进制:以0x或0X为前缀,其后由0至9的数和A到F(大小写均可)字母组成。
二进制:C++是不直接支持二进制的,可以用bitset表示,具体参考bitset的使用。
3.2 标准库中atoi函数
Example:
#include <iostream>
#include <typeinfo>
using namespace std;
int main()
{
string s = "51426";
int value = atoi(s.c_str());
cout << "s: " << typeid(s).name() << endl;
cout << "value: " << typeid(value).name() << endl;
system("pause");
}
Output:
s: class std::basic_string<char,struct std::char_traits<char>,class std::allocator<
char> >
value: int
对含有字母的字符串进行atoi转换,需要当心
string str1 = "12345";
string str2 = "abcde"
string str3 = "123abc";
string str4 = "abc123"
cout << atoi(str1.c_str()) << endl; // 12345
cout << atoi(str2.c_str()) << endl; // 0
cout << atoi(str3.c_str()) << endl; // 123
cout << atoi(str4.c_str()) << endl; // 0