New keywords
auto
The first function introduced in C ++ 11 is for automatic type inference.
Auto's automatic type derivation, used to infer the data type of a variable from an initialization expression. Through the automatic type derivation of auto, we can greatly simplify our programming work. auto actually does type inference for variables at compile time, so it will not adversely affect the running efficiency of the program. In addition, it seems that auto does not affect the compilation speed, because the compile time also has to derive on the right and then determine whether it matches the left. If there is no auto keyword, writing an iterator takes a long time, which can save our brain cells.
auto a; // error, auto is type inferred by the initialization expression, if there is no initialization expression, it cannot be determined a
type
auto i = 1;
auto d = 1.0;
auto str = "Hello World";
auto ch = ‘A’;
auto func = less <int> ();
vector <int> iv;
auto ite = iv.begin ();
auto p = new foo () // perform type derivation for custom types
Not only does auto have the above applications, it also shows its power in templates. For example, in the example of a processed product, if you do not use auto, you must declare the Product parameter:
template <typename Product, typename Creator>
void processProduct (const Creator & creator) {
Product * val = creator.makeObject ();
// do somthing with val
}
If you use auto, you can write:
template <typename Creator>
void processProduct (const Creator & creator) {
auto val = creator.makeObject ();
// do somthing with val
}
Abandoning troublesome template parameters, the entire code becomes more correct.
decltype
decltype is actually a bit like the inverse of auto. auto allows you to declare a variable, while decltype can get the type from a variable or expression. Some examples are as follows:
int x = 3;
decltype (x) y = x; // then it is easy to understand that the type of y is int
Some people will ask, where is the practicality of decltype, let's continue with the example above, what if we want to use the product as the return value in the example of the processed product above? We can write this:
template <typename Creator>
auto processProduct (const Creator & creator)->
decltype (creator.makeObject ()) {
auto val = creator.makeObject ();
// do somthing with val
}
nullptr
nullptr is a new type introduced to solve the original ambiguity of NULL in C ++, because NULL actually represents 0.
Sequence for loop
In C ++, a for loop can use a simplified java for loop. It can be used to iterate through arrays, containers, strings, and sequences defined by the begin and end functions (that is, Iterator).
map <string, int> m {{"a", 1}, {"b", 2}, {"c", 3}};
for (auto p: m) {
cout << p.first << ":" << p.second << endl;
}
Lambda expression
Lambda expressions are similar to closures in Javascript. They can be used to create and define anonymous function objects to simplify programming. Lambda's syntax is as follows:
[Function object parameter] (operator overload function parameter)-> return value type {function body}
vector <int> iv {5, 4, 3, 2, 1};
int a = 2, b = 1;
for_each (iv.begin (), iv.end (), [b] (int & x) {cout << (x + b) << endl;}); // (1)
for_each (iv.begin (), iv.end (), [=] (int & x) {x * = (a + b);}); // (2)
for_each (iv.begin (), iv.end (), [=] (int & x)-> int {return x * (a + b);}); // (3)
The parameters in [] refer to the global variables that can be obtained by the Lambda expression. (1) b in the function means that the function can get global variables outside the Lambda expression. If you pass = in [], you can get all external variables, such as (2) and (3) Lambda expressions. The parameters in formula () are the parameters passed in each time the function is called.
-> Followed by the type of the Lambda expression return value, such as (3) returns a variable of type int.
Variable-length parameter template
We have all used pair in C ++. A pair can use make_pair construction to construct a container containing two different types of data. For example, the following code:
1
auto p = make_pair (1, "C ++ 11");
Since variable-length parameter templates were introduced in C ++ 11, a new data type was invented: tuple, tuple is an N-tuple, which can pass in one, two or more different types of data.
auto t1 = make_tuple (1, 2.0, "C ++ 11");
auto t2 = make_tuple (1, 2.0, "C ++ 11", {1, 0, 2});
This avoids the ugly practice of nesting pairs in previous pairs and makes the code cleaner.
Another frequently seen example is the Print function. In C, printf can pass multiple parameters. In C ++ 11, we can use a variable-length parameter template to implement a more concise Print:
template <typename head, typename ... tail>
void Print (Head head, typename ... tail) {
cout << head << endl;
Print (tail ...);
}
You can pass multiple different kinds of parameters in Print, as follows:
Print (1, 1.0, "C ++ 11");
More elegant initialization method
Before the introduction of C ++ 11, only arrays could use initialization lists, and other containers who wanted to use initialization lists could only use the following methods:
int arr [3] = {1, 2, 3}
vector <int> v (arr, arr + 3);
In C ++ 11, we can use the following syntax to replace:
int arr [3] {1, 2, 3};
vector <int> iv {1, 2, 3};
map <int, string> {{1, "a"}, {2, "b"}};
string str {"Hello World"};
In addition, smart pointers are also very easy to use. A single code can write hundreds of characters in data type. But vs2012 doesn't support C ++ 11 very much, so we should install vs2013 with C ++ 11 suggestion.
C ++ 11 new features, cloth buckle, bubuko.com
C ++ 11 new features
Label: style color java use strong data for ar
Original address: http://my.oschina.net/Jacedy/blog/300878