c++11 string 转换大小写

创建日期: 2023-05-26 12:37 | 作者: 风波 | 浏览次数: 15 | 分类: C++

代码

#include <iostream>
#include <algorithm>

std::string lower(const std::string &s) {
    std::string l;
    l.resize(s.size());
    std::transform(
            s.begin(), s.end(), l.begin(),
            [](unsigned char c) { return std::tolower(c); }); 
    return l;
}

std::string upper(const std::string &s) {
    std::string u;
    u.resize(s.size());
    std::transform(
            s.begin(), s.end(), u.begin(),
            [](unsigned char c) { return std::toupper(c); }); 
    return u;
}

int main() {
    std::string s = "Hello World !";
    std::cout << s << " tolower: " << lower(s) << std::endl;
    std::cout << s << " toupper: " << upper(s) << std::endl;

    return 0;
}

编译

g++ --std=c++11 -o main main.cc
15 浏览
9 爬虫
0 评论