472,328 Members | 1,157 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,328 software developers and data experts.

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 11043
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
by: Janina Kramer | last post by:
hi ng, i'm working on a multiplayer game for a variable number of players and on the client side, i'm using a std::vector<CPlayer> to store...
7
by: Martin Magnusson | last post by:
I'm having trouble clearing and resizing a static std::vector of std::vectors (segmentation fault). Is it OK to call clear() and resize() on a...
4
by: Serge | last post by:
Hi, I have no problem creating a static member variable with integers, etc but when I try the same with a vector then I always get linker errors...
16
by: Honestmath | last post by:
Hi, I added the following line to my code within a class declaration: std::vector<Date> m_duedates(100); I also tried: std::vector<Date>...
17
by: Michael Hopkins | last post by:
Hi all I want to create a std::vector that goes from 1 to n instead of 0 to n-1. The only change this will have is in loops and when the vector...
32
by: zl2k | last post by:
hi, c++ user Suppose I constructed a large array and put it in the std::vector in a function and now I want to return it back to where the...
6
by: Jia | last post by:
Hi all, I have a class foo which has a static vector of pointers of type base class, and a static function to set this vector. #include...
4
by: Josefo | last post by:
Hello, is someone so kind to tell me why I am getting the following errors ? vector_static_function.c:20: error: expected constructor,...
2
by: Rockair | last post by:
hi! there is a class: class card { static vector<string> names; //... };
0
by: tammygombez | last post by:
Hey everyone! I've been researching gaming laptops lately, and I must say, they can get pretty expensive. However, I've come across some great...
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
1
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.