简体   繁体   English

静态向量的初始化

[英]Initialisation of static vector

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; push_back()中的值是独立声明的; not in array or something. 不是数组或其他东西。

Edit: if it isn't possible, tell me that also :) 编辑:如果不可能,告诉我也:)

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};

Typically, I have a class for constructing containers that I use (like this one from 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. 这样,你也可以制作静态const,以避免意外修改。

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 . 看到这里

使用C ++ 11:

std::vector<int> Foo::MyVector = {4, 17, 20};

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. 但是,你摆脱了重复的push_back()调用。 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. 当然,如果你的值“不在一个数组中”,你必须首先将它们放在那里,但你可以静态地(或者至少是引用/指针),这取决于上下文。

How about initializing using a static object. 如何使用静态对象初始化。 In its constuctor it could call a static function in the object to do the initalization. 在它的构造函数中,它可以调用对象中的静态函数来进行初始化。

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,请使用初始化列表。

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

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