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();
}
相关推荐
楼田莉子18 小时前
C++20新特性:Range库
开发语言·c++·后端·学习·c++20
楼田莉子2 天前
C++20现代特性:概念与约束
开发语言·c++·后端·学习·c++20
aluluka2 天前
C++ 20 协程的探索
c++·c++20
君鼎6 天前
内存池完整实现——C++20版
c++20·内存池
普通网友15 天前
记录我适配iOS26遇到的一些问题
c++20
前进吧-程序员15 天前
C++20/23 Ranges:从「迭代器对」到「可组合管道」
c++20
Shan120520 天前
实例分析:C++20的std::jthread
c++20
charlie11451419120 天前
基于开源项目的现代C++工程实践——OnceCallback 前置知识(下):C++20/23 高级特性
c++·开源·c++20
Hical_W20 天前
Hical 踩坑实录五部曲(二):MSVC / GCC / Clang 三平台 C++20 编译差异
linux·windows·经验分享·嵌入式硬件·macos·开源·c++20
Shan120522 天前
C++20中带有约束条件的new
c++20