How can I loop through a C++ map of maps?

Old question but the remaining answers are outdated as of C++11 – you can use a ranged based for loop and simply do: std::map<std::string, std::map<std::string, std::string>> mymap; for(auto const &ent1 : mymap) { // ent1.first is the first key for(auto const &ent2 : ent1.second) { // ent2.first is the second key // ent2.second is the … Read more

Remove elements from collection while iterating

Let me give a few examples with some alternatives to avoid a ConcurrentModificationException. Suppose we have the following collection of books List<Book> books = new ArrayList<Book>(); books.add(new Book(new ISBN(“0-201-63361-2”))); books.add(new Book(new ISBN(“0-201-63361-3”))); books.add(new Book(new ISBN(“0-201-63361-4”))); Collect and Remove The first technique consists in collecting all the objects that we want to delete (e.g. using an … Read more

Is recursion ever faster than looping?

This depends on the language being used. You wrote ‘language-agnostic’, so I’ll give some examples. In Java, C, and Python, recursion is fairly expensive compared to iteration (in general) because it requires the allocation of a new stack frame. In some C compilers, one can use a compiler flag to eliminate this overhead, which transforms … Read more

In Ruby, how do I skip a loop in a .each loop, similar to ‘continue’ [duplicate]

Use next: (1..10).each do |a| next if a.even? puts a end prints: 1 3 5 7 9 For additional coolness check out also redo and retry. Works also for friends like times, upto, downto, each_with_index, select, map and other iterators (and more generally blocks). For more info see http://ruby-doc.org/docs/ProgrammingRuby/html/tut_expressions.html#UL.

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)