简体   繁体   English

如何初始化静态向量成员?

[英]How to initialize static vector member?

For example 例如

struct A
{
    static vector<int> s;
};

vector<int> A::s = {1, 2, 3};

However, my compiler doesn't support initialization list. 但是,我的编译器不支持初始化列表。 Any way to implement it easily? 有什么办法可以轻松实现吗? Does lambda function help here? lambda函数在这里有用吗?

I always fear being shot down for initialization ordering here for questions like this, but.. 对于像这样的问题,我总是担心会因为初始化排序被击落,但是......

#include <iostream>
#include <vector>
#include <iterator>

struct A
{
    static std::vector<int> s;
};

static const int s_data[] = { 1,2,3 };
std::vector<int> A::s(std::begin(s_data), std::end(s_data));

int main()
{
    std::copy(A::s.begin(), A::s.end(), 
              std::ostream_iterator<int>(std::cout, " "));
    return 0;
}

Output 产量

1 2 3

Just because you can doesn't mean you should =P 因为你可以并不意味着你应该 = P

Winning the award for the least efficient way to do this: 最低效的方式赢得奖项:

#include <iostream>
#include <vector>
#include <cstdlib>
using namespace std;

template<typename T>
std::vector<T> v_init(const T& t)
{
    return std::vector<T>(1,t);
}

template<typename T, typename... Args>
std::vector<T> v_init(T&& t, Args&&... args)
{
    const T values[] = { t, args... };
    std::vector<T> v1(std::begin(values), std::end(values));
    return v1;
}

struct A
{
    static std::vector<int> s;
};

std::vector<int> A::s(v_init(1,2,3,4,5));


int main(int argc, const char *argv[])
{
    std::copy(A::s.begin(), A::s.end(), std::ostream_iterator<int>(std::cout, " "));
    return 0;
}

Output 产量

1 2 3 4 5 

This should puke at compile-time if T and anything in Args... is not type-compliant or type-castable. 如果T和Args中的任何东西都不符合类型或类型可转换,那么这应该在编译时呕吐。 Of course, if you have variadic templates odds are you also have initializer lists, but it makes for fun brain-food if nothing else. 当然,如果你有可变参数模板赔率,你也有初始化列表,但如果没有别的话,它会带来有趣的大脑食物。

Any way to implement it easily? 有什么办法可以轻松实现吗?

There's nothing particularly elegant. 没有什么特别优雅的。 You can either copy the data from a static array, or initialise it with the result of a function call. 您可以从静态数组中复制数据,也可以使用函数调用的结果对其进行初始化。 The former might use more memory than you'd like, and the latter needs some slightly messy code. 前者可能会使用比你想要的更多的内存,而后者需要一些稍微凌乱的代码。

Boost has a library to make that slightly less ugly: Boost有一个让它稍微不那么难看:

#include <boost/assign/list_of.hpp>
vector<int> A::s = boost::assign::list_of(1)(2)(3);

Does lambda function help here? lambda函数在这里有用吗?

Yes, it can save you from having to name a function just to initialise the vector: 是的,它可以让你不必为了初始化向量而命名一个函数:

vector<int> A::s = [] {
    vector<int> v;
    v.push_back(1);
    v.push_back(2); 
    v.push_back(3);
    return v;
}();

(Strictly speaking, this should have an explicit return type, []()->vector<int> , since the lambda body contains more than just a return statement. Some compilers will accept my version, and I believe it will become standard in 2014.) (严格来说,这应该有一个显式的返回类型, []()->vector<int> ,因为lambda主体不仅仅包含一个return语句。有些编译器会接受我的版本,我相信它会成为标准的2014年)

Write a simple init function for the vector: 为向量写一个简单的init函数:

vector<int> init()
{
  vector<int> v;
  v.reserve(3);
  v.push_back(1);
  v.push_back(2);
  v.push_back(3);
  return v;
};

vector<int> A::s = init();

You can initialize an std::vector from two pointers 您可以从两个指针初始化std::vector

int xv[] = {1,2,3,4,5,6,7,8,9};
std::vector<int> x(xv, xv+(sizeof(xv)/sizeof(xv[0])));

You can even factor this out in a template function: 您甚至可以在模板函数中考虑这一点:

template<typename T, int n>
std::vector<T> from_array(T (&v)[n]) {
    return std::vector<T>(v, v+n);
}

Another idea: 另一个想法:

struct A
{
  static std::vector<int> s;
};

std::vector<int> A::s;

static bool dummy((A::s.push_back(1), A::s.push_back(2), A::s.push_back(3), false));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM