Hello:
Yesterday, I encountered an interesting side-effect of iterators. Even
though the code looked fine, it actually was completely wrong. See if
you can figure out why this code doesn't do as it is expected and how
to fix it:
// Returns indicies of matching items
public static IEnumerable<intFindAll(IList<Tlist, Predicate<T>
predicate)
{
for (int i = 0; i != list.Count; ++i)
{
if (predicate(list[i]))
{
yield return i;
}
}
}
// Removes the items in the IList at the given indicies (if it's not
an T[])
public static void RemoveAt(IList<Tlist, IEnumerable<intindicies)
{
int shift = 0;
foreach (int index in indicies)
{
list.RemoveAt(index - shift);
++shift;
}
}
// Caller
List<intvalues = new List<int>(new int[] { 1, 2, 3, 4, 5, 6, 7, 8,
9 });
IEnumerable<intindicies = FindAll<int>(values, delegate (int
current) { return i % 2 == 0; });
RemoveAt(values, indicies);
I just forced the RemoveAt method to take the predicate, but there is
a way to fix the caller withouth touching the other methods.
Have fun,
Travis 0 966 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Grant Edwards |
last post by:
In an interview at http://acmqueue.com/modules.php?name=Content&pa=showpage&pid=273
Alan Kay said something I really liked, and I think it applies
equally well to Python as well as the languages mentioned:
I characterized one way of looking at languages in this
way: a lot of them are either the agglutination of features
or they're a...
|
by: cdiggins |
last post by:
The ostream_iterator from the standard library is a template. This
results in a somewhat inelegant syntax. I thought I would share the
following alternative ostream_iterator, which avoids the neccessity for
template instantion
// public domain code by Christopher Diggins
#include <iostream>
struct putter {
|
by: Lorenzo Castelli |
last post by:
This is an old problem of mine.
Basically I have an abstract base class which represents a generic iterator
over a collection of elements, and various derived classes that implement
the traversing on specific data structures.
For each iterator I want to be able to specify the four possible const
combinations, corresponding to the various ...
|
by: silversurfer |
last post by:
Ok, this should be fairly easy for most of you (at least I hope so),
but not for me:
Let us say we have got the following elements:
std::vector<Entry> models; //Entry is a struct
std::vector<Entry>::iterator modelIterator;
In a method, I am currently writing, I need to get a pointer to an
entry in the vector.. I thought of the following:
|
by: T.A. |
last post by:
I understand why it is not safe to inherit from STL containers, but I have
found (in SGI STL documentation) that for example bidirectional_iterator
class can be used to create your own iterator classes by inheriting from
it, ie.
class my_bidirectional_iterator : public bidirectional_iterator<double>
{
...
};
| |
by: mailforpr |
last post by:
How do I do that?
The thing is, the only information I have about the iterator is the
iterator itself. No container it is belonging to or anything. Like
template<Iteratorvoid totally_isolated(Iterator& it)
{
//how do I find out if it points to the end node?
}
|
by: Frederick Gotham |
last post by:
I thought it might be interesting to share experiences of tracking down a
subtle or mysterious bug. I myself haven't much experience with tracking
down bugs, but there's one in particular which comes to mind.
I was writing usable which dealt with strings. As per usual with my code, I
made it efficient to the extreme. One thing I did was...
|
by: JoeC |
last post by:
I am trying to make an iterator work with pointers. I wrote this code
and it works but with an upgrade to my program vector is now unit*
instead of unit type.
void kill(std::vector<unit>& u){
std::vector<unit>::iterator itr = u.begin();
while(itr != u.end()){
if (itr->marked()){
itr = u.erase(itr);
}else {++itr;}
|
by: vasili |
last post by:
hello All,
I have a simple issue.
I defined a custom container, that encloses a std::list, which in turn
holds objects that are a simple abstraction of a six position array.
Now, i would like to serialize the whole newly-defined container, in
order to copy the contents to another array. So i thought to define an
iterator which...
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
| |
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes...
|
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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 we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...
| |