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();
}
相关推荐
闻缺陷则喜何志丹2 天前
【C++20特性】std::format
c++·字符串·c++20·格式化
fqbqrr3 天前
2612C++,C++20实战心得
c++·c++20
争不过朝夕,又念着往昔5 天前
C++20-概念与约束
windows·c++20
沫璃染墨7 天前
io_uring 深度剖析:从 SQ/CQ 共享队列到 C++ 高性能异步服务器实战
linux·运维·服务器·c++·系统架构·c++20
Lucis__1 个月前
C++20概念约束—现代C++模板约束的演进与重构
c++20·程序·κzy
SunkingYang1 个月前
C++20 vs C++17:新特性对比与用法指南
c++·c++20·特性·版本·新特性·对比·c++17
前进吧-程序员1 个月前
C++20 协程入门实战:从 co_await 到一个 100 行的异步任务调度器
网络·c++·c++20
前进吧-程序员1 个月前
实战:C++20 协程 + io_uring 写一个 400 行的 echo server
网络·c++·c++20
骑着蜗牛撵大象3271 个月前
C++20 协程 × 屏障:手写一个 co_await 版 Barrier,让等待不再钉死线程
android·c++20·协程·并发编程·co_await·屏障同步
闻缺陷则喜何志丹1 个月前
【c++20】协程与屏障
c++·算法·c++20·携程·屏障