Add Element to list not Working
Chris Pons
cmpons at gmail.com
Sun Apr 1 23:18:33 PDT 2012
Thanks. I tried doing this and the list didn't update:
void AddToList( SList!int list, int i )
{
list.insert( i );
}
SList!int intList;
AddToList( intList, 42 );
but when I switched to this, it worked:
SList!int intList;
void AddToList( int i )
{
intList.insert( i );
}
AddToList( 42 );
The first method didn't give an error it just didn't update the
list as I thought. Any idea?
On Monday, 2 April 2012 at 06:07:40 UTC, Ali Çehreli wrote:
> On 04/01/2012 10:45 PM, Chris Pons wrote:
>> I'm trying to add an element to a list with insert but that
>> doesn't seem
>> to do anything at all. If I try using ~= it says that "Error:
>> cannot
>> append type Node to type SList!(Node). I'm pretty confused
>> about using
>> ~= because it works fine for arrays but apperantly not for
>> lists.
>>
>> How do I add an element to a list?
>
> import std.stdio;
> import std.container;
>
> void main()
> {
> auto l = SList!int();
>
> l.insert(42); // inserts front
> l.insert(43); // this too
>
> assert(l == SList!int(43, 42));
>
> // inserts after the specified range (l[] is the entire
> list)
> l.insertAfter(l[], 44);
>
> assert(l == SList!int(43, 42, 44));
>
> // This doesn't work because SList.Range doesn't define
> opOpAssign!"~"
> // l[] ~= 45;
> }
>
> Ali
More information about the Digitalmars-d-learn
mailing list