468,512 Members | 1,408 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

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 4685

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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

11 posts views Thread by Steve | last post: by
16 posts views Thread by Honestmath | last post: by
24 posts views Thread by simon | last post: by
8 posts views Thread by Ross A. Finlayson | last post: by
7 posts views Thread by Dilip | last post: by
8 posts views Thread by jacek.dziedzic | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.