使用范围库初始化std :: vector [英] Initializing std::vector with ranges library

查看:79
本文介绍了使用范围库初始化std :: vector的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用一系列连续的整数初始化 std :: vector 而不键入所有的整数,例如第二行,它不会编译此代码段:

I would like to initialize std::vector with a range of consecutive integers without typing all of them, something like a second line, which doesn't compile, in this code snippet:

  std::vector<int> a{0, 1, 2, 3, 4, 5};
  std::vector<int> b{std::ranges::iota_view(0, 5)};  // ERROR!

当然,我会更喜欢:

  std::vector<int> b{0:5};

,但这不是C ++ 41标准之前的时间。有任何想法如何在C ++ 20中实现吗?

but this is not scheduled before C++41 standard. Any ideas how to do it in C++20?

推荐答案

您正在寻找的是

auto b=std::ranges::to<std::vector>(std::ranges::iota_view(0, 5));

不幸的是,提案错过了C ++ 20,仅仅是因为没有时间审查其措辞(在之前的版本中添加了您尝试过的构造函数不可行)。希望它将在C ++ 23周期的早期合并并实现。

Unfortunately, that proposal missed C++20 simply because there wasn’t time to review its wording (after a previous version that added the constructor you tried was found unworkable). Hopefully it’ll be merged—and implemented—early in the C++23 cycle.

这篇关于使用范围库初始化std :: vector的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆