如何将字符 * 字符串 = "2001-02-03"转换为整数格式?
How to translate char * string = "2001-02-03" to integer format?
我有这些字符串:
const char * date = "2001-02-03";
const char * id = "987654/3210";
我需要非常快速地转换为整数或长整数(为id)。我需要翻译比较(对于数字strcmp()是缓慢的)。我只有这个库:
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
的例子:Const char * date = "2001-02-03";-> int int_date = 20010203;Const char * id = "987654/3210";-> long long_id = 9876543210;
怎么办?
如果您有字符串,仅strcmp
比将其转换(解析)为另一种格式然后进行比较要快。
但是一个简单的解析方法是:
const char * date = "2001-02-03";
int y, m, d;
int result = sscanf(date, "%d-%d-%d", &y, &m, &d);
if (result == 3)
{
// use them
}
(我建议我的代码只是作为一个例子)
相关文章:
- 防止主数据类型C++的隐式转换
- 模板参数替换失败,并且未完成隐式转换
- 努力将整数转换为链表。不知道我在这里做错了什么
- HEX值到wchar_t字符(UTF-8)的转换
- lambda参数转换为constexpr技巧,然后获取带链接的数组
- 将 Qvector<uint8_t> 转换为 QString
- 如何在cuSparse中使用cusparseXcoo2csr从coo转换为csc
- 有关插入适配器的错误。[错误]请求从 'back_insert_iterator<vector<>>' 类型转换为非标量类型
- 在c++中使用nlohmann从类到json的转换
- 从"int*"强制转换为"unsigned int"会丢失精度错误
- 将Integer转换为4字节的unsined字符矢量(按大端字节顺序)
- 处理小于cpu数据总线的数据类型.(c++转换为机器代码)
- 如何使用OpenCV将RBG图像转换为HSV,并将H、S和V值保存为C++中的3个独立图像
- 有没有简单的方法可以将"02:00"转换为分钟,比如 120?
- 在 C++03 中将 std::string 转换为 int
- C++ 03 类模板 这是转换构造函数还是转换运算符?以及如何声明解决此问题的方法
- 尝试将 C++11 代码转换为 C++03 时默认函数模板参数出错
- 在C++03中模拟显式强制转换
- 如何将字符 * 字符串 = "2001-02-03"转换为整数格式?
- 是否有C++11到C++03转换器