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();
}
相关推荐
ice_junjun2 天前
C++20 线程返回值处理指南
c++20·c++ 多线程返回值
凌乱风雨12115 天前
从源码角度解析C++20新特性如何简化线程超时取消
前端·算法·c++20
shuai132_14 天前
【无标题】
c++20
ULTRA??16 天前
基于range的函数式编程C++,python比较
c++·python·kotlin·c++20
apocelipes17 天前
从源码角度解析C++20新特性如何简化线程超时取消
c++·性能优化·golang·并发·c++20·linux编程
ALex_zry17 天前
C++20和C++23 在内存管理、并发控制和类型安全相关优化方式的详细技术分析
安全·c++20·c++23
ALex_zry17 天前
C++20/23标准对进程间共享信息的优化:从传统IPC到现代C++的演进
开发语言·c++·c++20
fpcc20 天前
c++20容器中的透明哈希
哈希算法·c++20
小老鼠不吃猫20 天前
C++20 STL <numbers> 数学常量库
开发语言·c++·c++20
Chrikk20 天前
C++20 Concepts 在算子库开发中的应用:从 SFINAE 到类型约束
人工智能·算法·c++20