473,497 Members | 2,051 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

"Tileing" a Container

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 '06 #1
2 1151
wo******@gmail.com <wo******@gmail.com> 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
Jun 30 '06 #2

<wo******@gmail.com> 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
Jun 30 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
1542
by: Maitre Bart | last post by:
What I want to perform is calling a member function of container 1 (CRunner), using as argument the value of a container 2 (CNames). The purpose is to transfer values from C2 into C1 using a...
3
2613
by: jignesh shah | last post by:
Hi all, Is there a way to recover a single container if its been corrupted or mark bad without restoring whole tablespace? environment: db28.1/aix5.1/tsm/rs-6000. Regards Jignesh
9
1775
by: horizon5 | last post by:
Hi, my collegues and I recently held a coding style review. All of the code we produced is used in house on a commerical project. One of the minor issues I raised was the common idiom of...
7
19992
by: toton | last post by:
Hi, I want a circular buffer or queue like container (queue with array implementation). Moreover I want random access over the elements. And addition at tail and remove from head need to be low...
11
1661
by: food4uk | last post by:
Dear all : I am not good at programming, please give a hand. My data structure is very similar as an array. I actually can use the std::vector as container to organize my data objects. However,...
2
1962
by: Daniel Lipovetsky | last post by:
I would like for an object to "report" to a container object when a new instance is created or deleted. I could have a container object that is called when a new instance is created, as below. ...
1
5900
by: Miked | last post by:
Hello: I'm relatively new to CSS, and I'm doing a site where I don't want to use any tables. I've gotten pretty far, and the site has the layout I want. My only problem is that I'm using the...
18
1795
by: Goran | last post by:
Hi @ all! Again one small question due to my shakiness of what to use... What is better / smarter? private: vector<MyClass_t* itsVector; OR...
36
1995
by: Peter Olcott | last post by:
So far the only way that I found to do this was by making a single global instance of the container class and providing access to the contained class, through this single global instance. Are...
0
7121
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7162
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7375
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5456
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4584
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3088
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3078
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1411
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
650
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.