C++之string类型入门(二):类型转化


一、其他类型转string

1.数字类型转string:to_string()

string std::to_string(int)

string std::to_string(long)

string std::to_string(unsigned long)

string std::to_string(long long)

string std::to_string(unsigned long long)

string std::to_string(float)

string std::to_string(double)

string std::to_string(long double)

例如:

#include <iostream>
#include <cstring>
using namespace std;

int main()
{
    int num_int = 123;
    long num_long = 1234;
    long long num_long_long = 12345;
    float num_float = 3.14;
    double num_double = 3.1415;
    long double num_long_double = 3.1415926;

    string num = to_string(num_int) + " " + to_string(num_long) + " " + to_string(num_long_long) + " " + to_string(num_float) + " " + to_string(num_double) + " " + to_string(num_long_double);
    cout << num << endl;
    // 123 1234 12345 3.140000 3.141500 3.141593
    return 0;
}

二、string转其他类型

1.string转c_str

const char* c_str() const

功能:返回string类型的字符串转化成的const char*的临时变量

(1)直接使用

string hello = "hello";
    
/* 可以输出它的c形式的c_str字符串 */
cout << hello.c_str() << endl;

(2)开辟char*内存空间,strcpy()赋值

string hello = "hello";

/* 不可以当赋值给一个变量 */
// !char* cstr = hello.c_str();

/* 静态开辟内存空间 */
char c_str1[10];
strcpy(c_str1, hello.c_str());
cout << c_str1 << endl;

/* 动态开辟内存空间 */
char *c_str2 = new char[hello.length() + 1];
// 内存空间不可以直接赋值,用strcpy()赋值
// !c_str2 = hello.c_str();
strcpy(c_str2, hello.c_str());
cout << c_str2 << endl;

注意:要预留一位\0,所以静态大一些,动态要hello.length() + 1

(3)const char*直接赋值

string hello = "hello";
// 用const char* 就不会出现非法转化错误了
const char* const_cstr = hello.c_str();
cout << const_cstr << endl;

2.string转数字类型

int std::stoi(const string& __str, size_t* __idx = 0, int __base = 10)

long std::stol(const string& __str, size_t* __idx = 0, int __base = 10)

unsigned long stoul(const string& __str, size_t* __idx = 0, int __base = 10)

long long std::stoll(const string& __str, size_t* __idx = 0, int __base = 10)

unsigned long long std::stoull(const string& __str, size_t* __idx = 0, int __base = 10)

float std::stof(const string& __str, size_t* __idx = 0)

double std::stod(const string& __str, size_t* __idx = 0)

long double std::stold(const string& __str, size_t* __idx = 0)

参数:

  • str:包含但可以不只是数字的string字符串。
    跳过前面的空格字符,直到遇上数字或正负符号才开始做转换,再遇到非数字或字符串结束时(’\0’)结束转换,并将转换数值返回。
  • __idx:指向一个size_t类型的对象。传入指针地址后,该对象的值会被修改为string中数值后的第一个字符所在位置
  • __base:进制基数,默认10进制

例如:

int num_int = stoi(to_string(123));
long num_long = stol(to_string(1234));
long long num_long_long = stoll(to_string(12345));
float num_float = stof(to_string(3.14));
double num_double = stod(to_string(3.1415));
long double num_long_double = stold(to_string(3.1415926));
string s = "  -34c1d.?";
int a = stoi(s);
cout << a << endl; //-34
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值