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();
}
相关推荐
十五年专注C++开发8 天前
全面深入了解C++20 范围库(std::ranges)
c++20·管道·哨兵·视图·ranges
小小龙学IT11 天前
C++20 协程深度解析:从原理到高性能异步框架实战
junit·c++20
楼田莉子16 天前
C++20新特性:协程
开发语言·c++·后端·学习·c++20
ouliten18 天前
C++笔记:C++20风格线程池
c++·笔记·c++20
眠りたいです19 天前
现代C++:C++17中的新库特性
开发语言·c++·c++20·c++17
楼田莉子1 个月前
C++20新特性:Range库
开发语言·c++·后端·学习·c++20
楼田莉子1 个月前
C++20现代特性:概念与约束
开发语言·c++·后端·学习·c++20
aluluka1 个月前
C++ 20 协程的探索
c++·c++20
君鼎1 个月前
内存池完整实现——C++20版
c++20·内存池
普通网友1 个月前
记录我适配iOS26遇到的一些问题
c++20