静态向量的初始化

发布于 2024-09-18 13:02:27 字数 380 浏览 3 评论 0原文

我想知道是否有比下面更好的初始化静态向量的方法?

class Foo
{
    static std::vector<int> MyVector;
    Foo()
    {
        if (MyVector.empty())
        {
            MyVector.push_back(4);
            MyVector.push_back(17);
            MyVector.push_back(20);
        }
    }
}

这是一个示例代码:)

Push_back() 中的值是独立声明的;不在数组或其他东西中。

编辑:如果不可能,也告诉我:)

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.

Edit: if it isn't possible, tell me that also :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

被翻牌 2024-09-25 13:02:27

在 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

“返回值优化”应该意味着数组被填充到位,而不是复制(如果这是一个问题)。或者,您可以从数组初始化:

int a[] = {4,17,20};
std::vector Foo::MyVector(a, a + (sizeof a / sizeof a[0]));

如果您不介意使用非标准库,则可以使用 Boost.Assignment:

#include <boost/assign/list_of.hpp>

std::vector Foo::MyVector = boost::list_of(4,17,20);

在 C++11 或更高版本中,您可以使用大括号初始化:

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

In C++03, the easiest way was to use a factory function:

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:

#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:

std::vector Foo::MyVector = {4,17,20};
冬天旳寂寞 2024-09-25 13:02:27

使用 C++11:

//The static keyword is only used with the declaration of a static member, 
//inside the class definition, not with the definition of that static member:
std::vector<int> Foo::MyVector = {4, 17, 20};

With C++11:

//The static keyword is only used with the declaration of a static member, 
//inside the class definition, not with the definition of that static member:
std::vector<int> Foo::MyVector = {4, 17, 20};
雨轻弹 2024-09-25 13:02:27

通常,我有一个用于构建我使用的容器的类(例如 这个来自 boost 的),这样你就可以这样做:

const list<int> primes = list_of(2)(3)(5)(7)(11);

这样,你也可以将 static const 化,以避免意外修改。

对于静态,您可以在 .cc 文件中定义它:

// Foo.h

class Foo {
  static const vector<int> something;
}

// Foo.cc

const vector<int> Foo::something = list_of(3)(5);

在 C++Ox 中,我们将有一个语言机制来使用初始值设定项列表来执行此操作,因此您可以这样做:

const vector<int> primes({2, 3, 5, 7, 11});

请参阅 此处

Typically, I have a class for constructing containers that I use (like this one from boost), such that you can do:

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:

// 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:

const vector<int> primes({2, 3, 5, 7, 11});

See here.

枯叶蝶 2024-09-25 13:02:27

你可以尝试这个:

int arr[] = { 1,2,3,4,5,6,7,8,9 };
MyVector.insert(MyVector.begin(), arr, &arr[sizeof(arr)/ sizeof(*arr)]);

但它可能只有当你有一个很长的向量时才值得,而且它看起来也好不了多少。但是,您可以摆脱重复的 Push_back() 调用。当然,如果您的值“不在数组中”,您必须先将它们放入其中,但是您可以根据上下文静态地执行此操作(或至少是引用/指针)。

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.

蓝礼 2024-09-25 13:02:27

使用静态对象进行初始化怎么样?在它的构造函数中
可以调用对象中的静态函数来进行初始化。

How about initializing using a static object. In its constuctor it
could call a static function in the object to do the initalization.

夏末 2024-09-25 13:02:27

对于 boost,您可以使用 boost::assign 命名空间中定义的 +=() 运算符。

#include <boost/assign.hpp>

using namespace boost::assign;

int main()
{
    static std::vector<int> MyVector;
    MyVector += 4,17,20;
    return 0;
}

或使用静态初始化:

#include <boost/assign.hpp>

using namespace boost::assign;

static std::vector<int> myVector = list_of(4)(17)(2);

int main()
{
    return 0;
}

或者更好的是,如果您的编译器支持 C++ 11,请使用初始化列表。

with boost you can use the +=() operator defined in the boost::assign namespace.

#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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文