some c++11 features

I'm trying to accommodate some new features of c++11. As a new programmer, writing it out in c++98 or C, seems absurd.

So far, i believe these are some of the features of c++11. The bracket initialization, auto keyword, and the for loop shortening. A small example being:
1
2
3
4
5
6
7
8
9
10
#include <iostream>
#include <vector>
#include <string>

int main(){
    std::vector<int> v{1,2,3};
    for(auto i:v){
        std::cout << i << std::endl;
    }
}

this for loop, i love, because it cuts down on code and typos. I dont know what its name is called.

But i was wondering...these features seem very useful , are there any other features that i missed that are just as useful in c++11. IVe read through some changes, but I dont see anything else so far that peeks my interest. Of course that could be because im new.
There are many new features in C++ 11. For example lambda expressions, decltype specifier, initializations of data members of classes, defaulted and deleted function definitions, move semantic and so on.
Last edited on
i dont understand the difference in auto and decltype specifier?
Consider an example

int a[2][3];

std:;cout << std:;extent<declatype( a )>::value << std::endl;
std:;cout << std:;extent<declatype( a ), 1>::value << std::endl;

How will you apply here auto?
That new for loop is called a range-based for loop, or just range-for.

Auto deduces the type from the initializer and thus uses the initializer like normal to init. the variable, whereas decltype deduces the type from the expression but does not evaluate that expression, therefore unlike auto, we don't need to use the deduced type to actually initialize the variable. E.g. if we use a function call as the expression, decltype gives our variable the type that would normally be returned by that function, but it doesn't actually call that function (therefor no initializing is done).

Everyone recommends different stuff, but I suggest C++ Primer 5th edition (not C++ Primer Plus!) as it is revised for C++11 and very in-depth for all the new features. Google a PDF of it if you want...
www.codeproject.com/Articles/570638/Ten-Cplusplus11-Features-Every-Cplusplus-Developer#foreach

these are some of the examples. There are more of it, you can try to google it :)
This would be my suggestion:

Start with smart pointers and lambda expressions.

Move on to std::function<>() and std::bind<>().

Based on posts in these forums, a whole lot of people learning C++ are interested in random number generation and time utilities; you may want to look at the facilities offered in the headers <random> and <chrono>

Above all, do not look at a language or library feature like a mountaineer looks at Mount Everest. A mountaineer decides to climb Mount Everest because it is there. You need a better reason than that to use a new programming feature that you have just learnt.

In other words, don't end up writing abominable crap like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <functional>

int main()
{
	std::function<void( unsigned int )> reverse = [&]( unsigned int x ) 
	{ 
		std::cout << x % 10;
		( x /= 10 ) ? reverse( x ) : void( std::cout << std::endl );
	};


	while ( unsigned int x = ( std::cout << "\nEnter a non-negative number (0 - exit): ",
	                           std::cin >> x, x ) )
	{
		reverse( x );
	}

	return 0;
}

A splendid code! I would now only append two minor changes

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <functional>

int main()
{
	std::function<void( unsigned int )> reverse = [&]( unsigned int x ) 
	{
		const unsigned int base = 10;
 
		std::cout << x % base;
		( x /= base ) ? reverse( x ) : void( std::cout << std::endl );
	};


	while ( unsigned int x = ( std::cout << "\nEnter a non-negative number (0 - exit): ",
	                           x = 0, std::cin >> x, x ) )
	{
		reverse( x );
	}

	return 0;
}
Last edited on
Topic archived. No new replies allowed.