10 C ++ 11 features that C ++ developers should use (1)

Source: Internet
Author: User

In the new C ++ 11 standard, both the language itself and the standard library have added a lot of new content. This article only covers a few things. However, I believe there are some of these new features that should be the general equipment of all C ++ developers. You may have seen many articles similar to the introduction of various C ++ 11 features. The following is my summary. C ++ developers all need to learn and use the new features of C ++ 11.

Auto

Before C ++ 11, the auto keyword is used to specify the storage period. In the new standard, its function changes to type inference. Auto is now a placeholder of the type, notifying the compiler to deduce the real type of the declared Variable Based on the initialization code. Variables declared in various scopes can be used. For example, in the namespace, in the program block, or in the initialization statement of the for loop.

 
 
  1. auto i = 42;        // i is an int 
  2. auto l = 42LL;      // l is an long long 
  3. auto p = new foo(); // p is a foo* 

Using auto usually means shorter code unless you use the int type, it will be one letter less than auto ). Think about the iterator that needs to be declared when you traverse the STL container ). Now you don't need to declare those typedef to get concise code.

 
 
  1. std::map<std::string, std::vector<int>> map; 
  2. for(auto it = begin(map); it != end(map); ++it) 

Note that auto cannot be used to declare the return value of a function. However, if a function has a trailing return type, auto can be returned in the function declaration. In this case, auto does not tell the compiler to deduce the return type, but guides the compiler to look for the return type at the end of the function. In the following example, the return value type of a function is the return value type of the operator + operator acting on T1 and T2 variables.

 
 
  1. template <typename T1, typename T2> 
  2. auto compose(T1 t1, T2 t2) -> decltype(t1 + t2) 
  3.    return t1+t2; 
  4. auto v = compose(2, 3.14); // v's type is double 

Nullptr

In the past, 0 was used to represent null pointers. However, since 0 can be implicitly converted to an integer type, there are some problems. The keyword nullptr is a value of the std: nullptr_t type, used to indicate a null pointer. Implicit type conversion can occur between nullptr and null values of any pointer type and class member pointer type, or between nullptr and null values of bool type ). But there is no implicit type conversion to the integer.

 
 
  1. void foo(int* p) {} 
  2.   
  3. void bar(std::shared_ptr<int> p) {} 
  4.   
  5. int* p1 = NULL; 
  6. int* p2 = nullptr;   
  7. if(p1 == p2) 
  8.   
  9. foo(nullptr); 
  10. bar(nullptr); 
  11.   
  12. bool f = nullptr; 
  13. int i = nullptr; // error: A native nullptr can only be converted to bool or, using reinterpret_cast, to an integral type 

For forward compatibility, 0 is still a valid NULL pointer value.

Range-based for loops Range-based for loop)

To support the "foreach" usage when traversing containers, C ++ 11 extends the syntax of the for statement. With this new method, you can traverse the C-type array, the initialization list, and any types that overload non-member begin () and end () functions.

If you only want to perform some operations on each element of a set or array, regardless of the subscript, iterator location, or number of elements, the foreach for loop will be very useful.

 
 
  1. std::map<std::string, std::vector<int>> map; 
  2. std::vector<int> v; 
  3. v.push_back(1); 
  4. v.push_back(2); 
  5. v.push_back(3); 
  6. map["one"] = v; 
  7.   
  8. for(const auto& kvp : map) 
  9.   std::cout << kvp.first << std::endl; 
  10.   
  11.   for(auto v : kvp.second) 
  12.   { 
  13.      std::cout << v << std::endl; 
  14.   } 
  15.   
  16. int arr[] = {1,2,3,4,5}; 
  17. for(int& e : arr) 
  18.   e = e*e; 
  19.  
  20. C++ 11 CPP 11 features 


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.