View Full Version : loops
tricolaire
08-07-2005, 05:16 AM
is there any way in c++ (besides using macros) to create your own loop? In this case, I wish to create a foreach loop. is this possible w/o macros?
http://www.cprogramming.com/tutorial/lesson3.html
tricolaire
08-09-2005, 10:00 PM
nonononono
I know how to use the pre-existing loops
but say I want to define a c++ equivalent to the php foreach loops
is there a way to create a new type of loop?
Loops are fundamental, syntactic part of C++... unless you were using a macro language, I don't see how you could possibly do it. In a language like Ruby, { blabla } is actually an object (of type Procedure, or something like that), and can be passed via arguments, so you could write a new "loop" in Ruby, but the language was designed from the ground up to support things like that, unlike C++.
sage45
08-10-2005, 12:54 AM
Another possibility is to write your own class that would act as a foreach loop... I guess from my point of view it all depends upon the use that you had in mind for your foreach loop...
C++ loops in general, do have that capacity to act as a for-each loop depending upon how you decide to use your count variable in conjunction with your test parameter(s)...
For example...
const int A_SIZE = 500;
int arraySize = 0;
int falserecords = 0;
do {
for (int i = 0, array[i].valid == "FALSE", i++)
{
array[i].valid = "TRUE";
// Since this record is false do something to it to make it true
falserecords++;
}
arraySize++;
} while (arraySize <= A_SIZE);
The above code acts like a foreach loop because it will go through the records in an array of structs or classes and do something to each record that presents itself as a false record, which is set by the bool varible included with each record.
A downside for this use of this loop is the amount of time it would take to run; with 500 records it really would take no time at all but in general your looking at an algorithm time of O(n^2), if my memory serves me correctly... One way to streamline this to mabye O(2n) is to have the internal loop start back where it left off... You may be able to take even more time off by have the outer loop keep track of the last record scanned as well so that its counter will approach 500 quicker... JKD thoughts??? Like I said before man, I need your coprocessor when it comes to mathematics... :D
You will have to excuse my not posting a complete working program using the above, but if you are ready to start writing your own classes then you should be able to take the above and pound out a small working program that will give you some idea... ;)
HTH,
-sage-
tricolaire
08-10-2005, 03:24 AM
ready to start with classes??
you act like I'm some kid
I run a frickin custom software firm
grrrrrrr
anywho, I guess my real problem shows up in that I'm not using arrays, nor am I using vectors, I'm using Hashes. This only makes sense if you use perl, I know. But instead of integers, it cycles through string keys. slight problem. oh well, I'll work it out w/ macros
sage45
08-10-2005, 03:53 AM
ready to start with classes??
you act like I'm some kid
I run a frickin custom software firm
grrrrrrr
anywho, I guess my real problem shows up in that I'm not using arrays, nor am I using vectors, I'm using Hashes. This only makes sense if you use perl, I know. But instead of integers, it cycles through string keys. slight problem. oh well, I'll work it out w/ macrosI did not mean to offend... I was not trying to imply that you are some kid, but at the same time, it is kind of hard to judge the knowledge level of many people (kids included ;) ), without first finding out something about their background...
-sage-
deadimp
08-10-2005, 04:16 AM
What is the structure of the "Hashes" class/structure? It should be storing the string keys in some sort of ordered format that you could iterate through.
Well, I Googled up ("C++" Hashes), and saw that they had to do with std::map's. Look into the iterators (std::map::begin, end, etc.). Link: http://www.cppreference.com/cppmap/
If it's a custom hash table implementation, then you have access to the internal array, where each entry is itself a linked list of values that hash to the index.
In which case:
for (int i = 0; i < MAX_KEY; i++) {
Node* link = _internal[i];
while (link != NULL) {
// do something to link
link = link.next;
}
}
Where you have some sort of internal Node struct with a "next" field. If you're dealing with a Standard Library hashtable, then it probably implements some sort of iterator for your convenience. I don't know anything about the standard library in C++, so I can't help you there.
ready to start with classes??
you act like I'm some kid
I run a frickin custom software firm
grrrrrrr
Birthday:
February 11, 1990
Don't grow up too fast Mr. Adult. :) Be a kid while you still can. lol
The php array is basically just a STL map<key, value> container so to reproduce the foreach() function would simply mean to write a foreach() function implementing the map::iterator. I would do it as a function rather than as a macro.
sage45
08-10-2005, 07:17 AM
Hey JKD, are my algorithm calculations correct??? I really gotta know... :D
-sage-
Hey JKD, are my algorithm calculations correct??? I really gotta know... :D
-sage-
I'm trying to figure out what your code actually does:
const int A_SIZE = 500;
int arraySize = 0;
int falserecords = 0;
do {
for (int i = 0, array[i].valid == "FALSE", i++)
{
array[i].valid = "TRUE";
// Since this record is false do something to it to make it true
falserecords++;
}
arraySize++;
} while (arraySize <= A_SIZE);
Your do...while is essentially just an outer for-loop. So, you iterate through each element of the array. Then for each element of the array, you iterate through the same array until you reach the first valid record, and you start the next outer iteration. Not quite sure what you're trying to do, as the inner-loop doesn't use the variable arraySize at all... as it is, yeah, it is O(n^2). But the code below should do exactly the same thing in O(n):
int falserecords = 0;
for (int i = 0; i < A_SIZE; i++) {
if (array[i].valid == "FALSE") {
++ falserecords;
array[i].valid = "TRUE";
}
}
Maybe I'm failing to see what your example is doing? :)
tricolaire
08-10-2005, 03:22 PM
Don't grow up too fast Mr. Adult. :) Be a kid while you still can. lol
when was the last time you posted correct information on an online forum?
sage45
08-10-2005, 10:20 PM
Maybe I'm failing to see what your example is doing? :)It was just a quick kit-bashed attempt at showing how you can induce C++ loops to act as foreach loops... I like your rewrite though... ****Overviews JKD's code to see if there are copyright implications**** HEHEHEHE... :D
As for the algorithm, I'm trying to bone up on my discrete math cause of a couple of classes I got comming this next semester... Wish me luck...
-sage-
P.S. --> when was the last time you posted correct information on an online forum?He was simply pointing out that in your profile your birthday is listed as Feb. 11, 1990; which makes you a bright and brisk 15 year old (going on 16)... Hence the comment of don't grow up too fast, Mr. Adult...
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.