473,903 Members | 6,152 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 11141
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<in t> 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
2896
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 informatik about the players. CPlayer is a class that contains another std::vector<CPosition>. Because one of the players is the client itself (and the size of the vector<CPlayer> doesn't change during a game), i thought i could store a std::vector<CPlayer>::iterator "localplayer" that points to the...
7
3726
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 static attribute? My code is similar to the one posted below. The code below works, though. My original code crashes on the second call to X::f(). My question is: is this code valid, or is it just a coincidence that it doesn't crash?
4
4901
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 that the static member variable is unknown (unresolved external symbol) Below is what an example of the code I use. Can somebody tell me what I am doing wrong ? Thanks very much in advance, Serge //myclass.h
16
2195
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> m_duedates(100, Date());
17
3375
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 returns positions of elements etc. I am calling this uovec at the moment (for Unit-Offset VECtor). I want the class to respond correctly to all usage of STL containers and algorithms so that it is a transparent replacement for std:vector. The options seems to be:
32
69733
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 function is called. I can do like this: std::vector<int> fun(){ //build the vector v; return v; }
6
4015
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 <iostream> #include <vector> using namespace std; class super{ protected:
4
3524
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, destructor, or type conversion before '.' token vector_static_function.c:21: error: expected constructor, destructor, or type conversion before '.' token
2
5623
by: Rockair | last post by:
hi! there is a class: class card { static vector<string> names; //... };
0
9853
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10990
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10504
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9692
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
8057
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7215
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
6100
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4734
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4316
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.