C++ 静态向量的初始化
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3701903/
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
Initialisation of static vector
提问by Xirdus
I wonder if there is the "nicer" way of initialising a static vector than below?
我想知道是否有比下面更“更好”的初始化静态向量的方法?
class Foo
{
static std::vector<int> MyVector;
Foo()
{
if (MyVector.empty())
{
MyVector.push_back(4);
MyVector.push_back(17);
MyVector.push_back(20);
}
}
}
It's an example code :)
这是一个示例代码:)
The values in push_back() are declared independly; not in array or something.
push_back() 中的值是独立声明的;不在数组或其他东西中。
Edit: if it isn't possible, tell me that also :)
编辑:如果不可能,也告诉我:)
采纳答案by Todd Gardner
Typically, I have a class for constructing containers that I use (like this onefrom boost), such that you can do:
通常,我有一个用于构建我使用的容器的类(例如boost 中的这个),这样您就可以执行以下操作:
const list<int> primes = list_of(2)(3)(5)(7)(11);
That way, you can make the static const as well, to avoid accidental modifications.
这样,您也可以创建静态常量,以避免意外修改。
For a static, you could define this in the .cc file:
对于静态,您可以在 .cc 文件中定义它:
// Foo.h
class Foo {
static const vector<int> something;
}
// Foo.cc
const vector<int> Foo::something = list_of(3)(5);
In C++Ox, we'll have a language mechanism to do this, using initializer lists, so you could just do:
在 C++Ox 中,我们将有一个语言机制来做到这一点,使用初始化列表,所以你可以这样做:
const vector<int> primes({2, 3, 5, 7, 11});
See here.
见这里。
回答by Mike Seymour
In C++03, the easiest way was to use a factory function:
在 C++03 中,最简单的方法是使用工厂函数:
std::vector<int> MakeVector()
{
std::vector v;
v.push_back(4);
v.push_back(17);
v.push_back(20);
return v;
}
std::vector Foo::MyVector = MakeVector(); // can be const if you like
"Return value optimisation" should mean that the array is filled in place, and not copied, if that is a concern. Alternatively, you could initialise from an array:
“返回值优化”应该意味着数组被填充到位,而不是复制,如果这是一个问题。或者,您可以从数组初始化:
int a[] = {4,17,20};
std::vector Foo::MyVector(a, a + (sizeof a / sizeof a[0]));
If you don't mind using a non-standard library, you can use Boost.Assignment:
如果你不介意使用非标准库,你可以使用 Boost.Assignment:
#include <boost/assign/list_of.hpp>
std::vector Foo::MyVector = boost::list_of(4,17,20);
In C++11 or later, you can use brace-initialisation:
在 C++11 或更高版本中,您可以使用大括号初始化:
std::vector Foo::MyVector = {4,17,20};
回答by syvex
With C++11:
使用 C++11:
std::vector<int> Foo::MyVector = {4, 17, 20};
回答by Alexander Rautenberg
You could try this one:
你可以试试这个:
int arr[] = { 1,2,3,4,5,6,7,8,9 };
MyVector.insert(MyVector.begin(), arr, &arr[sizeof(arr)/ sizeof(*arr)]);
But it's probably only worth when you have a really long vector, and it doesn't look much nicer, either. However, you get rid of the repeated push_back() calls. Of course, if your values are "not in an array" you'd have to put them into there first, but you'd be able to do that statically (or at least references/pointers), depending on the context.
但它可能只有当你有一个非常长的向量时才值得,而且它看起来也不太好。但是,您摆脱了重复的 push_back() 调用。当然,如果您的值“不在数组中”,您必须先将它们放入那里,但您可以静态地(或至少引用/指针)这样做,具体取决于上下文。
回答by bill
How about initializing using a static object. In its constuctor it could call a static function in the object to do the initalization.
如何使用静态对象初始化。在其构造函数中,它可以调用对象中的静态函数来进行初始化。
回答by J-Mik
with boost you can use the +=() operator defined in the boost::assign namespace.
使用 boost,您可以使用在 boost::assign 命名空间中定义的 +=() 运算符。
#include <boost/assign.hpp>
using namespace boost::assign;
int main()
{
static std::vector<int> MyVector;
MyVector += 4,17,20;
return 0;
}
or with static initialization:
或静态初始化:
#include <boost/assign.hpp>
using namespace boost::assign;
static std::vector<int> myVector = list_of(4)(17)(2);
int main()
{
return 0;
}
or even better, if your compiler supports C++ 11, use initialization lists.
甚至更好,如果您的编译器支持 C++ 11,请使用初始化列表。