iterator

When to write an iterator?

I know this is probably a silly question.. When would I need to write my own iterator? Is it just when designing my own container class? Are there any other times when I would want to create my own iterator? Examples would be appropriated. -Jon ...

Loop over a file and write the next line if a condition is met

Having a hard time fixing this or finding any good hints about it. I'm trying to loop over one file, modify each line slightly, and then loop over a different file. If the line in the second file starts with the line from the first then the following line in the second file should be written to a third file. with open('ids.txt', 'rU') ...

Const references when dereferencing iterator on set, starting from Visual Studio 2010

Starting from Visual Studio 2010, iterating over a set seems to return an iterator that dereferences the data as 'const data' instead of non-const. The following code is an example of something that does compile on Visual Studio 2005, but not on 2010 (this is an artificial example, but clearly illustrates the problem we found on our own...

Can't access a map member from a pointer

Hi. That's my first question :) I'm storing the configuration of my program in a Group->Key->Value form, like the old INIs. I'm storing the information in a pair of structures. First one, I'm using a std::map with string+ptr for the groups info (the group name in the string key). The second std::map value is a pointer to the sencond st...

std::vector iterator or index access speed question

Just a stupid question . I have a std::vector<SomeClass *> v; in my code and i need to access its elements very often in the program, looping them forward and backward . Which is the fastest access type between those two ? Iterator access std::vector<SomeClass *> v; std::vector<SomeClass *>::iterator i; std::vector<SomeClass *>:...

How might I wrap the FindXFile-style APIs to the STL-style Iterator Pattern in C++?

Hello everyone :) I'm working on wrapping up the ugly innards of the FindFirstFile/FindNextFile loop (though my question applies to other similar APIs, such as RegEnumKeyEx or RegEnumValue, etc.) inside iterators that work in a manner similar to the Standard Template Library's istream_iterators. I have two problems here. The first is w...

Java Iterators - Trying to get a for each loop to work

So I have a Tree<E> class where E is the datatype held and organized by the tree. I'd like to iterate through the Tree like this, or in a way similar to this: 1. Tree<String> tree=new Tree<String>(); 2. ...add some nodes... 3. for (String s : tree) 4. System.out.println(s); It gives me an error on line 3 though. Incompatible ...

maps, iterators, and complex structs - STL errors

So, I have two structs: struct coordinate { float x; float y; } struct person { int id; coordinate location; } and a function operating on coordinates: float distance(const coordinate& c1, const coordinate& c2); In my main method, I have the following code: map<int,person> people; // populate people map<int,map<fl...

is back_insert_iterator<> safe to be passed by value?

I have a code that looks something like: struct Data { int value; }; class A { public: typedef std::deque<boost::shared_ptr<Data> > TList; std::back_insert_iterator<TList> GetInserter() { return std::back_inserter(m_List); } private: TList m_List; }; class AA { boost::scoped_ptr<A> m_a; public: AA()...

C++ list<T>::iterator cant be used in derived class template

g++ compiler gives this error: expected `;' before 'it' template <typename T> class myList : public std::list<T> { public: void foo () { std::list<T>::iterator it; // compiler error as above mentioned, why ??? } }; Thanks. ...

Iterating backward

Suppose I have a vector<int> myvec and I want to loop through all of the elements in reverse. I can think of a few ways of doing this: for (vector<int>::iterator it = myvec.end() - 1; it >= myvec.begin(); --it) { // do stuff here } for (vector<int>::reverse_iterator rit = myvec.rbegin(); rit != myvec.rend(); ++rit) { // do stuf...

comparing two end() iterators

list<int> foo; list<int> foo2; list<int>::iterator foo_end = foo.end(); list<int>::iterator foo2_end = foo2.end(); for (list<int>::iterator it = foo.begin(); it != foo2_end; ++foo) <- notice != comparison here { ... it this allowed? will it work correctly. I am inclined to think that this is implementation dependent, anyone knows ...

Java - When to use Iterators?

Hi all, I am trying to better understand when I should and should not use Iterators. To me, whenever I have a potentially large amount of data to iterate through, I write an Iterator for it. If it also lends itself to the Iterator interface, then it seems like a win. I was reading a little bit that there is a lot of overhead with usi...

C++ STL vector iterator... but got runtime error

I'm studying STL and made win32 project.. But I got stuck in runtime error.. I tried to debug it but.. (partial code) vector<Vertex> currPoly=polygons.back(); vector<Vertex>::iterator it; for(it=currPoly.begin();it!=currPoly.end();++it){ vector<Vertex>::iterator p1; vector<Vertex>::iterator n1; vector<Vertex>::iterator ...

Is it a good idea to create an STL iterator which is noncopyable?

Most of the time, STL iterators are CopyConstructable, because several STL algorithms require this to improve performance, such as std::sort. However, I've been working on a pet project to wrap the FindXFile API (previously asked about), but the problem is it's impossible to implement a copyable iterator around this API. A find handle c...

iteration on numbers with no 2 same digits

I dont know if it is asked (I couldn't find any). I want to iterate on this kind of numbers implemented on array; int a[10]; int i = 0; for( ; i < 10; i++ ) a[i] = i+1; now the array has "1 2 3 4 5 6 7 8 9 10" and I want to get "1 2 3 4 5 6 7 8 10 9" and then "1 2 3 4 5 6 7 9 8 10" "1 2 3 4 5 6 ...

Correct way of setting a custom FileInfo class to an Iterator

I am trying to set a custom class to an Iterator through the setInfoClass method: Use this method to set a custom class which will be used when getFileInfo and getPathInfo are called. The class name passed to this method must be derived from SplFileInfo. My class is like this (simplified example): class MyFileInfo extends SplFile...

What are the advantages and disadvantages of using boost::iterator_facade?

Yep -- the title pretty much sums it up. I've got quite a few types that implement iterator concepts, and I'm wondering if it's worthwhile to pull in this boost header instead of implementing things manually. So far: Advantages Well specified Less likely to have bugs ...

Call a block method on an iterator: each.magic.collect { ... }

I have a class with a custom each-method: class CurseArray < Array def each_safe each do |element| unless element =~ /bad/ yield element end end end end And want to call different block methods, like "collect" or "inject" on those iterated elements. For example: curse_a...

How to use C++ Boost's regex_iterator()

Hello, I'm using boost library to match substrings in a text. to iterate over results i need to use regex_iterator (see http://www.boost.org/doc/libs/1_42_0/libs/regex/doc/html/boost_regex/ref/regex_iterator.html) that's the only usage example i have found, but it's not clear for me (i don't understand the callback). could someone fa...