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

How to implement the factory method pattern in C++ correctly

First of all, there are cases when object construction is a task complex enough to justify its extraction to another class. I believe this point is incorrect. The complexity doesn’t really matter. The relevance is what does. If an object can be constructed in one step (not like in the builder pattern), the constructor is … Read more

Python idiom to return first item or None

Python 2.6+ next(iter(your_list), None) If your_list can be None: next(iter(your_list or []), None) Python 2.4 def get_first(iterable, default=None): if iterable: for item in iterable: return item return default Example: x = get_first(get_first_list()) if x: … y = get_first(get_second_list()) if y: … Another option is to inline the above function: for x in get_first_list() or []: … Read more

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