A couple Python-like features in C++11

The new C++ standard includes a couple Python-like features that I ran across recently. There are other Python-like features in the new standard, but here I’ll discuss range-based for-loops and raw strings.
In Python you loop over lists rather than incrementing a loop counter variable. For example,

    for p in [2, 3, 5, 7, 11]:
        print p

Range-based for loops now let you do something similar in C++11:

    int primes[5] = {2, 3, 5, 7, 11};
    for (int &p : primes)
        cout << p << "n";

Also, Python has raw strings. If you preface a quoted string with R, the contents of the string is interpreted literally. For example,

    print "Hello\nworld"

will produce

    Hello
    world

but

    print R"Hello\nworld"

will produce

    Hello\nworld

because the \n is no longer interpreted as a newline character but instead printed literally as two characters.

Raw strings in C++11 use R as well, but they also require a delimiter inside the quotation marks. For example,

    cout << R"(Hello\nworld)";

The C++ raw string syntax is a little harder to read than the Python counterpart since it requires parentheses. The advantage, however, is that such strings can contain double quotes since a double quote alone does not terminate the string. For example,

    cout << R"(Hello "world")";

would print

    Hello "world"

In Python this is unnecessary since single and double quotes are interchangeable; if you wanted double quotes inside your string, you’d use single quotes on the outside.

Note that raw strings in C++ require a capital R unlike Python that allows r or R.

The C++ features mentioned here are supported gcc 4.6.0. The MinGW version of gcc for Windows is available here. To use C++11 features in gcc, you must add the parameter -std=c++0x to the g++ command line. For example,

    g++ -std=c++0x hello.cpp

Visual Studio 2010 supports many of the new C++ features, but not the ones discussed here.

Related links

8 thoughts on “A couple Python-like features in C++11

  1. Nice writeup, John. I just wanted to point out that the style of for iterating over a sequence has been around way before Python, at least since the Bourne shell. Also, the syntax for C++11’s range-based for is actually more reminiscent of Java’s for-each loop (not that you claimed otherwise).

  2. Oh great: yet another raw string syntax to learn/ Shouldn’t complain, I guess. At least it’s there.

  3. Nitpicking here: Visual Studio 2010 supports many of the new C++ features, but it not the ones discussed here.
    Should be: Visual Studio 2010 supports many of the new C++ features, but not the ones discussed here.

  4. Failed to mention in my previous comment. I always like the articles you write :)
    Thanks for sharing your knowledge.

  5. I never liked C– and this post cannot change anything.
    Look at C– template structure and sample codes, it is a piece of rubbish.
    Cheers,

  6. Hi John,

    I just stumbled upon this post and wanted to share a new feature in C++11 as well by modifying your iteration snippet from above. If you want C++ to automatically pick the correct iterator type for you, you can use the auto keyword (note the different meaning in C++ compared to C: http://en.cppreference.com/w/cpp/keyword/auto). This is very helpful when having to iterate over template containers. When coming from Python, one might also like the lambda functions in C++11.

    Thanks for your post!
    Konrad


    #include
    #include
    using namespace std;
    int main(int argc, char *argv[]) {
    int primes[] = {2, 3, 5, 7, 11};

    // Iteration with explicit type specification
    for (int &p : primes)
    cout << p << endl;

    // Iteration with implicit type specification
    // (using "auto" keyword)
    for (auto &p : primes)
    cout << p << endl;

    // Iteration with lambda function
    auto my_lambda = [](int &p){ cout << p << endl; };
    for (auto &p : primes)
    my_lambda(p);

    // Iteration using "for_each" algorithm
    // and lambda function
    for_each(primes, primes+5, my_lambda);

    return 0;
    }

Comments are closed.