Posts Tagged ‘range based for loops’

As a newbie to C++ it’s always nice to try and keep up with the latest changes in the field whilst they’re happening and the most recent change is the new C++11 standardization. For the longest time it’s been going under the moniker C++0x because no-one quite knew when it was going to be passed (with the last standardization being in 03), but last year the committee passed it into being. There are a few pretty nice additions to the language, a few of which I’m going to run through:

Range based for loops

So for the longest time, creating an iterator to loop over a container involved a heck of a lot of code:

for(vector<int>::iterator it = myVector.begin(); it != myVector.end(); ++it) {
    //do stuff here

A whole heap of writing where quite a few mistakes can be made (and boredom sets in about halfway through). Now C++11 has introduced the auto keyword and introduced range-based for loops so you don’t have to write nearly as much. The equivalent code to the for loop above has now been shrunk down to

for(auto x : myVector) {
    //do stuff here

So much nicer it’s unbelievable it’s only just turned up. This is almost as nice as Python’s for loop

for x in myVector:
    #do stuff here

With Python only winning out because it’s worded in a much more natural way (“for every ‘x’ in myvector, do stuff”) compared to C++.

Uniform initialization

When a type of data is initialized, it could be done through assignment, implicit creation, curly brackets {} or normal brackets (). Now C++11 has changed all that so everything can be initialized with curly brackets, through the use of initializer_list. Even better, if the type doesn’t have a contructor that takes an initializer_list, the compiler will look for a constructor that takes the types you’ve put in the list. So all of the below are valid:

int i{5};
int j[5]{1,2,3,4,5};
string s{"Hello World"};
string str{a};
MyClass c{"Construct me"};
vector<int> myVec{8}; //a one element vector with 8 in it

Constructor delegation

Something that I think is immensely cool is constructor delegation. If you write a class and it has multiple constructors, and you can now do this

class myClass {
    int myX;
    float myY;

        myClass() : myX(0), myY(0) {}
        myClass(int x) : myClass() {myX = x;}
        myClass(float y) : myClass() {myY = y;}

The second and third constructor no longer have to set default values for the two member values as the default constructor is called to do it instead. Much nicer.


Something that I was terrible at as a beginner (and I’m slowly improving at) was making sure my pointers are set to NULL on creation. It always struck me as a bit weird that something that was used in place of 0 could be a null pointer, but that was the way it went. Now nullptr has been declared, all pointers can be pointed at that on creation (or upon being freed up). Much easier to remember in my opinion.

So, just a few of the new items in C++11 that I’ve noticed so far and thought were impressive. I know there are a lot more items out there to pick up on and I can’t wait to start utilising them in my code.


Read Full Post »