C++ 如何初始化静态向量成员?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18853421/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to initialize static vector member?
提问by user1899020
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 函数在这里有帮助吗?
采纳答案by Mike Seymour
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 libraryto 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 年成为标准。)
回答by WhozCraig
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 candoesn't mean you should=P
仅仅因为你可以并不意味着你应该=P
Winning the award for the leastefficient 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. Of course, if you have variadic templates odds are you also have initializer lists, but it makes for fun brain-food if nothing else.
如果 T 和 Args... 中的任何内容都不是类型兼容或类型可转换的,则这应该在编译时呕吐。当然,如果你有可变参数模板,你也有初始化列表,但如果没有别的,它会很有趣。
回答by rubenvb
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();
回答by 6502
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);
}
回答by user1095108
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));