iterator

std::list iterator: get next element

I'm trying to build a string using data elements stored in a std::list, where I want commas placed only between the elements (ie, if elements are {A,B,C,D} in list, result string should be "A,B,C,D". This code does not work: typedef std::list< shared_ptr<EventDataItem> > DataItemList; // ... std::string Compose(DataItemList& dilList) {...

How to correctly inherit std::iterator.

Guys if I have class like below: template<class T> class X { T** myData_; public: class iterator : public iterator<random_access_iterator_tag,/*WHAT SHALL I PUT HERE? T OR T** AND WHY?*/> { T** itData_;//HERE I'M HAVING THE SAME TYPE AS MAIN CLASS ON WHICH ITERATOR WILL OPERATE }; }; Questions are in code next to appropriate lines. Th...

Why can't I sort this container?

Please don't mind that there is no insert fnc and that data are hardcoded. The main purpouse of it is to correctly implement iterator for this container. //file Set.h #pragma once template<class T> class Set { template<class T> friend ostream& operator<<(ostream& out, const Set<T>& obj); private: T** myData_; std::size_...

Fast iterating over first n items of an iterable (not a list) in python

Hello! I'm looking for a pythonic way of iterating over first n items of an iterable (upd: not a list in a common case, as for lists things are trivial), and it's quite important to do this as fast as possible. This is how I do it now: count = 0 for item in iterable: do_something(item) count += 1 if count >= n: break Doesn't seem ...

Implementing Iterable in Java

I have the following code public class A extends Iterable<Integer> { ... public Iterator<Integer> iterator() { return new Iterator<Integer>() { A a; public boolean hasNext() { ... } public Integer next() { ... } pu...

how to use iterator in c++?

I'm trying to calculate distance between 2 points. The 2 points I stored in a vector in c++: (0,0) and (1,1). I'm supposed to get results as 0 1.4 1.4 0 but the actual result that I got is 0 1 -1 0 I think there's something wrong with the way I use iterator in vector. Could somebody help? I posted the code below. typedef struct po...

Are there any open source SimpleTest Test Cases that test PHP SPL interfaces

I have quite a few objects in my system that implement the PHP SPL Iterator interface. As I write them I also write tests. I know that writing tests is generally NOT a cut 'n paste job. But, when it comes to testing classes that implement Standard PHP Library interfaces, surely it makes sense to have a few script snippets that can b...

Manually iterating over a selection of XML elements (C#, XDocument)

What is the “best practice” way of manually iterating (i.e., one at a time with a “next” button) over a set of XElements in my XDocument? Say I select the set of elements I want thusly: var elems = from XElement el in m_xDoc.Descendants() where (el.Name.LocalName.ToString() == "q_a") select el; I can use an...

How are iterators and pointers related?

Code with iterators looks pretty much like code with pointers. Iterators are of some obscure type (like std::vector<int>::iterator for example). What I don't get is how iterators and pointer are related to each other - is an iterator a wrapper around a pointer with overloaded operations to advance to adjacent elements or is it something...

Why is comparing against "end()" iterator legal?

According to C++ standard (3.7.3.2/4) using (not only dereferencing, but also copying, casting, whatever else) an invalid pointer is undefined behavior (in case of doubt also see this question). Now the typical code to traverse an STL containter looks like this: std::vector<int> toTraverse; //populate the vector for( std::vector<int>::i...

C++ iterators problem

I'm working with iterators on C++ and I'm having some trouble here. It says "Debug Assertion Failed" on expression (this->_Has_container()) on line interIterator++. Distance list is a vector< vector< DistanceNode > >. What I'm I doing wrong? vector< vector<DistanceNode> >::iterator externIterator = distanceList.begin(); while (exter...

What does enabling STL iterator debugging really do?

I've enabled iterator debugging in an application by defining _HAS_ITERATOR_DEBUGGING = 1 I was expecting this to really just check vector bounds, but I have a feeling it's doing a lot more than that. What checks, etc are actually being performed? Dinkumware STL, by the way. ...

How to remove the last element of a jQuery selection ?

Hi, I use a jquery selector : $('#menus>ul>li>a') I'd like to to iterate the selector result without the last one: $('#menus>ul>li>a').removeLast().each(fct()); Ofcourse the function "removeLast()" doesn't exist, is there an equivalent ? Thanks. ...

python iterators and thread-safety

I have a class which is being operated on by two functions. One function creates a list of widgets and writes it into the class: def updateWidgets(self): widgets = self.generateWidgetList() self.widgets = widgets the other function deals with the widgets in some way: def workOnWidgets(self): for widget in self.widgets: ...

How can I copy one map into another using std::copy?

I would like to copy the content of one std::map into another. Can I use std::copy for that? Obviously, the following code won't work: int main() { typedef std::map<int,double> Map; Map m1; m1[3] = 0.3; m1[5] = 0.5; Map m2; m2[1] = 0.1; std::copy(m1.begin(), m1.end(), m2.begin()); return 0; } This won't work because co...

How to iterate over modifed std::map values?

I have an std::map, and I would like to define an iterator that returns modified values. Typically, a std::map<int,double>::iterator iterates over std::pair<int,double>, and I would like the same behavior, just the double value is multiplied by a constant. I tried it with boost::transform_iterator, but it doesn't compile: #include <ma...

Are there any C# collections where modification does not invalidate iterators?

Are there any data structures in the C# Collections library where modification of the structure does not invalidate iterators? Consider the following: List<int> myList = new List<int>(); myList.Add( 1 ); myList.Add( 2 ); List<int>.Enumerator myIter = myList.GetEnumerator(); myIter.MoveNext(); // myIter.Current == 1 myList.Add( 3 ); m...

Getting value of std::list<>::iterator to pointer?

How can i loop thru a stl::List and store the value of one of the objects for use later in the function? Particle *closestParticle; for(list<Particle>::iterator p1 = mParticles.begin(); p1 != mParticles.end(); ++p1 ) { // Extra stuff removed closestParticle = p1; // fails to compile (edit from comments) } ...

Trying to use a list iterator to print out entire linked list in Java. Infinite loop for some reason?

I created my list: private static List list = new LinkedList(); and my iterator: ListIterator itr = list.listIterator(); and use this code to try to print out the list... Only problem is, it never comes out of the loop. When it reaches the tail, shouldn't it come out of the loop, because there is no next? Or is it going back to the...

[struts 2] How do you iterate through a list of objects?

I have a User class that has a String username in it. I have a list of users that I'm trying to display in a table using <s:iterator value="users" id="list"> <tr> <td><s:property value="#list.username" /></td> ...