C ++ does not know the seven characteristics, absolute human circle Wang Wei

As a computer language, C ++ has undergone many developments and changes.

Of course, these changes are not achieved overnight. C ++ has been the lack of vitality and innovation, so it is unpopular.

But after C ++ standards committee decided to accelerate the development of the language, the situation changed.

Since 2011, C ++ computer language became a dynamic, evolving, much loved.
Finally, if you if you encounter difficulties in learning, looking for a C ++ learning environment, can join our Learning Circle C ++, click I joined it , it will save a lot of time, reduce the number of problems encountered in the study.

C ++ does not know the seven characteristics, absolute human circle Wang Wei

After the C ++ transformation also did not simply how much is still one of the most difficult programming language. However, C ++ is really more humane than before.

This article is to talk about some of the new features of C ++ (with an 8-year-old C ++ 11, for example), I believe that every programmer will be interested in this topic.

Note: This article skip some of the advanced features.

  1. Keywords auto

When first introduced in C ++ 11 auto, programmers who have cried!

Meaning that the auto C ++ compiler can deduce the type of data at compile time, so you do not always have to declare the data type. When the data type is a map <string, vector <pair <int, int >>> particularly when convenient.

No initializer, you can not declare a data type (see the fifth line). This is plausible. The fifth line instructions and did not let the compiler deduce the type of data.

Initially, auto function is limited. After the new version in C ++, auto function more and more powerful.

C ++ does not know the seven characteristics, absolute human circle Wang Wei

Use the line seventh and eighth row in the brackets initialization (bracketedinitialization), which is one of the new features 11 C ++.

Please note that use auto, the compiler must be able to derive the data type.

An interesting question is: if you wrote autoa = {1, 2, 3} What will happen? This is a compilation error it? It is a vector?

C ++ does not know the seven characteristics, absolute human circle Wang Wei

In fact, C ++ 11 introduces a std :: initializer_list <type>. If Statement auto, braces initialization list is as lightweight containers.

Finally, as previously stated, when the complex data structures, the compiler type inference helpful:

C ++ does not know the seven characteristics, absolute human circle Wang Wei

别忘了检查第25行!auto [v1,v2] = itr.second纯粹是C++17的新特性。这个特性叫做结构化绑定。在旧版本C++中,程序员需要单独获取每个变量。但是结构化绑定给这一过程带来了便利。此外,如果想获得数据使用引用(reference),只需要加上一个symbol--auto&[v1,v2] = itr.second.

  1. Lambda表达式

C++11引入了lambda表达式,这类似于JavaScript里的匿名函数。它们都是函数对象,没有名字,且基于简洁的语法在不同作用域上捕获变量。它们也可以被分配给变量。

如果需要在代码中进行一些小而快的操作,又不愿意为此单独写一个函数,那么Lambdas很有用。另一种常见用法是将lambdas作为比较函数。

不知道C++这七大特性,绝对枉为圈中人

以上例子可以说明很多问题。

首先,请注意花括号初始化是如何提升权重的。然后是通用的begin(),end() (这也是C++11的新增部分)。接着是作为数据比较器的lambda函数。lambda函数的参数被声明为auto(这是C++14的新增部分)。在C++14之前是不能对于函数参数使用auto 的。

正如现代C++的awesome库中定义的那样:

· []—不捕获任何对象。所以不能在lambda表达式内使用全局作用域的局部变量,只能使用参数。

· [=]— 按值捕获作用域中的局部对象(局部变量,参数)。只可使用不可修改。

· [&]—按引用捕获作用域中的局部对象(局部变量,参数)。可以被修改。例子如下。

· [this]—按值捕获this 指针。

· [a, &b]—按值捕获对象a ,按引用捕获对象b。

所以,如果想在lambda函数内部将数据转换为其他格式,可以利用作用域的优势来运用lambda.比如:

不知道C++这七大特性,绝对枉为圈中人

在上面这个例子中,如果在lambda表达式中按值捕获([factor])局部变量,则不能改变第五行的factor.原因很简单——没有权限。

最终,请注意示例中使用了val 作为引用 (reference). 这确保了lambda函数内部的任何变化都会改变vector.

不知道C++这七大特性,绝对枉为圈中人

学完现代C++后,她们乐开了花!(摄影:Ian Schneider 图源:Unsplash)

  1. if/switch内的初始化语句

C++17的这个特性十分讨喜:

不知道C++这七大特性,绝对枉为圈中人

很明显,现在可以同时在if/switch句块内进行变量初始化和条件检查。这有助于保持代码简洁精炼。通用形式为:

if( init-statement(x);condition(x)) {
// do some stuff here
} else {
// else has the scope of x
// do some other stuff
}

  1. 在编译时使用constexpr

constexpr 很棒!假如要评估一些表达式,且它的值一旦初始化就不会改变,那么可以预运算其值并将之作为宏。或者利用C++11提供的constexpr.

程序员倾向于尽量减少程序运行时间。所以,如果能让编译器进行一些操作并减小程序运行的压力,那么就可以缩短运行时间。

不知道C++这七大特性,绝对枉为圈中人

以上代码是constexpr的常见例子之一。既然声明斐波那契数列函数为constexpr, 那么编译器就可以在编译时预运算fib(20). 所以编译之后,可以用constlong long bigval = 2432902008176640000来替代const longlong bigval = fib(20).

请注意,传递参数是一个const 值。这是被声明为constexpr的函数的一个重点——传递参数应该是constexpr或const。否则这里的函数会和普通函数一样,也就是说编译时不进行预运算。

变量也可以是constexpr. 在这种情况下,这些变量在编译时必须可评估;否则会出现编译错误。

Interestingly, later introduced constexpr-if and constexpr-lambda in C ++ 17.

  1. Tuples tuples

Very similar to the pair, tuple is a set of fixed size values ​​of various data types.

C ++ does not know the seven characteristics, absolute human circle Wang Wei

Sometimes, compared to the tuple, use std :: array is more convenient. array with the array functions like plain C standard library of C ++. This data structure is the new C ++ 11.

  1. Class template argument deduction

The name of the property pretty long-winded. C ++ 17 start from the standard class template template argument deduction may also be carried out. Before, template argument deduction only support function template. The result is:

std::pair<std::string,int> user = {"M", 25}; // previous
std::pair user = {"M", 25}; // C++17

This derivation is "hidden." This tuple is even more convenient.

// previous
std::tuple<std::string, std::string, int> user ("M","Chy", 25);
// deduction in action!
std::tuple user2("M", "Chy", 25);

This feature is not much more useful to people not familiar with C ++ templates for.

  1. Smart Pointers

Pointer sometimes terrible. Since the C ++ language gives programmers a great degree of freedom, so sometimes it is easy to shoot yourself in the foot. And in many cases, the trouble is caused by the pointer.

Fortunately, C ++ 11 introduces smart pointers, smart pointers is much more convenient than ordinary pointer. They help programmers to prevent memory leaks through the timely release of memory. They also contribute to the code reaches an abnormal level of security.

C ++ introduces many new features to the latest version of the computer language. If you are interested, you can understand.

C ++ does not know the seven characteristics, absolute human circle Wang Wei

Guess you like

Origin blog.51cto.com/14209412/2404803