C++ range/xrange equivalent in STL or boost?

Boost irange should really be the answer (ThxPaul Brannan) I’m adding my answer to provide a compelling example of very valid use-cases that are not served well by manual looping: #include <boost/range/adaptors.hpp> #include <boost/range/algorithm.hpp> #include <boost/range/irange.hpp> using namespace boost::adaptors; static int mod7(int v) { return v % 7; } int main() { std::vector<int> v; boost::copy( … Read more

Types of iterator : Output vs. Input vs. Forward vs. Random Access Iterator

If you can, find and read “The C++ Standard Library: A Tutorial and Reference”. This book contains a whole chapter about STL iterators. Here is a little something from the book: Iterator Category Ability Providers —————– ——————————- —————————- Input iterator Reads forward istream Output iterator Writes forward ostream, inserter Forward iterator Reads/writes forward forward_list, unordered_[multi]set, … Read more

For-each vs Iterator. Which will be the better option

for-each is syntactic sugar for using iterators (approach 2). You might need to use iterators if you need to modify collection in your loop. First approach will throw exception. for (String i : list) { System.out.println(i); list.remove(i); // throws exception } Iterator it=list.iterator(); while (it.hasNext()){ System.out.println(it.next()); it.remove(); // valid here }