Download presentation
Presentation is loading. Please wait.
Published byJodie Carson Modified over 9 years ago
1
Language enhancements and additions Learning & Development Team http://academy.telerik.com Telerik Software Academy
2
1. Runtime performance enhancements Rvalue references and move constructors Generalized constant expressions 2. Usability enhancements Initializer lists Type inference Range based for loop Lambda functions and expressions Null pointer constant Right angle bracket 2
3
3. Standard Template Library Tuple types Hash tables Extensible random number facility 4. Algorithms Non-modifying / modifying sequence Sorting, minmax, heaps
4
T&& and move constructor
5
Lvalues (references) Lie on the left side of assignments Identified by T& Rvalues (temporaries) Lie on the right side of assignments Identified by T&& Make move semantics possible
6
Moves the contents of str to str_moved Fast and doesn't allocate another 1GB of memory str will be an empty string after the assignment Move constructors on STL containers: Given a really big string ( 1GB ) 6 string str(1<<30); string str_moved = move(str); string str_copied = str; Copies the big string Slow and allocates another 1GB of memory
7
Move semantics on STL containers: 7 string append_char(string str, char ch) { str += ch; return move(str); return move(str);} // Really big string (1GB) string str(1<<30); string str2 = append_char(move(str), '5');
8
constexpr keyword
9
Allows constant expressions to be functions Restrictions (more relaxed in C++ 14 ) Function must be non-void Variables can not be declared Types can not be defined Only one statement allowed (return value) // Array size must be a compile time constant int A[5+10*23]; // constexpr keyword guarantees that // a function is a compile time constant constexpr int square(int x) { return x*x; return x*x;} int B[square(10)];
10
Initializing arrays, containers and other structures
11
Initializer list ( {value, value, … } ) Type initializer_list can be cast to: STL containers Pairs and tuples Custom structures Can be nested " = " can be omitted ( uniform initialization )
12
Examples: 12 int array[] = {1, 3, 2, 5, 7}; // from C vector v = {1, 3, 2, 5, 7}; v = {1, 2, 3}; set s = {1, 3, 2, 5, 7}; pair SumAndDif(int a, int b) { return {a+b, a-b}; return {a+b, a-b};} map Map = { {"banana", 3}, {"banana", 3}, {"apple", 7}, {"apple", 7}, {"cherry", -5} {"cherry", -5}};
13
Examples: 13 struct rgb { double red; double red; double green; double green; double blue; double blue;} // Can be used on custom structures rgb color = {1.0, 0.5, 0.0}; // Can be nested vector colors = { {0, 0, 0}, {0, 0, 0}, {1, 1, 1}, {1, 1, 1}, {0.5, 0, 1}, {0.5, 0, 1}, {0.2, 0.8, 0.4} {0.2, 0.8, 0.4}};
14
auto, decltype
15
Auto ( auto variable = value ) Deduces type for variable from value Does not deduce const types Does not deduce reference (&) types Decltype ( declype(expression) ) This is the type of the expression Can deduce const types Can deduce reference types 15
16
Examples: int a0; // int (apparently) auto a1 = a0; // int auto a2 = 7; // int auto a3 = 7-a0*(13+a1); // int auto a4 = 2ll; // long long const char c0 = 'x'; // const char (apparently) auto c1 = c0; // char (non-const) const auto c2 = c0; // const char char &r0 = c1; // char& (apparently) auto r1 = r0; // char (non-reference) auto &r2 = r0; // char& int *p0 = &a0; // int* (apparently) auto p1 = p0; // int*
17
Useful when working with iterators map Map; for (map ::iterator it = Map.begin(); it != Map.end(); ++it) { it != Map.end(); ++it) { cout first " second first " second <<'\n';} // becomes for (auto it = Map.begin(); it != Map.end(); ++it) { cout first " second first " second <<'\n';}
18
Examples: int a; // decltype(a) is int // decltype((a)) is int&, because (a) is an lvalue // decltype(0) is int, because 0 is an rvalue const vector v(1); // decltype(v[0]) is const int&
19
Iterating over arrays and containers
20
Iterating over range of elements C style arrays STL containers Initializer lists Any type with defined begin() and end() functions 20
21
Accessing all elements Modifying all elements int A[10] = {1, 5, 9, 2, 4, 0, 2, 2, 7, 9}; for(int x:A) { cout<<x<<'\n'; cout<<x<<'\n';} int A[10] = {1, 5, 9, 2, 4, 0, 2, 2, 7, 9}; for(int x:A) { // Copies each element in x x *= 2; // A is not modified x *= 2; // A is not modified} for(int &x:A) { // Using reference x *= 2; // A is modified x *= 2; // A is modified}
22
Can be used on STL containers Combining with auto vector = {"Pencil", "Knife", "Ball"}; for(string &x:A) { reverse(x); reverse(x);} map Map; for (pair &x:Map) { cout " "<<x.second<<'\n';} // becomes for (auto &x:Map) { cout " "<<x.second<<'\n';}
23
Definition and usage
24
[](int x,int y) { return x+y; } Return type is decltype(x+y) Variable capture [] [variable] [&variable] [&] [=] 24
25
Examples: vector v; sort(v.begin(), v.end(), [](int x, int y) { return abs(x) < abs(y); return abs(x) < abs(y);}); int n; cin >> n; sort(v.begin(), v.end(), [&n](int x, int y) { return x%n < y%n; return x%n < y%n;});
26
nullptr constant
27
nullptr – Constant of type nullptr_t Can be cast to any pointer type Can not be cast to integer types Can be cast to bool ( it's false ) 27 void f(int); void f(char*); // NULL is defined as 0 which is int f(NULL); // calls f(int) – Wrong f(nullptr); // calls f(char*) – Correct
28
C++ 03 and before vector > items C++ 11 Nested template declarations Before C++ 11 " >> " is always defined as right shift
29
C++ 03 and before vector > items; C++ 11 Nested template declarations Before C++ 11 " >> " is always defined as right shift Space required before C++ 11 Space not required in C++ 11
30
Creating and using tuples
31
Tuple ( #include ) Creating tuples tuple t; tuple t(value1, value2, …); tuple t{value1, value2, …}; make_tuple(value1, value2, …); Accessing tuple elements ( get ) Unpacking tuples ( tie, ignore ) Concatenating tuples ( tuple_cat ) 31
32
Examples: tuple tup(42, "hamster", true); cout (tup); // cout's 42 cin >> get (tmp); // cin's a string get (tmp) = false; // Unpacking the tuple in // num and str, ignoring the Boolean // tie creates a tuple of references int num; string str; tie(num, str, ignore) = tup;
33
Examples: tuple tup(42, "hamster", true); int n = 17; auto tup2 = tuple_cat(tup, make_tuple(3.5), tup, tie(n)); // tup2 is of type tuple<int, string, bool, double, int, string, double, int&> int, string, double, int&> n = 19; // last element in tup2 is reference to n // (42, "hamster", true, 3.5, 42, "hamster", 19)
34
Unordered_(multi)set, unordered_(multi)map
35
Unordered_(multi)set, unordered_(multi)map Same as (multi)set and (multi)map but unordered Key must have defined Hash function instead of Compare Extracting elements one by one: Yields the elements in some order
36
Unordered_(multi)sets ( #include ) unordered_set, Alloc = new> Unordered_(multi)maps ( #include ) unordered_map, Alloc = new> Constant average times for operations 36
37
Random number generators and distributions
38
Non-deterministic numbers random_device Random number engines Predefined generators Engine adaptors Distributions Uniform Normal Others 38
39
linear_congruential_engine minstd_rand minstd_rand 0 mersenne_twister_engine mt 19937 mt 19937_64 subtract_with_carry_engine ranlux 24 _base ranlux 48 _base
40
discard_block_engine ranlux 24 ranlux 48 independent_bits_engine shuffle_order_engine knuth_b
41
Examples: #include #include using namespace std; int main() { random_device rd; random_device rd; // get a random number // get a random number cout << rd() << '\n'; cout << rd() << '\n'; mt19937 gen(seed); // seed can be rd() mt19937 gen(seed); // seed can be rd() // get a pseudo-random number // get a pseudo-random number cout << gen() << '\n'; cout << gen() << '\n';}
42
Uniform distributions uniform_int_distribution uniform_real_distribution generate_canonical Normal distributions normal_distribution Others Others
43
Examples: mt19937 gen{random_device{}()}; // generator uniform_int_distribution dice(1, 6); // Gives number from 1 to 6 with equal probability dice(gen); uniform_real_distribution reals(0.0, 10.0); // Evenly distributed real numbers from 0 to 10 reals(gen); normal_distribution gauss(42, 5); // Numbers close to 42 will be most probable // 5 is standard deviation gauss(gen);
44
New algorithms in New algorithms in
45
Non-modifying sequence operations all_of(first, last, predicate) any_of(first, last, predicate) none_of(first, last, predicate) Modifying sequence operations move(first, last, d_first) move_backwards(first, last, d_last) shuffle(begin, end, generator) Uses the new random number functionality Replaces random_shuffle 45
46
Sorting operations is_sorted(begin, end) is_sorted_until(begin, end) Min/max operations minmax(a, b) minmax_element(begin, end) is_permutation(first1, last1, first2, last2) Heap operations is_heap(begin, end) is_heap_until(begin, end) 46
47
форум програмиране, форум уеб дизайн курсове и уроци по програмиране, уеб дизайн – безплатно програмиране за деца – безплатни курсове и уроци безплатен SEO курс - оптимизация за търсачки уроци по уеб дизайн, HTML, CSS, JavaScript, Photoshop уроци по програмиране и уеб дизайн за ученици ASP.NET MVC курс – HTML, SQL, C#,.NET, ASP.NET MVC безплатен курс "Разработка на софтуер в cloud среда" BG Coder - онлайн състезателна система - online judge курсове и уроци по програмиране, книги – безплатно от Наков безплатен курс "Качествен програмен код" алго академия – състезателно програмиране, състезания ASP.NET курс - уеб програмиране, бази данни, C#,.NET, ASP.NET курсове и уроци по програмиране – Телерик академия курс мобилни приложения с iPhone, Android, WP7, PhoneGap free C# book, безплатна книга C#, книга Java, книга C# Николай Костов - блог за програмиране
48
C# Programming @ Telerik Academy csharpfundamentals.telerik.com csharpfundamentals.telerik.com Telerik Software Academy academy.telerik.com academy.telerik.com Telerik Academy @ Facebook facebook.com/TelerikAcademy facebook.com/TelerikAcademy Telerik Software Academy Forums forums.academy.telerik.com forums.academy.telerik.com
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.