473,386 Members | 1,832 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

multiple iterators on non-generic class - correct 'idiom'?

Hi

Another query relating to iterators.

The following seems to work but it's different from the examples I can
find on MSDN ...

I'm trying to retrofit iterators into a non-generic class - here's an
attempt to make a forward and reverse one, with forward being the
default ...

One thing I can't quite figure out is why my foreach works when
specifying the 'named' enumerators?! Normally IEnumerator needs to be
returned but, below, IEnumerable is returned from the yield-versions -
foreach still works. I also noticed that it doesn't seem to matter
whether the iterator function returns IEnumerable or IEnumerator.
What's going on?

class SortedHashtable : IEnumerable
{
...

public IEnumerator GetEnumerator()
{
return GetForwardOrderEnumerator().GetEnumerator();
}

public IEnumerable GetForwardOrderEnumerator()
{
ArrayList sortedKeys = new ArrayList(hashtable.Keys);
sortedKeys.Sort();

foreach (object o in sortedKeys)
{
yield return new DictionaryEntry(o, hashtable[o]);
}
}

public IEnumerable GetReverseOrderEnumerator()
{
ArrayList sortedKeys = new ArrayList(hashtable.Keys);
sortedKeys.Sort();
sortedKeys.Reverse();

foreach (object o in sortedKeys)
{
yield return new DictionaryEntry(o, hashtable[o]);
}
}
}
Cheers

Emma

Aug 10 '06 #1
0 1057

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

Similar topics

2
by: Alex Martelli | last post by:
Interested parties (essentially, people who write user-coded iterator, or are intense users of iterators as "stream of data", of standard library module itertools, etc) are welcome to read, and...
2
by: Ney André de Mello Zunino | last post by:
Hello. A non-modifying algorithm I implemented uses two associative containers from STL: set and map. The elements on those containers are supposed to refer to actual elements which lie on...
3
by: Old Wolf | last post by:
Hi all. G++ fails to compile the following: #include <string> int main() { std::string foo("abc=123"); std::string::const_iterator delimiter = std::find(foo.begin(), foo.end(), '=');
0
by: gary_dr | last post by:
What's so special about istream_iterator that prevents me from determining the distance between two of them. In the code below, search() does return a foundAt iterator that points to the location...
4
by: fastback66 | last post by:
Evidently per the C++ standard, it is not possible to measure the distance between two stream iterators, because two non-end-of-stream iterators are equal when they are constructed from the same...
7
by: tea-jay | last post by:
hello every body our teacher asks us to write this assiment it has 2 question i did write the first one but the other was really complicated 2 me coz our teacher doesn't know how to explain...
18
by: desktop | last post by:
1) I have this code: std::list<intmylist; mylist.push_back(1); mylist.push_back(2); mylist.push_back(3); mylist.push_back(4);
6
by: Michael Coley | last post by:
Hi, I've wrote this function which should add a comma for every 3 digits in a number (so that it looks something like 5,000). This is my function: std::string formatNumber(int number) { //...
5
by: TP | last post by:
Hi everybody, Several means to escape a nested loop are given here: http://stackoverflow.com/questions/189645/how-to-break-out-of-multiple-loops-in-python According to this page, the best...
3
by: zr | last post by:
Hi, Does usage of checked iterators and checked containers make code more secure? If so, can that code considered to be reasonably secure?
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.