iterator

How to get the current loop index when using Iterator ?

greetings all i am using an Iterator to iterate through collection and i want to get the current loop index any ideas how to do that ? ...

Do STL iterators guarantee validity after collection was changed?

Lets say I have some kind of collection an I obtained an iterator for the beginning of it. Now lets say I modified the collection. Can I still use the iterator safely? (regardless of the type of the collection or the iterator) To avoid confusion , Here is the order of operations I talk about: Get an iterator of the collection. Modify ...

Set.size() doesn't match the number of elements in iterator

Why doesn't Set.size() match the number of elements in the set's iterator? I'm using a HashSet, and I added some duplicate values. Those duplicates were automatically eliminated since I used a set. Set.size() is returning 16. When I actually iterate over the elements, I get 13. What can be causing this difference? Am I doing it rig...

How to overload operator-> on a "generating" iterator?

I'm defining an iterator type that does not store its current value explicitly. Instead, it's a wrapper around another iterator, and returns std::pairs of the underlying iterator's values and another value. The relevant part of the class definition: class MyIterator : public std::iterator<input_iterator, std::pair<Foo *, Bar *> > { ...

foreach returns more elements than count()

Hi, I'm using Symfony 1.2.7 and Doctrine 1.1. I have $activities (sfOutputEscaperIteratorDecorator - Doctrine_Collection). I'm escaping everything on settings.yml with ESC_SPECIALCHARS method. If I weren't escaping it, it would work without any problem, so I think the problem is related with sfOutputEscaperIteratorDecorator. If I do ec...

Getting number of elements in an iterator in Python

Is there an efficient way to know how many elements are in an iterator in Python, in general, without iterating through each and counting? thanks. ...

std::swap returns 0xBAADF00D

Hi I'm trying to swap two std::list< dontcare* >::iterators under Visual 2005. Iter it1 = ... ,it2 = ...; // it1 and it2 are ok, not end() or such if(...){ std::swap(it1,it2); } The swap works, but when I leave the if() scope, it1 points to 0xbaadfood. It2 is ok though.I tried several variations, including swap_iter and a hand-ma...

Solving the array sum problem using iterators and testing for equality only

While getting ready for interviews, I decided to code the classic "Find if there are two elements in an array that sum up to a given number" question using iterator logic, so that it can be generalized to other containers than vector. Here's my function so far // Search given container for two elements with given sum. // If two such e...

python -- callable iterator size?

I am looking through some text file for a certain string with the method. re.finditer(pattern,text) I would like to know when this returns nothing. meaning that it could find nothing in the passed text. I know that callable iterators, have next() and __iter__ I would like to know if I could get the size or find out if it returns no s...

Building a dynamic tree with session information in Django (template question)

Hi, So I've got a expanding/collapsing tree that I'm keeping track of in session data. I want to be able to render the tree in Django based upon the session data. I am saving the state with something like: request.session['treedata'][item_id] = state # (0 or 1) In my template for rendering, I'm looping through the items and for e...

STL iterators and 'const'

I have a problem with what appears to be some sort of implicit casting to const when I use iterators. I'm not really sure which code is relevant (if I did I probably wouldn't be asking this question!) so I will try my best to illustrate my problem. typedef set<SmallObject> Container; //not const void LargeObject::someFunct...

Crazy C++ Vector iterator

I declare: typedef std::tr1::shared_ptr<ClassA> SharedPtr; And then: std::vector<SharedPtr> mList; And: typedef std::vector<SharedPtr>::iterator ListIterator; The return of mList.size() is 0, but when I use iterators, it iterates over the vector which is empty ! This is how I use the iterator: for(ListIterator it = mList.begin(...

cycle and erase elments from std::set

I have a std::set and I need to erase similar adjacet elements: DnaSet::const_iterator next = dna_list.begin(); DnaSet::const_iterator actual = next; ++next; while(next != dna_list.end()) // cycle over pairs, dna_list is the set { if (similar(*actual, *next)) { Dna dna_temp(*actual); // copy constructor dna_lis...

How can I assign this pointer to my Iterator class

Having class : template<class T> class Link { Link* myParent_; Link* myLeft_; Link* myRight_; T* myData_; void assign_(Link<T>*& marker, Link<T>*& aLink); void insert_(const T&);//inserts new data into a link void insert_(const T*); void remove_();//removes data...

iterating getters of a vector of pointers

Hi, I am trying to write an iterator class which returns a getter function return value when it is dereferenced. The code works fine, my only problem is that I would like to write the member_ptr_functor using only one template parameter and not 3, since I am supposed to be able to deduce the types of the argument and return value from th...

Converting a C++ Iterator into an Iterator over a Member (Select iterator?)

In C#, I can create an iterator (or IEnumerable in C# land) which takes another iterator and selects a member of the original type: class ParentType { public MemberType member { get; private set; } } // And somewhere else IEnumerable<MemberType> getMembers(IEnumerable<ParentType> parents) { for each (ParentType parent in paren...

How to counting not 0 elements in an iterable?

I'm looking for a better/more Pythonic solution for the following snippet count = sum(1 for e in iterable if e) ...

What is an iterator's default value?

For any STL container that I'm using, if I declare an iterator (of this particular container type) using the iterator's default constructor, what will the iterator be initialised to? For example, I have: std::list<void*> address_list; std::list<void*>::iterator iter; What will iter be initialised to? ...

enumerate()-ing a generator in Python

I'd like to know what happens when I pass the result of a generator function to python's enumerate(). Example: def veryBigHello(): i = 0 while i < 10000000: i += 1 yield "hello" numbered = enumerate(veryBigHello()) for i, word in numbered: print i, word Is the enumeration iterated lazily, or does it slurp ...

SEGMENTATION FAULT in vector<string> iterator function used to draw text to my self-coded GUI.

Greetings all, ERROR: Program received signal 'SIGSEGV', Segmentation fault. I am having some issues with the following code creating the above fault in Code::Blocks. It is for a chatbox I am using for a network chat program where the vector is filled with the strings of text for each line of the chat log. I don't see why its throwing...