C++常用类型转换

1.QString转string

QString  qstr = "test";
string s = qstr.toStdString();

2.string转QString

string s = "test";
QString qstr = QString::fromStdString(s);

3.QString转char *

方法1(推荐):

QString  qstr = "test"; 
char * filename = qstr.toUtf8().data();

方法2:

QString  qstr = "test"; 
char * filename = qst.toLocal8Bit().data();

4.char *转QString

char *ch="test";
QString str(ch);   // Qt5     
QString str = QString::fromUtf8(ch));    //  Qt4

5.QString转const char *

方法1:

QString  qstr = "test"; 
char* filename = qstr .toStdString().c_str();

6.float转string

stringstream ss;
float b = 10.1;
ss << b;
string p;
ss >> p;
cout << p;

注意:如果需要多个float转成string,stringstream的变量ss貌似无法共用,例如下面的例子中只有thresh1能成功输出。thresh2和thresh3是空的。

stringstream ss;
float thresh1 = 10.1, thresh2 = 20.1, thresh3 = 30.1;
string p1, p2, p3;
ss << thresh1;
ss >> p1; cout << p1;
ss << thresh21;
ss >> p2; cout << p2;
ss << thresh3;
ss >> p3; cout << p3;

如果需要多个float转成string,可以用多个stringstream变量,如下:

stringstream ss1, ss2, ss3;
float thresh1 = 10.1, thresh2 = 20.1, thresh3 = 30.1;
string p1, p2, p3;
ss1 << thresh1;
ss1 >> p1; cout << p1;
ss2 << thresh21;
ss2 >> p2; cout << p2;
ss3 << thresh3;
ss3 >> p3; cout << p3;

方法2:借助float转QString,再QString转string

7.float转QString

float thresh1 = 10.1, thresh2 = 20.1, thresh3 = 30.1;
QString  qstr = QString("%1+%2 %3").arg(thresh1).arg(thresh2).arg(thresh3); 
### C++ 常用变量类型转换方法及示例 在C++中,变量类型之间的换可以通过多种方式进行,包括隐式类型转换、强制类型转换(显示类型转换)、以及标准库提供的类型转换操作符。以下是常用的变量类型转换方法及其示例代码。 #### 1. 隐式类型转换 隐式类型转换是编译器自动完成的类型转换过程,无需程序员显式干预。例如,将一个较小类型的值赋给较大类型时,编译器会自动进行类型转换。 ```cpp int a = 10; double b = a; // 隐式地将 int 换为 double ``` 这种换方式简单易用,但需注意可能引发的数据丢失或精度问题[^2]。 #### 2. 强制类型转换(显示类型转换) 强制类型转换是指程序员显式地将一种数据类型转换为另一种数据类型。C++支持两种风格的强制类型转换:C风格和C++风格。 - **C风格**:使用圆括号包裹目标类型。 ```cpp int a = 4; float c = (float)a; // 将 int 类型转换float 类型 ``` - **C++风格**:直接指定目标类型作为函数调用的形式。 ```cpp int a = 4; float d = float(a); // 将 int 类型转换float 类型 ``` 需要注意的是,C风格的强制类型转换虽然兼容性好,但在某些场景下可能会导致不安全的行为,因此推荐使用C++风格[^3]。 #### 3. 使用标准库类型转换操作符 C++标准库提供了四种类型转换操作符,分别是`static_cast`、`dynamic_cast`、`reinterpret_cast`和`const_cast`。以下分别介绍它们的用途和示例。 - **`static_cast`**:用于相关类型之间的换,如基本数据类型之间的换、指针与引用的换等。它在编译期执行,效率较高。 ```cpp double pi = 3.14159; int integer_pi = static_cast<int>(pi); // 将 double 换为 int ``` - **`dynamic_cast`**:主要用于继承体系中的类型转换,通常涉及多态对象。它在运行时执行,并通过RTTI(Run-Time Type Information)机制检查换是否合法。 ```cpp class Base { virtual void func() {} }; class Derived : public Base {}; Base* base = new Derived(); Derived* derived = dynamic_cast<Derived*>(base); // 动态类型转换 if (derived == nullptr) { // 换失败处理 } ``` - **`reinterpret_cast`**:用于低级类型转换,能够将一个指针类型转换为另一个完全无关的指针类型。这种换可能导致未定义行为,应谨慎使用。 ```cpp int value = 42; void* ptr = reinterpret_cast<void*>(&value); // 将 int 指针换为 void 指针 ``` - **`const_cast`**:用于添加或移除变量的`const`属性。 ```cpp const int a = 10; int& b = const_cast<int&>(a); // 移除 const 属性 ``` #### 4. 时间戳换 在实际开发中,时间戳与日期时间格式之间的换也属于常见的类型转换需求。可以借助`<ctime>`头文件中的函数实现时间戳与日期时间格式的互。 ```cpp #include <iostream> #include <ctime> int main() { std::time_t now = std::time(nullptr); // 获取当前时间戳 std::tm* local_time = std::localtime(&now); // 将时间戳换为本地时间结构 std::cout << "Year: " << (local_time->tm_year + 1900) << std::endl; std::tm time_struct = {}; time_struct.tm_year = 123; // 设置年份(从1900年开始计数) time_struct.tm_mon = 1; // 设置月份(从0开始计数) time_struct.tm_mday = 1; // 设置日期 std::time_t timestamp = std::mktime(&time_struct); // 将时间结构换为时间戳 return 0; } ``` 上述代码展示了如何使用`std::time`和`std::localtime`函数进行时间戳与日期时间格式的换[^4]。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值