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();
}
相关推荐
在黎明的反思4 天前
c++20协程
java·前端·c++20
黑不溜秋的6 天前
C++语言特性32 - 三方比较(C++20)
开发语言·c++·c++20
小毅&Nora8 天前
【后端】【C++】泛型算法:从传统到C++20 Ranges的进化之旅
算法·c++20·泛函算法
Lion Long8 天前
C++20 异步编程:用future、promise 还是协程?
开发语言·c++·stl·c++20
Mr_WangAndy10 天前
C++20新特性_指定初始化 (Designated Initializers)
c++20·c++40周年·指定初始化
Mr_WangAndy11 天前
C++20新特性_std::is_constant_evaluated() 编译期判断
c++20·c++40周年·编译期间判断
小毅&Nora12 天前
【后端】【C++】从裸指针到 C++20 协程:现代 C++ 内存与并发编程的双重革命
java·c++20
Mr_WangAndy12 天前
C++20新特性_std::jthread和chrono库扩展
c++20·c++20新特性·c++40周年·chrono库扩展·jthread线程
Mr_WangAndy12 天前
C++20新特性_原子智能指针,std::source_location和位操作函数
c++20·c++40周年·c++原子智能指针·source_location·位操作函数
Mr_WangAndy12 天前
C++20新特性_[[likely]] , [[unlikely]]属性和特性测试宏
c++20·likely·c++40周年·unlikely·特性测试宏