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();
}
相关推荐
Max_uuc11 天前
【架构心法】逃离回调地狱:从 Protothreads 到 C++20 协程 (Coroutines) 的嵌入式进化
c++20
阿猿收手吧!19 天前
【C++】C++20协程的await_transform和coroutine_handle
开发语言·c++·c++20
阿猿收手吧!19 天前
【C++】 co_yield如何成为语法糖?解析其背后的Awaitable展开与协程状态跃迁
c++·c++20
吐泡泡_21 天前
C++20(三路比较运算符)
c++20
啟明起鸣1 个月前
【C++20新特性】概念约束特性与 “模板线程池”,概念约束是为了 “把握未知对象”
开发语言·c++·c++20·模板线程池
linweidong1 个月前
虎牙C++面试题及参考答案(上)
stl·vector·线程·内存管理·c++20·c++面试·c++调用
吐泡泡_1 个月前
C++20(概念和约束)
c++20
訫悦1 个月前
体验在Qt中简单使用C++20的协程
qt·c++20·协程
fpcc1 个月前
C++20中的预处理器宏——__VA_OPT__
c++20
Codeking__1 个月前
C++20的consteval和constinit(接C++11的constexpr)
算法·c++20