Some New features in C++11

Lambda Expression
Lambda Expressions are nameless functions given as constant values, and written exactly in the place where it’s needed, typically as a parameter to some other function. The canonical example is that you’ll pass a comparison function to a generic “sort” routine, and instead of going to the trouble of defining a whole function (and incurring the lexical discontinuity and namespace pollution) to describe this comparison, you can just pass a lambda expression describing the comparison.

However, this misses one of the most important features of Lambda Expressions, which is that they execute in the context of their appearance. Therefore, they can use the values of the variables that are defined in that context.

Lambda expressions appear (with different syntax) in all LISPs, Perl, Python, and many other languages, but notably not C, Java, or any similar language, even though those all have a way to deal with passing functions (or some excuse for them) around as parameters.

C++11 has introduced a Lambda Expressions. Here is the typical usage of the same tried with Visual Studio 2010.

#include <stdlib.h>
#include <iostream>
#include <algorithm>

int main ( int argc, char **argv) {
    char s[]="Hello World!";
    int Uppercase = 0; 
    std::for_each(s, s+sizeof(s), [&Uppercase] (char c) {
        if (isupper(c))
            Uppercase++;
    });
    std::cout<< Uppercase<<" uppercase letters in: "<< s << std::endl;
    return 0;
}

Automatic Type deduction
C++11 allows you to declare the object without specifying there type. Consider following program which pushes some integers into the stl vector and prints the contents of the vector container.

#include <vector>
#include <iostream>
int main ( int argc, char **argv) {
    std::vector<int> myvector;
    for ( int i = 0; i < 10; i++) {
        myvector.push_back(i);
    }
    for ( std::vector<int>::iterator itr = myvector.begin();
        itr != myvector.end(); ++itr) {
            std::cout << *itr << " " ;
    }
    std::cout << std::endl;
}

Declaration of the std::vector iterator is very long and clumsy. C++11 allow you to use auto keywords to do the same.

#include <vector>
#include <iostream>
int main ( int argc, char **argv) {
    std::vector<int> myvector;
    for ( auto i = 0; i < 10; i++) {
        myvector.push_back(i);
    }
    for ( auto itr = myvector.begin(); itr != myvector.end(); ++itr) {
            std::cout << *itr << " " ;
    }
    std::cout << std::endl;
}

Note: The keyword auto isn’t new; it actually dates back the pre-ANSI C era. However, C++11 has changed its meaning; auto no longer designates an object with automatic storage type. Rather, it declares an object whose type is deducible from its initializer. The old meaning of auto was removed from C++11 to avoid confusion.

Range-based for-loop
In C++03, iterating over the elements of a list requires a lot of code. Other languages like C# and Java have shortcuts that allow one to write a simple “foreach” statement that automatically walks the list from start to finish.

C++11 added a similar feature. The statement for allows for easy iteration over a list of elements:

int main ( int argc, char **argv) {
    int my_array[5] = {1, 2, 3, 4, 5};
    for (int &x : my_array) {
        x *= 2;
    }

Note: This is not yet implemented in visual studio 2010 See here for more details.

Regular Expression support added

#include <regex>
#include <iostream>

int main ( int argc, char **argv) {
    const char *reg_esp = "[ ,.\\t\\n;:]"; 
    std::regex rgx(reg_esp);
    std::cmatch match;
    const char *target = "Unseen University - Ankh-Morpork";
    if( std::regex_search( target, match, rgx ) ) {
        const size_t n = match.size();
        for( size_t a = 0; a < n; a++ ) {
            std::string str( match[a].first, match[a].second );
            std::cout << str << "\n";
        }
    }
}
Advertisements

One thought on “Some New features in C++11

  1. for counting the number of upper case letters you don't need for_each. std::count_if will do the job using lambda.const char* str = "AbcdeF";std::cout << std::count_if(begin(str), end(str), [](char c) { return c == ::toupper(c); });

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s