C++ 内置函数 判断字母、数字及大小写转换
一、c++的几个内置函数
1.1 判断
islower(char c) :是否为小写字母
isupper(char c): 是否为大写字母
isdigit(char c) :是否为数字
isalpha(char c) :是否为字母
isalnum(char c): 是否为字母或者数字
1.2 转换
toupper(char c): 字母小转大
tolower(char c) :字母大转小
二、std 模板库的转换
2.1 string转换为其他类型
string转换为float: float stof(const string& str, size_t* idx = 0);
string转换为double: double stod(const string& str, size_t* idx = 0);
string转换为int: int stoi (const string& str, size_t* idx = 0, int base = 10);
string转换为long: long stol (const string& str, size_t* idx = 0, int base = 10);
string转换为long double: long double stold (const string& str, size_t* idx = 0);
string转换为long long: long long stoll (const string& str, size_t* idx = 0, int base = 10);
string转换为unsigned long: unsigned long stoul (const string& str, size_t* idx = 0, int base = 10);
string转换为unsigned long long: unsigned long long stoull (const string& str, size_t* idx = 0, int base = 10);
2.3 其他类型转换为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);
三、使用sstream方式
/// 数字转换字符串
template <typename T>
std::string num_to_string(cont T& t) {
std::ostringstream oss;
oss << t;
return oss.str();
}
/// 字符串转换数字
template <typename T>
T string_to_num(const std::string& str) {
std::istringstream iss(str);
T num;
iss > num;
return num;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了