std::string从C++20支持的新Operations

https://en.cppreference.com/w/cpp/string/basic_string

cpp 复制代码
#include <iostream>
#include <string>
#include <format>

void test_01()
{
    int val = std::stoi("101");
    std::cout << std::format("{}\n", val);  // 101

    std::string str("Hello C++20!");
    if(str.ends_with("C++20!")){
        std::cout << std::format("{} ends with \"{}\"\n",str, "C++20!");  // Hello C++20! ends with "C++20!"
    }
    if(str.starts_with("Hello")){
        std::cout << std::format("{} starts with \"{}\"\n",str, "Hello");  // Hello C++20! ends with "C++20!"
    }
    if(str.contains("C++23")){
        std::cout << std::format("{} contains \"{}\"\n",str, "C++23");
    }
    else {
        // std::basic_string<CharT,Traits,Allocator>::contains is supported from "C++23".
        std::cout << std::format("std::basic_string<CharT,Traits,Allocator>::contains is supported from \"{}\".\n", "C++23");
    }
}
 
int main()
{
    test_01();
}
相关推荐
fpcc4 天前
c++20新功能—std::osyncstream
c++·c++20
啦啦啦啦啦zzzz11 天前
一致性哈希
服务器·算法·哈希算法·c++20·分布式存储
keep intensify15 天前
C++20线程池
c++·算法·c++20
繁华的地方不一定留下你的脚印20 天前
C++20 concepts:把模板要求写在入口,而不是等一屏报错
c++20
码匠许师傅20 天前
【C++三方组件】Asio 下篇:C++20 协程版 TCP 客户端与服务端
c++·tcp/ip·c++20
啦啦啦啦啦zzzz1 个月前
利用RAII机制进行日志的采集
linux·服务器·c++·网络编程·c++20
啦啦啦啦啦zzzz1 个月前
Logger的组装(c++20)
c++·算法·c++20
啦啦啦啦啦zzzz1 个月前
Appender抽象类的实现c++20
linux·服务器·c++·c++20
OPEN-F1 个月前
C++20新特性精讲:概念、范围与三路比较
算法·c++20
小小龙学IT2 个月前
C++ std::chrono 时间库深度解析:从 duration 模板到 C++20 日历与时区
linux·c++·c++20