【C++】string转换int和数字转换成字符串

本文详细介绍了C++中不同类型名的获取方法,包括使用typeid关键字和name()函数。探讨了从string到int、long、float等基本类型以及从数字到string的转换技巧,特别关注stoi、stof、stod等函数的使用,并提供了实例代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1 类型名获取

typeinfo库中,有工具函数typeid().name(),可以用来返回变量的数据类型:

#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;
}

我的编译器的输出:
在这里插入图片描述
另一种输出:

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”、”float”等之类的名称,对于这类的编译器可以这样使用:

#include <typeinfo>
if (typeid(a) == typeid(int))

注意:typeid().name() 返回值

[类别]:若是自定义的名称空间则输出字符数及它的名字,若内建类型输出如下:
  bool:                     b
  char:                     c
  signed char:              a
  unsigned char:            h
  (signed) short (int):     s
  unsigned short (int):     t
  (signed) (int):           i
  unsigned (int):           j
  (signed) long (int):      l
  unsigned long (int):      m
  (signed) long long (int): x
  unsigned long long (int): y
  float:                    f
  double:                   d
  long double:              e

参考链接:C++ typeid关键字详解
     typeid().name() 返回值

2 string转换int

C语言转换形式:

std::string str;
int i = atoi(str.c_str());
对含有字母的字符串进行atoi转换,需要当心
#include <iostream>
#include <string>
using namespace std;

int main()
{
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
}

C++转换形式(C++11):

std::string str;
int i = std::stoi(str);

stol(long), stof(float), stod(double)也相同的用法的。

举例说明:

#include<iostream>
#include <typeinfo>
using namespace std;
int main(){
    string str = "123";
    int num = stoi(str);
    cout<<num;
    if(typeid(num) == typeid(int))
        cout<<"wwww";

    return 0;
}
如果string不是数值使用stoi会有问题的。

注意:C++中判断数据类型的函数

我们可以利用库函数typeid,来判断变量a是否为整型变量;

#include <typeinfo>
if (typeid(a) == typeid(int))

string库中stoi函数

int stoi (string str);
float stof(string str);
long stol(string str);
long long stoll(string str);
#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;
}

在这里插入图片描述

3 数字转换成字符串

下面都是可以使用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);
#include <iostream>
#include <string>
int main ()
{
  std::string pi = "pi is " + std::to_string(3.1415926);
  std::string per = std::to_string(1+2+4+7) + " is  number";
  std::cout << pi << '\n';
  std::cout << per << '\n';
  return 0;

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值