473,471 Members | 1,995 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

std::vector - allocation size at declaration?


Hi,

I want to be able to write:
class foo
{
std::vector<intm_i (64);
}

which creates a vector, that constructs with size=64;

Is there a std:: way to sdo this, or must I make a template class
like:

template class<type T, int fixsizemyvec:public std::vector<T>
{
myvec() : std::vector<T>(fixsize) {}
};

--
-Gernot
int main(int argc, char** argv) {printf
("%silto%c%cf%cgl%ssic%ccom%c", "ma", 58, 'g', 64, "ba", 46, 10);}

________________________________________
Looking for a good game? Do it yourself!
GLBasic - you can do
www.GLBasic.com
Aug 9 '06 #1
9 5096

Gernot Frisch wrote:
Hi,

I want to be able to write:
class foo
{
std::vector<intm_i (64);
}
Use the same technique you do below and initialize during construction.
>
which creates a vector, that constructs with size=64;

Is there a std:: way to sdo this, or must I make a template class
like:

template class<type T, int fixsizemyvec:public std::vector<T>
{
myvec() : std::vector<T>(fixsize) {}
};

--
-Gernot
int main(int argc, char** argv) {printf
("%silto%c%cf%cgl%ssic%ccom%c", "ma", 58, 'g', 64, "ba", 46, 10);}

________________________________________
Looking for a good game? Do it yourself!
GLBasic - you can do
www.GLBasic.com
Aug 9 '06 #2
Gernot Frisch wrote:
I want to be able to write:
class foo
{
std::vector<intm_i (64);
}

which creates a vector, that constructs with size=64;
Declaration of a member doesn't construct anything. If you want to
initialise your member with a certain expression, use the constructor
initialiser list.
Is there a std:: way to sdo this, [..]
Yes, it's called "member initialiser list". Look it up.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 9 '06 #3

"Gernot Frisch" <Me@Privacy.neta écrit dans le message de news:
4j************@individual.net...
>
Hi,

I want to be able to write:
class foo
{
std::vector<intm_i (64);
}

which creates a vector, that constructs with size=64;

Is there a std:: way to sdo this, or must I make a template class like:
struct foo
{
std::vector<intm_i;

foo() : m_i(64) {} // construction with m_i initialized
};

One of the vector constructor takes a size_type as parameter.
it uses that to resize the vector at construction. After construction
your vector has 64 int allocated (in this case).

the following will also allocate 64 int in your vector but without using the
ctor initializer list

struct foo
{
std::vector<intm_i;

foo()
{
m_i.resize(64);
}
};

Here m_i is first constructed by the way of vector's default constructor.
Even if you dont type it, the foo constructor looks like this

[...]
foo() :
m_i() // this calls the constructor taking no arguments
{
m_i.resize(64);
}
[...]
So aftyer construction, the vector has a size of 0.
After that it is resized with his resize member function.
Eric
Aug 10 '06 #4
>I want to be able to write:
>>

class foo
{
std::vector<intm_i (64);
}

Use the same technique you do below and initialize during
construction.
>>
which creates a vector, that constructs with size=64;

Is there a std:: way to do this, or must I make a template class
like:

template class<type T, int fixsizemyvec:public std::vector<T>
{
myvec() : std::vector<T>(fixsize) {}
};
This should be in the std next version. I think it's a very convenient
way of transforming old code:
struct foo
{
int bar[MAX_SIZE];
};

into
struct foo
{
fix_vec<int, MAX_SIZEbar;
};


Aug 10 '06 #5
On 2006-08-09 12:15:02 -0400, "Gernot Frisch" <Me@Privacy.netsaid:
>
Hi,

I want to be able to write:
class foo
{
std::vector<intm_i (64);
}

which creates a vector, that constructs with size=64;

Is there a std:: way to sdo this, or must I make a template class like:

template class<type T, int fixsizemyvec:public std::vector<T>
{
myvec() : std::vector<T>(fixsize) {}
};
How about:

class foo
{
std::vector<intm_i;

public:
foo() : m_i(64) {}
};
--
Clark S. Cox, III
cl*******@gmail.com

Aug 10 '06 #6
Gernot Frisch wrote:
This should be in the std next version. I think it's a very convenient
way of transforming old code:
struct foo
{
int bar[MAX_SIZE];
};

into
struct foo
{
fix_vec<int, MAX_SIZEbar;
};
It already is. Check out std::tr1::array (aka boost::array).

Cheers! --M

Aug 10 '06 #7
On 2006-08-10 03:53:10 -0400, "Gernot Frisch" <Me@Privacy.netsaid:
>>I want to be able to write:
class foo
{
std::vector<intm_i (64);
}

Use the same technique you do below and initialize during construction.
>>>
which creates a vector, that constructs with size=64;

Is there a std:: way to do this, or must I make a template class
like:

template class<type T, int fixsizemyvec:public std::vector<T>
{
myvec() : std::vector<T>(fixsize) {}
};

This should be in the std next version. I think it's a very convenient
way of transforming old code:
struct foo
{
int bar[MAX_SIZE];
};

into
struct foo
{
fix_vec<int, MAX_SIZEbar;
};
Take a look at boost::array.

<http://www.boost.org/doc/html/array.html>
--
Clark S. Cox, III
cl*******@gmail.com

Aug 10 '06 #8
How about:

class foo
{
std::vector<intm_i;

public:
foo() : m_i(64) {}
};
the c'tor is in a different file. I had 20 classes all implemented in
on cpp, but different header - no very clever... It worked my way.
Thank you.
Aug 10 '06 #9

Gernot Frisch wrote:
I want to be able to write:
class foo
{
std::vector<intm_i (64);
}
Use the same technique you do below and initialize during
construction.
>
which creates a vector, that constructs with size=64;

Is there a std:: way to do this, or must I make a template class
like:

template class<type T, int fixsizemyvec:public std::vector<T>
{
myvec() : std::vector<T>(fixsize) {}
};

This should be in the std next version. I think it's a very convenient
way of transforming old code:
struct foo
{
int bar[MAX_SIZE];
};

into
struct foo
{
fix_vec<int, MAX_SIZEbar;
};
This is not the same code. Using fix_vec, your vector would still be
resizeable.

/Peter

Aug 10 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Steve | last post by:
Hi, I'm using a std::vector to store a list of user defined objects. The vector may have well over 1000 elements, and I'm suffering a performance hit. If I use push_back I get a much worse...
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> m_duedates(100, Date());
20
by: Anonymous | last post by:
Is there a non-brute force method of doing this? transform() looked likely but had no predefined function object. std::vector<double> src; std::vector<int> dest; ...
24
by: simon | last post by:
Hi, First some background. I have a structure, struct sFileData { char*sSomeString1; char*sSomeString2;
4
by: John | last post by:
I have a function declaration that gives an error while compiling. Can anyone help me figure this one out? inline void create(const std::vector< myclass >& plist, std::vector< myclass...
8
by: Ross A. Finlayson | last post by:
I'm trying to write some C code, but I want to use C++'s std::vector. Indeed, if the code is compiled as C++, I want the container to actually be std::vector, in this case of a collection of value...
7
by: Dilip | last post by:
If you reserve a certain amount of memory for a std::vector, what happens when a reallocation is necessary because I overshot the limit? I mean, say I reserve for 500 elements, the insertion of...
4
by: mathieu | last post by:
Hello, I am looking at the API of std::vector but I cannot find a way to specify explicitely the size of my std::vector. I would like to avoid vector::resize since it first initializes the...
8
by: jacek.dziedzic | last post by:
Hi! I need to be able to track memory usage in a medium-sized application I'm developing. The only significant (memory-wise) non- local objects are of two types -- std::vector<and of a custom...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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,...
1
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...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...
0
muto222
php
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.