What does iterator->second mean?

I’m sure you know that a std::vector<X> stores a whole bunch of X objects, right? But if you have a std::map<X, Y>, what it actually stores is a whole bunch of std::pair<const X, Y>s. That’s exactly what a map is – it pairs together the keys and the associated values. When you iterate over a … Read more

Is the order of iterating through std::map known (and guaranteed by the standard)?

Yes, that’s guaranteed. Moreover, *begin() gives you the smallest and *rbegin() the largest element, as determined by the comparison operator, and two key values a and b for which the expression !compare(a,b) && !compare(b,a) is true are considered equal. The default comparison function is std::less<K>. The ordering is not a lucky bonus feature, but rather, … Read more

Why does std::list::reverse have O(n) complexity?

Hypothetically, reverse could have been O(1). There (again hypothetically) could have been a boolean list member indicating whether the direction of the linked list is currently the same or opposite as the original one where the list was created. Unfortunately, that would reduce the performance of basically any other operation (albeit without changing the asymptotic … Read more

Advantages of std::for_each over for loop

The nice thing with C++11 (previously called C++0x), is that this tiresome debate will be settled. I mean, no one in their right mind, who wants to iterate over a whole collection, will still use this for(auto it = collection.begin(); it != collection.end() ; ++it) { foo(*it); } Or this for_each(collection.begin(), collection.end(), [](Element& e) { … Read more

Why is a C++ Vector called a Vector? [closed]

It’s called a vector because Alex Stepanov, the designer of the Standard Template Library, was looking for a name to distinguish it from built-in arrays. He admits now that he made a mistake, because mathematics already uses the term ‘vector’ for a fixed-length sequence of numbers. C++11 compounds this mistake by introducing a class ‘array’ … Read more

STL or Qt containers?

This is a difficult to answer question. It can really boil down to a philosophical/subjective argument. That being said… I recommend the rule “When in Rome… Do as the Romans Do” Which means if you are in Qt land, code as the Qt’ians do. This is not just for readability/consistency concerns. Consider what happens if … Read more

maximum value of int

In C++: #include <limits> then use int imin = std::numeric_limits<int>::min(); // minimum value int imax = std::numeric_limits<int>::max(); std::numeric_limits is a template type which can be instantiated with other types: float fmin = std::numeric_limits<float>::min(); // minimum positive value float fmax = std::numeric_limits<float>::max(); In C: #include <limits.h> then use int imin = INT_MIN; // minimum value int … Read more

Throwing the fattest people off of an overloaded airplane.

This won’t help for your proxy problem, however: For 1,000,000 passengers to drop 3000 pounds of weight, each passenger must lose (3000/1000000) = 0.003 lbs per person. That could be achieved through jettisoning every ones shirt, or shoes, or probably even fingernail clippings, saving everyone. This assumes efficient collection and jettison before the weight loss … Read more

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