C++11 New features trial

Source: Internet
Author: User

Auto

Before c++11, the Auto keyword is used to specify the storage period. In the new standard, its function becomes type inference. Auto is now a placeholder for a type that informs the compiler to infer the true type of the declared variable based on the initialization code. It can be used for declaring variables in various scopes. For example, in a namespace, in a program block, or in a for-loop initialization statement.

Auto i = 42;//I am an intauto p = new char;//p is a char *
using auto usually means shorter code (unless you have a type of int, it will be one letter less than auto). Imagine those iterators (iterator) that need to be declared when you traverse an STL container. There is no need to declare those typedef to get a concise code.

Map<int, vector<int>> map;for (Auto it = begin (map); it! = end (map); ++it) {}


Nullptrit used to represent a null pointer, but since 0 can be converted to shaping by an implicit type, there are some problems. The keyword nullptr is a value of type std::nullptr_t that is used to refer to null pointers. An implicit type conversion can occur between nullptr and any pointer type, as well as null values of the class member pointer type, or it can be implicitly converted to type bool (with a value of false). However, there is no implicit type conversion to shaping.
int* P1 = null;int* P2 = nullptr;if (P1 = p2) {cout << "P1 = p2" << Endl;}
<span style= "White-space:pre" ></span>bool f = nullptr;
To re-execute the results:
<span style= "font-family: ' Microsoft yahei ', Arial, ' Myriad Pro ', Lato, ' Helvetica Neue ', Helvetica, Arial, Sans-serif;" >P1 = = p2</span>
<pre name= "code" class= "plain" > Please press any key to continue ...


Range-based for loop

To support the "foreach" usage when traversing a container, c++11 extends the syntax for the FOR statement. With this new notation, you can traverse arrays of type C, initialization lists, and any type of begin () and end () functions that are overloaded with non-members.

This foreach for loop is useful if you just want to do something about each element of a collection or array, regardless of the subscript, the position of the iterator, or the number of elements.

Map<string, vector<int>> mapforloop;vector<int> vec;vec.push_back (1); Vec.push_back (2); vec.push_  Back (3); mapforloop["one"] = vec;for (const auto& kvp:mapforloop) {cout << kvp.first << endl;for (Auto VEC : Kvp.second) {cout << vec << Endl;}} int arr[] = {1, 2, 3, 4, 5};for (int& e:arr) {e = e * e;} for (int& E:arr) {cout << e << Endl;}
To re-execute the results:
one1231491625 Please press any key to continue ...




Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

C++11 New features trial

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.