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();
}
相关推荐
繁星十年2 天前
在C++中,工厂模式的思考(《C++20设计模式》及常规设计模式对比)
c++·设计模式·c++20
繁星十年8 天前
在C++中,构造器(Builder)模式的思考(《C++20设计模式》及常规设计模式对比)
c++·设计模式·c++20
程序猿阿伟23 天前
PHP 中如何高效地处理大规模数据的排序?
c++20
喜欢打篮球的普通人25 天前
【C++20工程实战】自己动手实现纯头文件日志库
c++20
zhangzhangkeji1 个月前
vs2019 c++20 规范 STL库中关于时间的模板
c++·c++20
DogDaoDao2 个月前
c++ 各版本特性介绍
c++·c++11·c++20·c++14·c++17·c++03
vczxh2 个月前
c++20 std::reinterpret_cast、std::bit_cast、std::static_cast
c++20
zhangzhangkeji2 个月前
vs2019 里 C++ 20规范的 string 类的源码注释
c++20
别NULL2 个月前
探究C++20协程(5)——基于挂起实现无阻塞的定时器
算法·c++20
+xiaowenhao+2 个月前
《Beginning C++20 From Novice to Professional》第五章 Arrays and Loops
开发语言·c++·c++20