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();
}
相关推荐
a东方青8 天前
[蓝桥杯C++ 2024 国 B ] 立定跳远(二分)
c++·算法·蓝桥杯·c++20
小葡萄202510 天前
黑马程序员2024新版C++笔记 第五章 面向对象
开发语言·c++·笔记·c++20
Tipriest_12 天前
【C++20新特性】ranges::sort()使用方法,优势,注意点
算法·leetcode·c++20·排序·sort
Tipriest_12 天前
ubuntu20.04&vscode使用C++20(调整gcc版本&vscode设置)
ide·vscode·c++20·gcc
小葡萄202512 天前
黑马程序员C++2024新版笔记 第4章 函数和结构体
笔记·c++20
AI迅剑14 天前
《C++20新特性全解析:模块、协程与概念(Concepts)》
c++20
superior tigre18 天前
C++学习:六个月从基础到就业——C++20:范围(Ranges)进阶
c++·学习·c++20
superior tigre18 天前
C++学习:六个月从基础到就业——C++20:范围(Ranges)基础
c++·学习·c++20
点云SLAM19 天前
C++中聚合类(Aggregate Class)知识详解和注意事项
c++·算法·c++20·c++学习·聚合类·面向对象设计、·c++高级应用
小葡萄202519 天前
黑马程序员C++2024新版笔记 第三章 数组
笔记·算法·c++20