"Tileing" a Container

Discussion in 'C++' started by woessner@gmail.com, Jun 30, 2006.

  1. Guest

    I've been flipping through the Josuttis book trying to find it there's
    a better way to accomplish this. Haven't come across anything so I
    thought I'd turn to the group.

    Suppose I have 2 containers, a large one and a small one. I would like
    to replace the elements of the larger container with the elements of
    the smaller container, but in such a way the the elements are repeated
    (or tiled, hence the subject) until the larger container is full.

    Here's a simple example: I have a deque<bool> and I'd like to fill it
    up with alternating 1's and 0's. Naievely, this is how I would go
    about it:


    deque<bool>::iterator it;
    deque<bool> coll(100);

    for(it = coll.begin(); it != coll.end(); ++it)
    {
    *it = false;
    *++it = true;
    }

    Of course this assumes that the size of coll is even. In general, this
    translates to the size of the larger container being an integer
    multiple of the size of the smaller container. I can live with that
    assumption.

    But even with the safety assumption, this seems clumsy and error prone.
    Is there a better way to accomplish this?

    Thanks in advance,
    Bill
    , Jun 30, 2006
    #1
    1. Advertising

  2. Steve Pope Guest

    <> wrote:

    >Here's a simple example: I have a deque<bool> and I'd like to fill it
    >up with alternating 1's and 0's. Naievely, this is how I would go
    >about it:


    >
    > deque<bool>::iterator it;
    > deque<bool> coll(100);
    >
    > for(it = coll.begin(); it != coll.end(); ++it)
    > {
    > *it = false;
    > *++it = true;
    > }


    I would find any way of avoiding chaning the for loop's
    variable both within the for statement, and within the body.

    Personally I would first try:

    deque<bool> coll(100);
    deque<bool> falseTrue(2); // this is your source container
    // could be any size

    falseTrue[0] = false;
    falseTrue[1] = true;

    for (int ii=0; ii<(int)coll.size(); ii++)
    coll[ii] = falseTrue[ii % falseTrue.size()];

    Steve
    Steve Pope, Jun 30, 2006
    #2
    1. Advertising

  3. <> wrote:

    > I've been flipping through the Josuttis book trying to find it there's
    > a better way to accomplish this. Haven't come across anything so I
    > thought I'd turn to the group.
    >
    > Suppose I have 2 containers, a large one and a small one. I would like
    > to replace the elements of the larger container with the elements of
    > the smaller container, but in such a way the the elements are repeated
    > (or tiled, hence the subject) until the larger container is full.
    >
    > Here's a simple example: I have a deque<bool> and I'd like to fill it
    > up with alternating 1's and 0's. Naievely, this is how I would go
    > about it:
    >
    >
    > deque<bool>::iterator it;
    > deque<bool> coll(100);
    >
    > for(it = coll.begin(); it != coll.end(); ++it)
    > {
    > *it = false;
    > *++it = true;
    > }
    >
    > Of course this assumes that the size of coll is even. In general, this
    > translates to the size of the larger container being an integer
    > multiple of the size of the smaller container. I can live with that
    > assumption.
    >
    > But even with the safety assumption, this seems clumsy and error prone.
    > Is there a better way to accomplish this?


    Yes. Probably lots, but this is what comes to my mind:

    std::deque<widget> BigBox (1132);
    std::deque<widget> LilBox (23);

    // ... put stuff in LilBox here ...

    std::deque<widget>::iterator i, j;

    for (i=BigBox.begin(), j=LilBox.begin(); i != BigBox.end(); ++i,++j)
    {
    if (j == LilBox.end()) j=LilBox.begin();
    (*i)=(*j);
    }


    Each time j reaches the end of LilBox, it just loops back to the
    beginning; so the contents of LilBox tiles BigBox.


    --
    Cheers,
    Robbie Hatley
    Tustin, CA, USA
    lonewolfintj at pacbell dot net (put "[ciao]" in subject to bypass spam
    filter)
    home dot pac bell dot net slant earnur slant
    Robbie Hatley, Jun 30, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Vivi Orunitia
    Replies:
    11
    Views:
    4,473
    Martijn Lievaart
    Feb 4, 2004
  2. Maitre Bart
    Replies:
    2
    Views:
    520
    Maitre Bart
    Feb 11, 2004
  3. Steven T. Hatton
    Replies:
    4
    Views:
    3,896
    Rob Williscroft
    Dec 5, 2004
  4. Replies:
    4
    Views:
    797
    Daniel T.
    Feb 16, 2006
  5. wolverine
    Replies:
    2
    Views:
    450
    Marcus Kwok
    Jul 24, 2006
Loading...

Share This Page