小编Bla*_*opa的帖子

C++在编译时计算和排序向量

我有一个class A具有std::vector<int>作为属性. AA创建实例时需要填充此向量.计算可能需要一些时间,我想知道是否:

  1. 它可以在编译时完成.
  2. 矢量也可以在编译时进行排序

我不熟悉元编程,我现在找不到办法.这不是特定于操作系统的问题.

这是A.cpp文件:

#include "A.h"
#define SIZEV 100

A::A()
{
    fillVector();
}

void A::fillVector()
{
    // m_vector is an attribute of class "A"
    // EXPECTATION 1 : fill the vector with the following calculation at compile time

    const int a=5;
    const int b=7;
    const int c=9;

    for(int i=0;i<SIZEV;i++){
        for(int j=0;j<SIZEV;j++){
            for(int k=0;k<SIZEV;k++){
                this->m_vector.push_back(a*i+b*j+c*k);
            }
        }
    }

    // EXPECTATION 2 : sort the vector as compile time 
} …
Run Code Online (Sandbox Code Playgroud)

c++ metaprogramming c++11

11
推荐指数
4
解决办法
4114
查看次数

标签 统计

c++ ×1

c++11 ×1

metaprogramming ×1