加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

C动态初始化const静态向量

发布时间:2020-12-16 10:23:36 所属栏目:百科 来源:网络整理
导读:我想将类Foo中的静态const std :: vector初始化为{0,1,2,3,…,n},其中n在编译时基于下面枚举中的Last值已知.目标是Foo :: all包含Fruit枚举的所有值. 在foo.h中: enum Fruit { Apple,Orange,Banana,...,Last };class Foo {public: static const vectorint a
我想将类Foo中的静态const std :: vector初始化为{0,1,2,3,…,n},其中n在编译时基于下面枚举中的Last值已知.目标是Foo :: all包含Fruit枚举的所有值.

在foo.h中:

enum Fruit { Apple,Orange,Banana,...,Last };

class Foo {
public:
    static const vector<int> all;
};

在foo.cpp中:

// initialization of Foo::all goes here.

解决方法

你可以使用boost :: irange:

auto range = boost::irange(0,n + 1);
const vector<int> Foo::numbers(range.begin(),range.end());

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读