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();
}
相关推荐
CHANG_THE_WORLD7 天前
C++20 协程从零入门:用 ASCII 图彻底理解 `co_await`、协程帧与异步执行
c++20
无限的鲜花17 天前
协程本质是函数加状态机——零基础深入浅出 C++20 协程
c++·算法·c++20
十五年专注C++开发1 个月前
全面深入了解C++20 范围库(std::ranges)
c++20·管道·哨兵·视图·ranges
小小龙学IT1 个月前
C++20 协程深度解析:从原理到高性能异步框架实战
junit·c++20
楼田莉子2 个月前
C++20新特性:协程
开发语言·c++·后端·学习·c++20
ouliten2 个月前
C++笔记:C++20风格线程池
c++·笔记·c++20
眠りたいです2 个月前
现代C++:C++17中的新库特性
开发语言·c++·c++20·c++17
楼田莉子2 个月前
C++20新特性:Range库
开发语言·c++·后端·学习·c++20
楼田莉子2 个月前
C++20现代特性:概念与约束
开发语言·c++·后端·学习·c++20
aluluka2 个月前
C++ 20 协程的探索
c++·c++20