Monday, February 15, 2010

Insert containers into container

A short note about the difference between inserting and adding containers to a container.

If we use += operator to add new containers to a container, they will be added as a consequence of usual values (plainly).


static void SisTestContainer(Args _args)
{
    container   c;
    container   pair;
    int         i;
    ;
    pair = [1, "first"];
    c += pair;
    pair = [2, "second"];
    c += pair;
    pair = [3, "third"];
    c += pair;

    infoLog.add(Exception::Info, strfmt("conlen %1", conlen(c)));
    
    for (i = 0; i<= conLen(c);i++)
    {
        pair = conPeek(c, i);
        infoLOg.add(Exception::Info, strfmt("%1; %2", conpeek(pair,1),conpeek(pair,2)));
    }

Output:

Message (16:25:24)
conlen 6
0; 0
1; 0
first; 0
2; 0
second; 0
3; 0
third; 0


If we use conins() operator, new containers will be added as containers (structured by pairs).


static void SisTestContainer(Args _args)
{
    container   c;
    container   pair;
    int         i;
    ;
    pair = [1, "first"];
    c = conins(c, 1, pair);
    pair = [2, "second"];
    c = conins(c, 2, pair);
    pair = [3, "third"];
    c = conins(c, 3, pair);
    
    infoLog.add(Exception::Info, strfmt("conlen %1", conlen(c)));

    for (i = 1; i<= conLen(c); i++)
    {
        pair = conPeek(c, i);
        infoLOg.add(Exception::Info, strfmt("%1; %2", conpeek(pair,1),conpeek(pair,2)));
    }

Output:

Message (16:10:17)
conlen 3
1; first
2; second
3; third



P.S.: as Dron AKA andy mentioned, the following syntax does the same:

c += [pair];

1 comment:

olaru.mircea said...

You helped me a lot today with this post. I was about to go crazy because of these containers in containers. Oh, I'm much better now. Thanks a lot!