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();
}
相关推荐
ShineWinsu1 天前
对于 C++:C++20中Concept(概念) 与 Coroutine(协程)的解析
linux·开发语言·网络·c++·c++20·epoll
Lzg_na8 天前
C++20 Concept 核心概念、语法详解与三大工程实践场景
c++20
Billy121388 天前
Day14-C++20 Modules:模块声明与导入、模块分区、与头文件对比、迁移策略
c++20·c++进阶学习
程序猿编码8 天前
不用GPU,不用多线程,我用C++20手写了一个Gemma 3推理引擎
pytorch·语言模型·大模型·c++20·推荐算法
Billy1213810 天前
Day15-C++20 三路比较运算符 `<=>`:一次定义,全面排序
c++20·c++进阶学习
A_cainiao_A17 天前
C++20:std::stop_source让线程说停就听
开发语言·c++20
小小龙学IT21 天前
C++20 Ranges 库实现原理深度解析:从视图适配器到管道运算符
c++20
Billy1213825 天前
C++20 Concepts:约束与概念的现代范式
c++20·c++进阶学习
可爱系程序猿25 天前
mfc90.dll 缺失如何处理?VC++ 2008 运行库与并行程序集排查记录
程序人生·电脑·c++20
code_pgf25 天前
C++11 / C++14 / C++17 / C++20 新特性总结
c++·c++20