Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > initialising a static std::vector data member

Reply
Thread Tools

initialising a static std::vector data member

 
 
Dylan
Guest
Posts: n/a
 
      01-12-2005
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
 
Reply With Quote
 
 
 
 
Rolf Magnus
Guest
Posts: n/a
 
      01-12-2005
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.

 
Reply With Quote
 
 
 
 
Dylan
Guest
Posts: n/a
 
      01-12-2005
thanks Rolf

On Wed, 12 Jan 2005 15:07:59 +0100, Rolf Magnus <(E-Mail Removed)>
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.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Initialising map member without copy Paul Brettschneider C++ 23 03-05-2008 09:25 PM
Can a static member function access non-static member? dolphin C++ 3 12-05-2007 12:39 PM
initialising static member with static methode Jens Henrik Goebbert C++ 3 10-31-2007 11:54 PM
What is the correct grammar to make a function call by using static member data which is a pointer to a ordinary class member function? zaeminkr@gmail.com C++ 3 07-06-2007 12:50 PM
explicitly initialising member vars - necessary? Alex Hunsley Java 4 10-27-2003 04:51 AM



Advertisments