BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Vectors for components and a problem

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (VCL Components Usage)
View previous topic :: View next topic  
Author Message
Mauro
Guest





PostPosted: Sun Jan 25, 2004 11:38 am    Post subject: Vectors for components and a problem Reply with quote



Hi all,

I would like to have a vector of TLabel. it's no problem
to create and manage them. But if I do it, I will have an
other problem that is already solved by you, I think.

My amount of labels depends on the program way that is executed.
This means that I could have 20 or 50 labels.
As I do for every new label a push_back(), the place in a vector
for this label is always different.
But in the runtime of a program I will have an acces to some of these
labels, but have no possibility to access them directly via index like
labels.begin() + X.

What do you in such a case? Have I to fill the Tag property or is there
another efficient way to do that?

thanks in advance,
Mauro


Back to top
Mauro
Guest





PostPosted: Sun Jan 25, 2004 11:45 am    Post subject: Re: Vectors for components and a problem Reply with quote



I have something to add here, at the moment I have 100 pointers to a label:
TLabel *labels[100];

With this method I create new labels always in the same array place.
This works great, but I have unneccassery memory usage, because
I don't need all 100 pointers in the same time.

Mauro

"Mauro" <mauro23 (AT) gmx (DOT) de> schrieb im Newsbeitrag
news:4013aabb (AT) newsgroups (DOT) borland.com...
Quote:
Hi all,

I would like to have a vector of TLabel. it's no problem
to create and manage them. But if I do it, I will have an
other problem that is already solved by you, I think.

My amount of labels depends on the program way that is executed.
This means that I could have 20 or 50 labels.
As I do for every new label a push_back(), the place in a vector
for this label is always different.
But in the runtime of a program I will have an acces to some of these
labels, but have no possibility to access them directly via index like
labels.begin() + X.

What do you in such a case? Have I to fill the Tag property or is there
another efficient way to do that?

thanks in advance,
Mauro





Back to top
Hans Galema
Guest





PostPosted: Sun Jan 25, 2004 4:17 pm    Post subject: Re: Vectors for components and a problem Reply with quote



Mauro wrote:

Quote:
TLabel *labels[100];

This works great, but I have unneccassery memory usage, because
I don't need all 100 pointers in the same time.

400 bytes are not that much to worrt about.

But to keep it clean, try:

int nlabels = 25;

TLabel **labels;

labels = new TLabel* [nlabels];

int labelnr = - 1;

while ( ++labelnr < nlabels )
labels [labelnr] = new TLabel ( this );

// use

labelnr = -1;

while ( ++labelnr < nlabels )
delete labels [labelnr];

delete [labels];

Hans.


Back to top
Hans Galema
Guest





PostPosted: Sun Jan 25, 2004 6:08 pm    Post subject: Re: Vectors for components and a problem Reply with quote

Hans Galema wrote:

Quote:
delete [labels];

Oh, noo...

delete [] labels;

Hans.

Back to top
Remy Lebeau (TeamB)
Guest





PostPosted: Sun Jan 25, 2004 10:38 pm    Post subject: Re: Vectors for components and a problem Reply with quote


"Mauro" <mauro23 (AT) gmx (DOT) de> wrote


Quote:
As I do for every new label a push_back(), the place
in a vector for this label is always different.
But in the runtime of a program I will have an acces to
some of these labels, but have no possibility to access
them directly via index like labels.begin() + X.

It is unclear what the actual problem is exactly. Please clearify.


Gambit



Back to top
JD
Guest





PostPosted: Mon Jan 26, 2004 5:42 am    Post subject: Re: Vectors for components and a problem Reply with quote


"Remy Lebeau (TeamB)" <gambit47.no.spam (AT) no (DOT) spam.yahoo.com> wrote:
Quote:

"Mauro" <mauro23 (AT) gmx (DOT) de> wrote in message
news:4013aabb (AT) newsgroups (DOT) borland.com...
to access them directly via index like labels.begin() + X.

It is unclear what the actual problem is exactly. Please clearify.

I think he's looking for something like:

labels[ x ]->Caption = "A different caption";

~ JD


Back to top
JD
Guest





PostPosted: Mon Jan 26, 2004 5:43 am    Post subject: Re: Vectors for components and a problem Reply with quote


Hans Galema <dontusethis (AT) dontusethis (DOT) nl> wrote:
Quote:
Hans Galema wrote:

delete [labels];

Oh, noo...

Had enough sleep lately? <vbg>

~ JD


Back to top
Remy Lebeau (TeamB)
Guest





PostPosted: Mon Jan 26, 2004 7:34 am    Post subject: Re: Vectors for components and a problem Reply with quote


"JD" <nospam (AT) nospam (DOT) com> wrote


Quote:
I think he's looking for something like:

labels[ x ]->Caption = "A different caption";

If that were the case, then why did he already state that using an index was
not an option?


Gambit



Back to top
JD
Guest





PostPosted: Mon Jan 26, 2004 7:44 am    Post subject: Re: Vectors for components and a problem Reply with quote


"Remy Lebeau (TeamB)" <gambit47.no.spam (AT) no (DOT) spam.yahoo.com> wrote:
Quote:

"JD" <nospam (AT) nospam (DOT) com> wrote in message
news:4014b6b8$1 (AT) newsgroups (DOT) borland.com...

I think he's looking for something like:

labels[ x ]->Caption = "A different caption";

If that were the case, then why did he already state that using an index was
not an option?

Oh, I see. He said:

"... but have no possibility to access them directly via
index like labels.begin() + X.".

I read that as a syntax problem where I bet you read that as he not knowing which index value to use for which label.

~ JD


Back to top
Mauro
Guest





PostPosted: Mon Jan 26, 2004 10:17 am    Post subject: Re: Vectors for components and a problem Reply with quote

It's no a technical but logical question.

I had an idea yesterday nad have created a vector:

vector <TLabel *> labels;

Then at the begining of my program I'm reserving
memory:

labels.reserve(100) // Maximum 100 labels at one time

Then if I have several parts of my program then I can
say that if include one is executed then create 10
labels, which pointers are stored in labels(0) - labels(9),
in include two I use the range 10 - 40 for 30 labels and so
on...

This brings me the advatage that if I have to access the
labels that have been created in include one from other
program part, then I know that they have the position 0 - 9
in the vector.

But originally as I have posted this topic then I have thoouhgt,
that i could make on every program part a push_back() to
this vector. But then I would have to remove the label
pointers from the vector, if they would be deleted and I would
not known, at which position are my labels from program part
one or program part two.

So at the end, I think, that my first idea that I have
described at the beginning, is ok, or not??

Regards,
Marius



Back to top
Duane Hebert
Guest





PostPosted: Mon Jan 26, 2004 2:21 pm    Post subject: Re: Vectors for components and a problem Reply with quote

"Mauro" <Mauro23 (AT) gmx (DOT) de> wrote

Quote:

It's no a technical but logical question.

I had an idea yesterday nad have created a vector:

vector <TLabel *> labels;

Then at the begining of my program I'm reserving
memory:

labels.reserve(100) // Maximum 100 labels at one time


In general, if you want a container that you can access by index,
a vector sounds correct. If you want an index that is immutable
for each entry, this sounds more like a key and a map may
be better suited. Please describe what you are trying to
accomplish.



Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (VCL Components Usage) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.