469,903 Members | 1,950 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,903 developers. It's quick & easy.

initialising a static std::vector data member

what is the best way of initializing a static std::vector data member
with some values?

(currently I just push_back some values in the constructor if the size
== 0)

thanks
Jul 22 '05 #1
2 10907
Dylan wrote:
what is the best way of initializing a static std::vector data member
with some values?


By static, you mean static member? You can initialize it with an array.

#include <vector>
#include <iostream>

struct Foo
{
static std::vector<int> vec;
private:
static const int array[];
};

const int Foo::array[] = { 1, 10, 100, 1000, 10000, 42 };
std::vector<int> Foo::vec(array, array + sizeof(array)/sizeof(*array));

int main()
{
std::cout << Foo::vec.back() << '\n';
}

If your vector is also constant, you might just as well use the array
directly instead.

Jul 22 '05 #2
thanks Rolf

On Wed, 12 Jan 2005 15:07:59 +0100, Rolf Magnus <ra******@t-online.de>
wrote:
Dylan wrote:
what is the best way of initializing a static std::vector data member
with some values?


By static, you mean static member? You can initialize it with an array.

#include <vector>
#include <iostream>

struct Foo
{
static std::vector<int> vec;
private:
static const int array[];
};

const int Foo::array[] = { 1, 10, 100, 1000, 10000, 42 };
std::vector<int> Foo::vec(array, array + sizeof(array)/sizeof(*array));

int main()
{
std::cout << Foo::vec.back() << '\n';
}

If your vector is also constant, you might just as well use the array
directly instead.


Jul 22 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

18 posts views Thread by Janina Kramer | last post: by
7 posts views Thread by Martin Magnusson | last post: by
16 posts views Thread by Honestmath | last post: by
17 posts views Thread by Michael Hopkins | last post: by
32 posts views Thread by zl2k | last post: by
6 posts views Thread by Jia | last post: by
2 posts views Thread by Rockair | last post: by
1 post views Thread by Waqarahmed | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.