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 

TList and TDateTime*
Goto page 1, 2, 3  Next
 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (Language C++)
View previous topic :: View next topic  
Author Message
Gallo_Teo
Guest





PostPosted: Sat Feb 11, 2006 9:03 am    Post subject: TList and TDateTime* Reply with quote



Hi to all,
i need to store some TDateTime in a list.

I've done it in this way:
TDateTime *t = new TDateTime(0,0,10,0);
List->Add(t);

As i read in some post of Gambit, it is not suggested to use it with the
operator NEW.
infact i have a lot of probelm with the operator like this example

TDateTime *t1 = (TDateTime*)List->Items[0];
TDateTime *t2 = (TDateTime*)List->Items[1];

if i want to find the smallest i have to do

if( *t1 < *t2)
{....}
otherwise the compiler check the pointer i think...

but the real problem is with the operation because if i want to add a
second to t1 i have to do
TDateTime *myNewTime = new TDateTime(*t1 + TDateTime(0,0,1,0))

the compiler doesn't give any error but the sum it's not ever correct and
some time t1 change but i can't understand why.

so, how can i store TDateTime Objects in a TList and do perations with them?


Teo
Back to top
JD
Guest





PostPosted: Sat Feb 11, 2006 4:03 pm    Post subject: Re: TList and TDateTime* Reply with quote



"Gallo_Teo" <galloteo (AT) infinito (DOT) it> wrote:
Quote:

i need to store some TDateTime in a list.

I suspect that you'll get several responses because to
a point, it's a matter of choice.

Just off of the top of my head, I think that a TStringList
might be a better choice. For example:

TStringList *pList = NULL;
//-------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner)
{
pList = new TStringList;
}
//-------------------------------------------------------------
__fastcall TForm1::~TForm1()
{
delete pList;
}
//-------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
for( int x = 0; x < 10; ++x )
{
pList->Add( DateTimeToStr(Now() + x) );
}
for( int x = 0; x < pList->Count; ++x )
{
TDateTime dt = StrToDateTime( pList->Strings[x] );
ShowMessage( DateTimeToStr(dt) );
}
}
//-------------------------------------------------------------

Not only do you not have to worry about memory leaks when
using a TList, TStringList also has an Objects property that
you can use to associate a particular date with a particular
object.

~ JD
Back to top
Liz Albin
Guest





PostPosted: Sat Feb 11, 2006 4:03 pm    Post subject: Re: TList and TDateTime* Reply with quote



On 11 Feb 2006 07:16:32 -0700, JD wrote:

Quote:
Not only do you not have to worry about memory leaks when
using a TList, TStringList also has an Objects property that
you can use to associate a particular date with a particular
object.

Let me just add that if you want you can use an auto_ptr for the
TStringList, and then not worry about allocation and deletion
--
liz
Back to top
Duane Hebert
Guest





PostPosted: Sat Feb 11, 2006 5:03 pm    Post subject: Re: TList and TDateTime* Reply with quote

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

"Gallo_Teo" <galloteo (AT) infinito (DOT) it> wrote:

i need to store some TDateTime in a list.

I suspect that you'll get several responses because to
a point, it's a matter of choice.

My choice would be:

std::list<TDateTime> DateTimeList;
DateTimeList.insert(...);

No casting required to access anything.
No pointers to worry about cleaning up.
Back to top
Dennis Jones
Guest





PostPosted: Sat Feb 11, 2006 5:03 pm    Post subject: Re: TList and TDateTime* Reply with quote

----- Original Message -----
From: "Dennis Jones" <nospam (AT) nospam (DOT) com>
Newsgroups: borland.public.cppbuilder.language.cpp
Sent: Saturday, February 11, 2006 8:22 AM
Subject: Re: TList and TDateTime*


Quote:
If you just want to change the value of one element in the list, you don't
have to create a new pointer to do that. Just dereference the pointer and
change the value it points to:

*List->Items[0] += TDateTime(0,0,1,0);

Oops, I forgot the cast:

*(TDateTime *)List->Items[0] += TDateTime(0,0,1,0);

That's another good reason not to use TList -- you have to remember to cast
everything. With an STL container you don't have to remember/worry about
casting.

- Dennis
Back to top
Dennis Jones
Guest





PostPosted: Sat Feb 11, 2006 5:03 pm    Post subject: Re: TList and TDateTime* Reply with quote

"Gallo_Teo" <galloteo (AT) infinito (DOT) it> wrote in message
news:43ed9cdb$1 (AT) newsgroups (DOT) borland.com...
Quote:

Hi to all,
i need to store some TDateTime in a list.

I've done it in this way:
TDateTime *t = new TDateTime(0,0,10,0);
List->Add(t);

As i read in some post of Gambit, it is not suggested to use it with the
operator NEW.
infact i have a lot of probelm with the operator like this example

TDateTime *t1 = (TDateTime*)List->Items[0];
TDateTime *t2 = (TDateTime*)List->Items[1];

Gambit's right. You should avoid storing pointers that you allocate in a
TList. Not that you can't do it, but it is very difficult to manage all the
pointers and make sure they all get deleted at the proper time.

Quote:
if i want to find the smallest i have to do

if( *t1 < *t2)
{....}
otherwise the compiler check the pointer i think...

There is nothing wrong with that, but you could also do this:

TDateTime t1 = *(TDateTime*)List->Items[0];
TDateTime t2 = *(TDateTime*)List->Items[1];

if ( t1 < t2 )
{...}


Quote:
but the real problem is with the operation because if i want to add a
second to t1 i have to do
TDateTime *myNewTime = new TDateTime(*t1 + TDateTime(0,0,1,0))

the compiler doesn't give any error but the sum it's not ever correct and
some time t1 change but i can't understand why.

If you just want to change the value of one element in the list, you don't
have to create a new pointer to do that. Just dereference the pointer and
change the value it points to:

*List->Items[0] += TDateTime(0,0,1,0);


Quote:
so, how can i store TDateTime Objects in a TList and do perations with
them?


Well, as others will probably suggest, don't use TList. If there is no
specific reason to use TList, I'd suggest using an STL container, such as
std::vector, instead. Then you won't have to allocate any memory at all:

std::vector<TDateTime> List;

List.push_back( TDateTime(0,0,10,0) );
List.push_back( TDateTime(0,0,11,0) );

To find the smaller of two times:

if ( List[0] < List[1] )
{...}

To change an existing time from the list:

List[0] += TDateTime(0,0,1,0);

- Dennis
Back to top
JD
Guest





PostPosted: Sun Feb 12, 2006 5:09 am    Post subject: Re: TList and TDateTime* Reply with quote

Liz Albin <lizalbinNotThis (AT) yahoo (DOT) com> wrote:
Quote:
On 11 Feb 2006 07:16:32 -0700, JD wrote:

Let me just add that if you want you can use an auto_ptr for
the TStringList, and then not worry about allocation and
deletion

Can auto_ptr be used for something other than a local variable?

~ JD
Back to top
JD
Guest





PostPosted: Sun Feb 12, 2006 5:09 am    Post subject: Re: TList and TDateTime* Reply with quote

"Duane Hebert" <spoo (AT) flarn2 (DOT) com> wrote:
Quote:

My choice would be:

std::list<TDateTime> DateTimeList;
DateTimeList.insert(...);

No casting required to access anything.
No pointers to worry about cleaning up.

I confess to being relatively ignorant of anything std except
vectors and bits but it sure does look like a better choice
(presuming that one can maintain order like with an Add
method).

~ JD
Back to top
Duane Hebert
Guest





PostPosted: Sun Feb 12, 2006 2:03 pm    Post subject: Re: TList and TDateTime* Reply with quote

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

"Duane Hebert" <spoo (AT) flarn2 (DOT) com> wrote:

I confess to being relatively ignorant of anything std except
vectors and bits but it sure does look like a better choice
(presuming that one can maintain order like with an Add
method).

std::list has push_back() and push_front().
It also has a sort().
Back to top
AlisdairM
Guest





PostPosted: Sun Feb 12, 2006 3:03 pm    Post subject: Re: TList and TDateTime* Reply with quote

JD wrote:

Quote:
Can auto_ptr be used for something other than a local variable?

Yes, but its copy semantics do make it rather awkward and unusual. For
instance, I often liked to use std::auto_ptr as the return type of a
factory function.

Generally, you will want to use a different kind of smart pointer for
storing in containers. Simplest recommendation is boost::shared_ptr,
or std::tr1::shared_ptr as TR1 implementations become available.

If you want to get fancy, you can go looking for other more specialized
pointer libraries (I still like boost::scoped_ptr) but for general
purpose daily use - shared_ptr is an incredible balancing act of
efficiency vs. utility.

[It works with containers, it stores 'incomplete types', it supports
customer deleters to handle arrays/COM/...]

--
AlisdairM(TeamB)
Back to top
AlisdairM
Guest





PostPosted: Sun Feb 12, 2006 3:03 pm    Post subject: Re: TList and TDateTime* Reply with quote

Duane Hebert wrote:

Quote:
std::list has push_back() and push_front().
It also has a sort().

Note that sort is also a standard algorithm, for any range taking
random-access iterators.

The reason list has a sort function is that there are some reasonably
efficient sort algorithms that can work on that data structure too, and
work a whole lot better than bubble-sorting with mere bi-directional
iterators!

'Normally' though, you would choose a container offering random access
and the standard sort algorithm.

[Just in case someone is wondering why std::list has a sort function,
but not std::vector, std::deque, std::basic_string, std::tr1::array,
....]

--
AlisdairM(TeamB)
Back to top
Gallo_Teo
Guest





PostPosted: Mon Feb 13, 2006 1:03 am    Post subject: Re: TList and TDateTime* Reply with quote

Thanks but a have to works also with ms and use code and decode time
in a string in my format. but that's not a problem
thanks good idea
"JD" <nospam (AT) nospam (DOT) com> ha scritto nel messaggio
news:43edffd0$1 (AT) newsgroups (DOT) borland.com...
Quote:


"Gallo_Teo" <galloteo (AT) infinito (DOT) it> wrote:

i need to store some TDateTime in a list.

I suspect that you'll get several responses because to
a point, it's a matter of choice.

Just off of the top of my head, I think that a TStringList
might be a better choice. For example:

TStringList *pList = NULL;
//-------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner)
{
pList = new TStringList;
}
//-------------------------------------------------------------
__fastcall TForm1::~TForm1()
{
delete pList;
}
//-------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
for( int x = 0; x < 10; ++x )
{
pList->Add( DateTimeToStr(Now() + x) );
}
for( int x = 0; x < pList->Count; ++x )
{
TDateTime dt = StrToDateTime( pList->Strings[x] );
ShowMessage( DateTimeToStr(dt) );
}
}
//-------------------------------------------------------------

Not only do you not have to worry about memory leaks when
using a TList, TStringList also has an Objects property that
you can use to associate a particular date with a particular
object.

~ JD

Back to top
Gallo_Teo
Guest





PostPosted: Mon Feb 13, 2006 3:03 am    Post subject: Re: TList and TDateTime* Reply with quote

Hi, first to all i would like to thanks everybody.
I tried to use std::list but in the help i find only few things.
i understood how to create it but not how to use. it is not a class with
method and property?

i wrote
std::list<TDateTime> DateTimeList ;
TDateTime Pippo = TDateTime(0,15,0,50) ;
DateTimeList.insert(Pippo);

but it doesn't works.
how have i to use list?

thanks

"Duane Hebert" <spoo (AT) flarn2 (DOT) com> ha scritto nel messaggio
news:43ee1217$1 (AT) newsgroups (DOT) borland.com...
Quote:


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

"Gallo_Teo" <galloteo (AT) infinito (DOT) it> wrote:

i need to store some TDateTime in a list.

I suspect that you'll get several responses because to
a point, it's a matter of choice.

My choice would be:

std::list<TDateTime> DateTimeList;
DateTimeList.insert(...);

No casting required to access anything.
No pointers to worry about cleaning up.

Back to top
Gallo_Teo
Guest





PostPosted: Mon Feb 13, 2006 3:03 am    Post subject: Re: TList and TDateTime* Reply with quote

Hi Dennis, i'll try now.
Thanks


"Dennis Jones" <nospam (AT) nospam (DOT) com> ha scritto nel messaggio
news:43ee103b$1 (AT) newsgroups (DOT) borland.com...
Quote:


----- Original Message -----
From: "Dennis Jones" <nospam (AT) nospam (DOT) com
Newsgroups: borland.public.cppbuilder.language.cpp
Sent: Saturday, February 11, 2006 8:22 AM
Subject: Re: TList and TDateTime*


If you just want to change the value of one element in the list, you
don't
have to create a new pointer to do that. Just dereference the pointer
and
change the value it points to:

*List->Items[0] += TDateTime(0,0,1,0);

Oops, I forgot the cast:

*(TDateTime *)List->Items[0] += TDateTime(0,0,1,0);

That's another good reason not to use TList -- you have to remember to
cast
everything. With an STL container you don't have to remember/worry about
casting.

- Dennis


Back to top
Dennis Jones
Guest





PostPosted: Mon Feb 13, 2006 4:03 am    Post subject: Re: TList and TDateTime* Reply with quote

"Gallo_Teo" <galloteo (AT) infinito (DOT) it> wrote in message
news:43efe6a9$1 (AT) newsgroups (DOT) borland.com...
Quote:

Hi, first to all i would like to thanks everybody.
I tried to use std::list but in the help i find only few things.
i understood how to create it but not how to use. it is not a class with
method and property?

i wrote
std::list<TDateTime> DateTimeList ;
TDateTime Pippo = TDateTime(0,15,0,50) ;
DateTimeList.insert(Pippo);

but it doesn't works.
how have i to use list?

thanks

Since you don't say what doesn't work, I can only guess that the problem is
that you have not included the necessary header file:

#include <list> // make sure you include this!

std::list<TDateTime> DateTimeList ;
TDateTime Pippo = TDateTime(0,15,0,50) ;
DateTimeList.insert(Pippo);

- Dennis
Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (Language C++) All times are GMT
Goto page 1, 2, 3  Next
Page 1 of 3

 
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.