Handy Extensions
One of the first things you learn when you're using containers is to write a bunch of typedefs so that you can change the type of the container without rewriting the rest of the code:
typedef std::vector<int> values; typedef values::iterator iter; typedef values::const_iterator const_iter;
Now you can write a loop easily:
values data; for (iter it = data.begin(); it != data.end(); ++it) { /* ... */ }
And you can change the container from, say, a vector to a deque by changing its typedef:
typedef std::deque<int> values;
The typedefs for iter and const_iter will change meaning appropriately, and the loop will compile correctly. But that's a lot of boilerplate to have to write every time you use a container type. Soon, you'll be able to write that code like this:
typedef std::vector<int> values; for (auto it = data.begin(); it != data.end(); ++it) { /* ... */ }
and when you change the container type, the compiler will simply change the type to match [6].
If you write code that uses dynamic libraries (DLLs under Windows, and shared libraries under UNIX), you've probably used a language extension that lets you talk about a template instantiation whose code lives somewhere else. For example, std::basic_string<char> should be instantiated in the dynamic library that has the rest of the Standard Library code, and not in the code that uses it. Unfortunately, if you just use the name of the template instantiation, the compiler doesn't know that you don't mean to put the code there:
std::basic_string<char> string; // might generate code in // executable
The trick that Standard Library implementers have been using relies on a compiler extension. The header <string> generally has a declaration for basic_string<char> that looks something like this:
extern template <> basic_string<char>; // not instantiated here
Then somewhere in the library's implementation code, there's the actual instantiation:
template <> basic_string<char>; // may be instantiated here
That is, putting the extern keyword in front of the template declaration tells the compiler that you're using a template instantiation, but you don't want to have it instantiated at that point. That extension is now part of the language, giving you better control over where templates are instantiated.
New Algorithms
The algorithms [7] min, max, min_element, and max_element are sometimes used in pairs, when you're concerned about both the minimum and the maximum values. They've been supplemented by two new algorithms, minmax and minmax_element, that determine both the minimum and the maximum value with a single call to the algorithm. When you're searching through a sequence this is obviously beneficial, because you only have to go through the sequence once instead of making two passesonce to find the minimum value and once to find the maximum value.