iterator

Does changing a item field in a Collection while iterating invalidates the collection?

Hi, If do: foreach(var a in col) { a.X = 1; } Will my iterator over the collection become invalid? Thanks ...

Java foreach loop: for (Integer i : list) { ... }

when i use jdk5 like below ArrayList<Integer> list = new ArrayList<Integer>(); for (Integer i : list) { //cannot check if already reached last item } on the other hand if i just use iterator ArrayList<Integer> list = new ArrayList<Integer>(); for (Iterator i = list.iterator(); i.hasNext();) { //i can ...

Java, Google Collections Library; problem with AbstractIterator?

I am using the Google Collections library AbstractIterator to implement a generator. I ran across a problem while doing so; I've reduced it to a more basic type and reproduced the problem. This reduction is obviously overkill for what it does, counting from 1 to numelements via an Iterable. Essentially in the following code, the uncom...

Iterating over pair elements in a container of pairs (C++)

If I have a container (vector, list, etc) where each element is a std::pair, is there an easy way to iterate over each element of each pair? i.e. std::vector<std::pair<int,int> > a; a.push_back(std::pair(1,3)); a.push_back(std::pair(2,3)); a.push_back(std::pair(4,2)); a.push_back(std::pair(5,2)); a.push_back(std::pair(1,5)); and the...

Iterating over all pairs of elements in std-containers (C++)

What's the best way to iterate over all pairs of elements in std container like std::list, std::set, std::vector, etc.? Basically to do the equivalent of this, but with iterators: for (int i = 0; i < A.size()-1; i++) for(int j = i+1; j < A.size(); j++) cout << A[i] << A[j] << endl; ...

Is close() necessary when using iterator on a Python file object

Is it bad practice to do the following and not explicitly handle a file object and call its close() method? for line in open('hello.txt'): print line NB - this is for versions of Python that do not yet have the with statement. I ask as the Python documentation seems to recommend this :- f = open("hello.txt") try: for line in...

How to encapsulate a std::set properly?

Hi, I have a class named Particle which has a std::set as a member. The class looks like this: class Particle { private: std::set<vtkIdType> cells; std::set<vtkIdType>::iterator ipc; public: Particle() {}; enum state {EXISTS = -1, SUCCESS = 0, ERROR = 1}; state addCell(const vtkIdType cell); int numCells() {...

Use a regular iterator to iterate backwards, or struggle with reverse_iterator?

I recently learned about the right way to work with reverse iterators in C++ (specifically when you need to erase one). (See this question and this one.) This is how you're supposed to do it: typedef std::vector<int> IV; for (IV::reverse_iterator rit = iv.rbegin(), rend = iv.rend(); rit != rend; ++rit) { // Use 'rit' if a rever...

C++ binary file I/O to/from containers (other than char *) using STL algorithms

I'm attempting a simple test of binary file I/O using the STL copy algorithm to copy data to/from containers and a binary file. See below: 1 #include <iostream> 2 #include <iterator> 3 #include <fstream> 4 #include <vector> 5 #include <algorithm> 6 7 using namespace std; 8 9 typedef std::ostream_iterator<double> oi_t; 10 typed...

Passing an iterator to another function

I was wondering what would be the best way to accomplish something like this... // Iterating through a list if ( foo ) { RemoveBar( it ); } void RemoveBar( std::list< Type >::iterator it ) { it = listName.erase( it ); ...// Other stuff related to cleaning up the removed iterator } I don't think pass by value will work here. Ob...

C++ "Scrolling" through items in an stl::map

I've made a method to scroll/wrap around a map of items, so that if the end is reached, the method returns the first item and vice-versa. Is there more succinct way of doing this? MyMap::const_iterator it = myMap.find(myKey); if (it == myMap.end()) return 0; if (forward) { it++; if (it == myMap.end()) { it = myM...

Java Inner Class Iterator Problem

I'm having difficulty using an inner Iterator. private List<List<? extends HasWord>> sentences = new ArrayList<List<? extends HasWord>>(); private Iterator<String> wordIterator = new Words(); private class Words implements Iterator<String> { int currSentence = 0; int currWord = 0; @Override public boolean hasNext() { return cur...

Java ResultSet hasNext()

I've got a class that implements Iterator with a ResultSet as a data member. Essentially the class looks like this: public class A implements Iterator{ private ResultSet entities; ... public Object next(){ entities.next(); return new Entity(entities.getString...etc....) } public boolean hasNext(){ ...

How do you use a C++ iterator?

I have a vector like so: vector<MyType*> _types; And I want to iterate over the vector and call a function on each of MyTypes in the vector, but I'm getting invalid return errors from the compiler. It appears the pos iterator isn't a pointer to MyType, it's something else. What am I not understanding? Edit: Some code.. for (pos =...

Understanding Iterator Blocks and Dispose Method .

I am watching Jon Skeet's Copenhagen C# talk videos and I ended up with this code. QUESTION: What is happening after the code prints Finished. I mean why is iterator.MoveNext() failing? CODE: class IteratorBlocks { public static IEnumerable<string> GetStringsForever() { string current = ""; ...

STL iterator - why is the code analysis tool complaining?

I'm checking the results from the static code analysis tool Klocwork. It complains about the following code: 293 for( my_vector_typedef::iterator it( start_pos ); it != end_pos ; ++it ){ 294 delete *it; 295 } With the following message: Object 'it._M_current' is used after it was freed. Object 'it._M_current' was used at line 293 ...

ODBC iterate table without storing in memory

Hi, I need to have a way to iterate through a database table without actually storing it in memory anywhere. I want to essentially read through the rows like an input iterator. I've tried using cursors with a select statement (select * from table_name), but this retrieves the entire table and feeds it back to be one row at a time. So ...

traditional for loop vs Iterator in Java

Is there any performance testing results available in comparing traditional for loop vs Iterator while traversing a ArrayList,HashMap and other collections? Or simply why should I use Iterator over for loop or vice versa? ...

C++ class hierarchy for collection providing iterators

I'm currently working on a project in which I'd like to define a generic 'collection' interface that may be implemented in different ways. The collection interface should specify that the collection has methods that return iterators by value. Using classes that wrap pointers I came up with the following (greatly simplified): Collection....

c++ iterator confusion

I have a vector<list<customClass> > I have an iterator vector<list<customClass> >::const_iterator x When I try to access an member of customClass like so: x[0]->somefunc(), I get errors of a non-pointer type/not found. ...