iterator

LINQ: Remove items from IQueryable

I want to remove an item from the result of a LINQ query before using it to databind. What is the proper way to do this? The foreach in my illustration is the topic of my question. Illustration: var obj = (from a in dc.Activities where a.Referrer != null && a.Referrer.Trim().Length > 12 && a.Session.IP.NumProblems == 0...

C++ STL: Which method of iteration over a STL container is better?

This may seem frivolous to some of you, but which of the following 2 methods of iteration over a STL container is better? Why? class Elem; typedef vector<Elem> ElemVec; ElemVec elemVec; // Method 0 for (ElemVec::iterator i = elemVec.begin(); i != elemVec.end(); ++i) { Elem& e = *i; // Do something } // Method 1 for (int i = 0;...

Iterator access performance for STL map vs. vector?

What is the performance difference between using an iterator to loop through an STL map, versus a vector? I'd like to use the map key for insertion, deletion, and some accesses, but I also need to do regular accesses to every element in the map. ...

yield statement implementation

I want to know everything about the yield statement, in an easy to understand form. I have read about the yield statement and its ease when implementing the iterator pattern. However, most of it is very dry. I would like to get under the covers and see how Microsoft handles return yield. Also, when do you use yield break? ...

Convert iterator to pointer?

I have a std::vector with n elements. Now I need to pass a pointer to a vector that has the last n-1 elements to a function. For example, my vector<int> foo contains (5,2,6,87,251). A function takes vector<int>* and I want to pass it a pointer to (2,6,87,251). Can I just (safely) take the iterator ++foo.begin(), convert it to a pointer...

Remove from a HashSet failing after iterating over it

I'm writing an agglomerative clustering algorithm in java and having trouble with a remove operation. It seems to always fail when the number of clusters reaches half the initial number. In the sample code below, clusters is a Collection<Collection<Integer>>. while(clusters.size() > K){ // determine smallest distance b...

Are const_iterators faster ?

Our coding guidelines say prefer const_iterator, because they are little faster compared to normal iterator. It seems like compiler optimizes the code when you use the const _iterator. Is it really correct ? If yes, what really happens internally to make const_iterator takes the edge?. EDIT: I wrote small test to check const_iterator ...

Concatenating C++ iterator ranges into a const vector member variable at construction time

I have a class X, which I provide a snippet of here: class X { public: template <typename Iter> X(Iter begin, Iter end) : mVec(begin, end) {} private: vector<Y> const mVec; }; I now want to add a new concatenating constructor to this class, something like: template <typename Iter1, typename Iter2> X(Iter1 begin1, Ite...

What is the lifetime and validity of C++ iterators ?

I'm planning to implement a list of Things in C++ where elements might be removed out of order. I don't expect that i'll need any kind of random access (i just need to sweep the list periodically), and the order of items isn't important either. So I thought of std::list<Thing*> with this->position = insert(lst.end(), thing) should do th...

How to remove constness of const_iterator?

As an extension to this question Are const_iterators faster?, I have another question on const_iterators. How to remove constness of a const_iterator? Though iterators are generalised form of pointers but still const_iterator and iterators are two different things. Hence, I believe, I also cannot use const_cast<> to covert from const_i...

Should I prefer iterators over const_iterators?

Someone here recently brought up the article from Scott Meyers that says: Prefer iterators over const_iterators (pdf link). Someone else was commenting that the article is probably outdated. I'm wondering what your opinions are? Here is mine: One of the main points of the article is that you cannot erase or insert on a const_iterat...

Can you embed for loops (in each other) in C++

I am working on a merge sort function. I got the sort down - I am trying to get my merge part finished. Assume that I am learning C++, have cursory knowledge of pointers, and don't understand all the rules of std::vector::iterator's (or std::vector's, for that matter). Assume that num is the size of the original std::vector that have c...

Is there a parameter I can use in Java that works with all for-each loops?

Suppose I've got a method that accepts an array and processes each element in it using Java's built in for-each loop, like this: public static void myFun(SomeClass[] arr) { for (SomeClass sc : arr) { // Stuff is processed here } } This works just fine, but now I want to be able to pass the same method a List<SomeClass>...

std::map iterator not iterating in MFC app

I have a std::map declared thusly in a legacy MFC application: typedef std::map<long, CNutrientInfo> NUTRIENT_INFO_MAP; typedef NUTRIENT_INFO_MAP::const_iterator NUTRIENT_INFO_ITER; typedef NUTRIENT_INFO_MAP::value_type NUTRIENT_INFO_PAIR; static NUTRIENT_INFO_MAP m_NutrientInfoMap; m_NutrientInfoMap is populated when the app loads by...

How to create an iterator over elements that match a derived type in C++?

I'd like an iterator in C++ that can only iterate over elements of a specific type. In the following example, I want to iterate only on elements that are SubType instances. vector<Type*> the_vector; the_vector.push_back(new Type(1)); the_vector.push_back(new SubType(2)); //SubType derives from Type the_vector.push_back(new Type(3)); t...

What's faster, iterating an STL vector with vector::iterator or with at()?

In terms of performance, what would work faster? Is there a difference? Is it platform dependent? //1. Using vector<string>::iterator: vector<string> vs = GetVector(); for(vector<string>::iterator it = vs.begin(); it != vs.end(); ++it) { *it = "Am I faster?"; } //2. Using size_t index: for(size_t i = 0; i < vs.size(); ++i) { //...

How to implement a method to return Iterator of files recursively in Java

I want to implement a method like this: public Iterator<File> getFiles(String root) { // return an Iterator looping through all files in root and all files in sub-directories of roots (recursively) } In C#, this can easily be implemented with the yield return keyword. In Java, I suspect I have to end up writing a lot of complicated...

Problem when disabling checked iterators in vs2008 SP1 (_HAS_ITERATOR_DEBUGGING=0)

I've been having some trouble with vs2008 SP1 running in debug mode when I try to disable checked iterators. The following program reproduces the problem (a crash in the string destructor): #define _HAS_ITERATOR_DEBUGGING 0 #include <sstream> int do_stuff(std::string const& text) { std::string::const_iterator i(text.end()); r...

SQL Iterators

Does anyone know good source where I can find about implementation of SQL iterator/Operator in java and any other languages? Than you, -Nimesh ...

boost::filter_iterator -- how would I do that with the STL?

I am passed an Iterator and I have to pass it on to another function -- but filtered so that certain elements are skipped (it's a range of pointers, and I want to filter out the NULL pointers). I googled for "stl filter iterator" to see how to do this, and boost::filter_iterator came up. That looks nice and I could use it, but could ...