详情

王海鱼 Lv.8

关注
std::variant

类模板 std::variant 表示一个范例安全的 union。std::variant 的实例在任何给定时间都持有一个其替代范例的值(它也可以是无值的)
  1. std::variant<int, double> v{ 12 };
  2. std::get<int>(v); // == 12
  3. std::get<0>(v); // == 12
  4. v = 12.0;
  5. std::get<double>(v); // == 12.0
  6. std::get<1>(v); // == 12.0
std:ptional

类模板 std:ptional 管理一个可选的包罗值,即一个大概存在也大概不存在的值。optional 的常见用例是函数的返回值,该函数大概会失败
  1. std::optional<std::string> create(bool b) {
  2.   if (b) {
  3.     return "Godzilla";
  4.   } else {
  5.     return {};
  6.   }
  7. }
  8. create(false).value_or("empty"); // == "empty"
  9. create(true).value(); // == "Godzilla"
  10. // 返回 optional 的工厂函数可以用作 while 和 if 的条件
  11. if (auto str = create(true)) {
  12.   // ...
  13. }
std::any

一个范例安全容器,用于存储任何范例的单个值
  1. std::any x {5};
  2. x.has_value() // == true
  3. std::any_cast<int>(x) // == 5
  4. std::any_cast<int&>(x) = 10;
  5. std::any_cast<int>(x) // == 10
std::string_view

对字符串的非拥有引用。它实用于在字符串上提供抽象(比方用于分析)
  1. // 普通字符串。
  2. std::string_view cppstr {"foo"};
  3. // 宽字符串。
  4. std::wstring_view wcstr_v {L"baz"};
  5. // 字符数组。
  6. char array[3] = {'b', 'a', 'r'};
  7. std::string_view array_v(array, std::size(array));
  8. std::string str {"   trim me"};
  9. std::string_view v {str};
  10. v.remove_prefix(std::min(v.find_first_not_of(" "), v.size()));
  11. str; //  == "   trim me"
  12. v; // == "trim me"
std::invoke

调用一个 Callable 对象及其参数。可调用 对象的示例包罗 std::function 或 lambda;可以像平常函数一样调用的对象
  1. template <typename Callable>
  2. class Proxy {
  3.   Callable c_;
  4. public:
  5.   Proxy(Callable c) : c_{ std::move(c) } {}
  6.   template <typename... Args>
  7.   decltype(auto) operator()(Args&&... args) {
  8.     // ...
  9.     return std::invoke(c_, std::forward<Args>(args)...);
  10.   }
  11. };
  12. const auto add = [](int x, int y) { return x + y; };
  13. Proxy p{ add };
  14. p(1, 2); // == 3
std::apply

利用元组的参数调用一个 Callable 对象
  1. auto add = [](int x, int y) {
  2.   return x + y;
  3. };
  4. std::apply(add, std::make_tuple(1, 2)); // == 3
std::filesystem

新的 std::filesystem 库提供了一种尺度的方式来利用文件、目次和文件体系中的路径。
以下是一个大文件被复制到暂时路径的示例,条件是存在充足的空间:
  1. const auto bigFilePath {"bigFileToCopy"};
  2. if (std::filesystem::exists(bigFilePath)) {
  3.   const auto bigFileSize {std::filesystem::file_size(bigFilePath)};
  4.   std::filesystem::path tmpPath {"/tmp"};
  5.   if (std::filesystem::space(tmpPath).available > bigFileSize) {
  6.     std::filesystem::create_directory(tmpPath.append("example"));
  7.     std::filesystem::copy_file(bigFilePath, tmpPath.append("newFile"));
  8.   }
  9. }
std::byte

新的 std::byte 范例提供了一种尺度的方式来表示数据为字节。利用 std::byte 而不是 char 或 unsigned char 的利益是它不是字符范例,也不是算术范例;唯一可用的运算符重载是按位运算
  1. std::byte a {0};
  2. std::byte b {0xFF};
  3. int i = std::to_integer<int>(b); // 0xFF
  4. std::byte c = a & b;
  5. int j = std::to_integer<int>(c); // 0
留意,std::byte 只是一个罗列范例,而罗列的直接列表初始化成为大概,这得益于罗列的直接列表初始化
映射和聚集的拼接

在没有昂贵的拷贝、移动或堆分配/释放开销的情况下,移动节点和合并容器
从一个映射移动元素到另一个映射:
  1. std::map<int, string> src {{1, "one"}, {2, "two"}, {3, "buckle my shoe"}};
  2. std::map<int, string> dst {{3, "three"}};
  3. dst.insert(src.extract(src.find(1))); // 从 `src` 到 `dst` 廉价地移除并插入 { 1, "one" }
  4. dst.insert(src.extract(2)); // 从 `src` 到 `dst` 廉价地移除并插入 { 2, "two" }
  5. // dst == { { 1, "one" }, { 2, "two" }, { 3, "three" } };
插入整个聚集:
  1. std::set<int> src {1, 3, 5};
  2. std::set<int> dst {2, 4, 5};
  3. dst.merge(src);
  4. // src == { 5 }
  5. // dst == { 1, 2, 3, 4, 5 }
插入超出容器生命周期的元素:
  1. auto elementFactory() {
  2.   std::set<...> s;
  3.   s.emplace(...);
  4.   return s.extract(s.begin());
  5. }
  6. s2.insert(elementFactory());
更改映射元素的键:
  1. std::map<int, string> m {{1, "one"}, {2, "two"}, {3, "three"}};
  2. auto e = m.extract(2);
  3. e.key() = 4;
  4. m.insert(std::move(e));
  5. // m == { { 1, "one" }, { 3, "three" }, { 4, "two" } }
并行算法

许多 STL 算法(如 copy、find 和 sort)开始支持 并行实验计谋:seq、par 和 par_unseq,分别表示"次序"、“并行"和"并行无序”。
  1. std::vector<int> longVector;
  2. // 使用并行执行策略查找元素
  3. auto result1 = std::find(std::execution::par, std::begin(longVector), std::end(longVector), 2);
  4. // 使用顺序执行策略排序元素
  5. auto result2 = std::sort(std::execution::seq, std::begin(longVector), std::end(longVector));
std::sample

从给定序列中随机抽样 n 个元素(不放回),每个元素被选中的概率相称。
  1. const std::string ALLOWED_CHARS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
  2. std::string guid;
  3. // 从 ALLOWED_CHARS 中随机抽样 5 个字符。
  4. std::sample(ALLOWED_CHARS.begin(), ALLOWED_CHARS.end(), std::back_inserter(guid),
  5.   5, std::mt19937{ std::random_device{}() });
  6. std::cout << guid; // 例如:G1fW2
std::clamp

将给定值限定在上下界之间。
  1. std::clamp(42, -1, 1); // == 1
  2. std::clamp(-42, -1, 1); // == -1
  3. std::clamp(0, -1, 1); // == 0
  4. // `std::clamp` 也接受自定义比较器:
  5. std::clamp(0, -1, 1, std::less<>{}); // == 0
std::reduce

对给定范围的元素举行折叠利用。它与 std::accumulate 概念上雷同,但 std::reduce 会并行实验折叠利用。由于折叠是并行实验的,假如指定了二元运算符,则要求它是联合的和交换的。给定的二元运算符也不应修改范围内的任何元素或使任何迭代器失效。
默认的二元运算是 std::plus,初始值为 0
  1. const std::array<int, 3> a{ 1, 2, 3 };
  2. std::reduce(std::cbegin(a), std::cend(a)); // == 6
  3. // 使用自定义二元运算符:
  4. std::reduce(std::cbegin(a), std::cend(a), 1, std::multiplies<>{}); // == 6
别的,还可以为归约器指定转换利用:
  1. std::transform_reduce(std::cbegin(a), std::cend(a), 0, std::plus<>{}, times_ten); // == 60
  2. const std::array<int, 3> b{ 1, 2, 3 };
  3. const auto product_times_ten = [](const auto a, const auto b) { return a * b * 10; };
  4. std::transform_reduce(std::cbegin(a), std::cend(a), std::cbegin(b), 0, std::plus<>{}, product_times_ten); // == 140
前缀和算法

支持前缀和(包罗包罗扫描和清除扫描)以及转换。
  1. const std::array<int, 3> a{ 1, 2, 3 };
  2. std::inclusive_scan(std::cbegin(a), std::cend(a),
  3.     std::ostream_iterator<int>{ std::cout, " " }, std::plus<>{}); // 1 3 6
  4. std::exclusive_scan(std::cbegin(a), std::cend(a),
  5.     std::ostream_iterator<int>{ std::cout, " " }, 0, std::plus<>{}); // 0 1 3
  6. const auto times_ten = [](const auto n) { return n * 10; };
  7. std::transform_inclusive_scan(std::cbegin(a), std::cend(a),
  8.     std::ostream_iterator<int>{ std::cout, " " }, std::plus<>{}, times_ten); // 10 30 60
  9. std::transform_exclusive_scan(std::cbegin(a), std::cend(a),
  10.     std::ostream_iterator<int>{ std::cout, " " }, 0, std::plus<>{}, times_ten); // 0 10 30
最大公约数和最小公倍数

最大公约数(GCD)和最小公倍数(LCM)
  1. const int p = 9;
  2. const int q = 3;
  3. std::gcd(p, q); // == 3
  4. std::lcm(p, q); // == 9
std::not_fn

实用函数,返回给定函数效果的否定
  1. const std::ostream_iterator<int> ostream_it{ std::cout, " " };
  2. const auto is_even = [](const auto n) { return n % 2 == 0; };
  3. std::vector<int> v{ 0, 1, 2, 3, 4 };
  4. // 打印所有偶数。
  5. std::copy_if(std::cbegin(v), std::cend(v), ostream_it, is_even); // 0 2 4
  6. // 打印所有奇数(非偶数)。
  7. std::copy_if(std::cbegin(v), std::cend(v), ostream_it, std::not_fn(is_even)); // 1 3
字符串与数字的相互转换

将整数和浮点数转换为字符串,反之亦然。这些转换是不抛出非常的,不会举行分配,而且比 C 尺度库中的等效函数更安全
用户负责为 std::to_chars 分配充足的存储空间,否则函数将通过在其返回值中设置错误代码对象来失败。
这些函数允许您可选地转达基数(默认为 10 进制)或浮点输入的格式说明符。
std::to_chars 返回一个(非 const)字符指针,指向函数在给定缓冲区内写入的字符串的末端,以及一个错误代码对象。
std::from_chars 返回一个 const 字符指针,成功时等于转达给函数的结束指针,以及一个错误代码对象。
这两个函数返回的错误代码对象在成功时都等于默认初始化的错误代码对象。
将数字 123 转换为 std::string:
  1. const int n = 123;
  2. // 可以使用任何容器,字符串,数组等。
  3. std::string str;
  4. str.resize(3); // 为 `n` 的每个数字分配足够的存储空间
  5. const auto [ ptr, ec ] = std::to_chars(str.data(), str.data() + str.size(), n);
  6. if (ec == std::errc{}) { std::cout << str << std::endl; } // 123
  7. else { /* 处理失败 */ }
从值为 “123” 的 std::string 转换为整数:
  1. const std::string str{ "123" };
  2. int n;
  3. const auto [ ptr, ec ] = std::from_chars(str.data(), str.data() + str.size(), n);
  4. if (ec == std::errc{}) { std::cout << n << std::endl; } // 123
  5. else { /* 处理失败 */ }
chrono 连续时间和时间点的舍入函数

为 std::chrono::duration 和 std::chrono::time_point 提供 abs、round、ceil 和 floor 辅助函数
  1. using seconds = std::chrono::seconds;
  2. std::chrono::milliseconds d{ 5500 };
  3. std::chrono::abs(d); // == 5s
  4. std::chrono::round<seconds>(d); // == 6s
  5. std::chrono::ceil<seconds>(d); // == 6s
  6. std::chrono::floor<seconds>(d); // == 5s
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
23阅读
0回复

暂无评论,点我抢沙发吧