在类中初始化静态向量最方便的方法是什么?

What is the most convenient way to initialize a static vector inside a class?

本文关键字:方便 方法 是什么 向量 初始化 静态      更新时间:2023-10-16

如果我想在类中初始化一个向量,例如:

class A {
  private:
    static std::vector<double> label_map;
};

如果我想初始化这个静态向量,最好的方法是什么?我在其他一些帖子中读到,从GCC 4.4开始,它支持c++ 0x的新功能,我们可以直接使用

static std::vector<double> label_map = {1, 2, 3, 4};

然而,它似乎不适合我

所以…包装:

// thefile.cpp
class Foo
{
    static std::vector<int> v;
};
std::vector<int> Foo::v { 1, 2, 3, 4 };

g++ -std=c++0x -c -o thefile.o thefile.cpp # ...编译