iterator

Does passing an empty range (identical iterators) to an STL algorithm result in defined behavior?

Consider the following: std::vector<int> vec(1); // vector has one element std::fill(vec.begin(), vec.begin(), 42); std::fill(vec.begin()+1, vec.end(), 43); std::fill(vec.end(), vec.end(), 44); Will all of the std::fill usages above result in defined behavior? Am I guaranteed that vec will remain unmodified? I'm inclined to think "yes...

stl hash_map - modifying key

I have a hash map defined as class KeyType { int key; mutable bool flag; KeyType(int key) : key(key), flag(false) {} void setFlag() const { flag = true; } }; struct KeyType_hasher { size_t operator()(const KeyType& s) const { return static_cast<size_t> key; } }; struct KeyType_equal { size_t operat...

Can C++'s value_type be extended from iterator_traits to all types?

I would like to create a construct similar to std::iterator_traits::value_type that can work seamlessly for all types using the same syntax. Imagine we have the following: template <typename T> struct value_type { typedef T type; }; #define VALUE_TYPE(T) typename value_type<T >::type This will work for POD types. I can specialize...

Is there a C++ iterator that can iterate over a file line by line?

I would like to get an istream_iterator-style iterator that returns each line of the file as a string rather than each word. Is this possible? ...

iterator over nested collections

Hi all, I have two data structures in Java: One is called DebateAssignment and has 5 DebateTeam objects, each associated with a specific enum that includes {JUDGE, PROP1, PROP2, OP1, OP2} In another class I use List<DebateAssignment> and I want to create an iterator that will point to a specific DebateTeam in a specific DebateAssign...

How to allow iteration over a private collection but not modification?

If I have the following class member: private List<object> obs; and I want to allow traversal of this list as part of the class' interface, how would I do it? Making it public won't work because I don't want to allow the list to be modified directly. ...

How to implement the Iterator interface in PHP?

Please explain with a tiny demo ...

Why can't I call specific class methods on an Iterator?

ArrayList array = new ArrayList(); Iterator it1 = array.iterator(); while (it1.hasNext()){ Myclass temp = it1.myGetterMethod(); System.out.println (temp); } This is what I would like to implement, but Iterator only returns a generic Object. When I call Object.getClass(), the class is Myclass. Does this mean that the Iterator is ...

How to return a generic iterator (independent of particular container)?

I'd like to design a class Foo that stores various data of different types and returns iterators over them. It's supposed to be generic, so the user of Foo does not know how the data is stored (Foo could be using std::set or std::vector or whatever). I'm tempted to write an interface like this: class Foo { class FooImpl; FooImpl* i...

What are Iterators, C++?

What are Iterators in C++? ...

Struts 2 nesting iterators

I can't believe how something this simple can seem so hard to do in Struts 2. This is approximately what I would like to do as it would be done in Java. for (Parent parent : parents){ for (Child child: parent.getChildren()){ System.out.println(child.getName()); } } That should translate to something close to this in Stuts t...

How to parametrize on iterator direction?

Basically I'm doing the following: std::set<int> indices; // ..fill indices if (flag) { // we need to process in ascending order BOOST_FOREACH (int i, indices) { process(i); } } else { // we need to process in descending order BOOST_REVERSE_FOREACH (int i, indices) { process(i); } } I wonder if ...

index , pointer , iterator , which is better ? when it comes to vectors

hi i'm doing a program that needs high performance of handling vector elements vector<Class_A> object ; 1- which one is fastest to access the elements 2- which is more code simpler and not complex to deal with index ? iterator ? pointer ? ...

calculate mean and variance with one iteration

Suppose I have an iterator of numbers, for example a file object: f = open("datafile.dat") now I want to do: mean = get_mean(f) sigma = get_sigma(f, mean) What is the best implementation? Suppose that the file is big ...

Alternative version of find_if which finds all, not just the first?

Is there an alternative version of std::find_if that returns an iterator over all found elements, instead of just the first one? Example: bool IsOdd (int i) { return ((i % 2) == 1); } std::vector<int> v; v.push_back(1); v.push_back(2); v.push_back(3); v.push_back(4); std::vector<int>::iterator it = find_if(v.begin(), v.end(), IsOd...

How to get data out of the STL's const_iterator?

I have something that runs like this: T baseline; list<T>::const_iterator it = mylist.begin(); while (it != mylist.end()) { if (it == baseline) /* <----- This is what I want to make happen */ // do stuff } My problem is that I have no idea how to extract the data from the iterator. I feel like this is a stupid thing to be con...

How should Iterator implementation deal with checked exceptions?

I'm wrapping a java.sql.RecordSet inside a java.util.Iterator. My question is, what should I do in case any recordset method throws an SQLException? The java.util.Iterator javadoc explains which exceptions to throw in various situations (i.e. NoSuchElementException in case you call next() beyond the last element) However, it doesn't m...

How to write a pager for Python iterators?

I'm looking for a way to "page through" a Python iterator. That is, I would like to wrap a given iterator iter and page_size with another iterator that would would return the items from iter as a series of "pages". Each page would itself be an iterator with up to page_size iterations. I looked through itertools and the closest thing ...

Is this a good way to iterate through a .NET LInkedList and remove elements?

I'm thinking of doing: for(LinkedListNode<MyClass> it = myCollection.First; it != null; it = it.Next) { if(it.Value.removalCondition == true) it.Value = null; } What I'm wondering is if simply pointing the it.Value to null actually gets rid of it. ...

Iterating hashtable in java

I have a hashtable in java and want to iterate over all the value in the table and delete a particular key,value pair while iterating. Is there some way i could do that ...