Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the seven characteristics of C++

2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the relevant knowledge of "what are the seven characteristics of C++". In the operation of actual cases, many people will encounter such a dilemma. Then let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

1. Keyword auto

Programmers wept with delight when auto was introduced for the 11th time.

The point of auto is to enable the C++ compiler to derive data types at compile time, so that you don't have to declare data types every time. When the data type is

Map

Without initializer, data types cannot be declared (see line 5). It makes sense. The fifth line of instruction does not ask the compiler to derive the data type.

At first, the functionality of auto was limited. Later, in the new version of C++, the function of auto becomes more and more powerful.

Parenthesis initialization (bracketedinitialization) is used in lines 7 and 8, which is also one of the new features of Clippers 11.

Note that when using auto, the compiler must be able to derive data types.

An interesting question is: what happens if you write autoa = {1,2,3}? Is this a compilation error? Is it a vector?

In fact, std::initializer_list has been introduced into Central11. If you declare auto, the curly braces initialization list is treated as a lightweight container.

Finally, as mentioned earlier, compiler type derivation is helpful when data structures are complex:

Don't forget to check line 25! auto [v1Magol v2] = itr.second is purely a new feature of Clipper 17. This feature is called structured binding. In the old version of C++, programmers needed to get each variable individually. But structured binding facilitates this process. In addition, if you want to get the data to use the reference (reference), you only need to add a symbol--auto& [v1memev2] = itr.second.

2. Lambda expression

Clocking 11 introduces lambda expressions, which are similar to anonymous functions in JavaScript. They are all function objects, have no names, and capture variables in different scopes based on concise syntax. They can also be assigned to variables.

Lambdas is useful if you need to do something small and fast in your code and don't want to write a separate function for it. Another common use is to use lambdas as a comparison function.

The above examples can illustrate a lot of problems.

First, notice how curly braces initialization increases the weight. Then there is the generic begin (), end () (this is also a new part of Clippers 11). Then comes the lambda function as a data comparator. The argument to the lambda function is declared as auto (this is a new part of Clippers 14). It is not possible to use auto for function parameters prior to Clipper 14.

As defined in the awesome library of modern C++:

[]-No objects are captured. Therefore, you cannot use global-scoped local variables within lambda expressions, only parameters.

[=]-captures local objects (local variables, parameters) in scope by value. Can only be used and cannot be modified.

[&]-captures local objects (local variables, parameters) in scope by reference. Can be modified. Examples are as follows.

[this]-captures the this pointer by value.

[a, & b]-capture object a by value and object b by reference.

Therefore, if you want to convert the data into other formats within the lambda function, you can take advantage of the scope to use lambda. For example:

In the above example, if you capture ([factor]) a local variable by value in a lambda expression, you cannot change the factor in the fifth line. The reason is simple-no permissions.

Finally, notice that val is used as a reference in the example. This ensures that any change within the lambda function will change the vector.

After learning modern C++, they are in full bloom! (photo: Ian Schneider Image Source: Unsplash)

3. Initialization statements in if/switch

This feature of Category 17 is very pleasing:

Obviously, variable initialization and conditional checking can now be done at the same time within the if/switch block. This helps keep the code concise and concise. The common form is:

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

4. Use constexpr at compile time

Constexpr is great! If you want to evaluate some expressions and their values do not change once initialized, you can precompute their values and use them as macros. Or make use of the constexpr provided by Clover 11.

Programmers tend to minimize the running time of programs. So, if you can get the compiler to do something and reduce the pressure on the program to run, you can shorten the run time.

The above code is one of the common examples of constexpr. Since the Fibonacci sequence function is declared as constexpr, the compiler can precompute fib (20) at compile time. So after compilation, you can use constlong long bigval = 2432902008176640000 instead of const longlong bigval = fib (20).

Note that the passing parameter is a const value. This is a key point of a function declared as constexpr-- the argument passed should be constexpr or const. Otherwise, the functions here will be the same as ordinary functions, that is, there is no preoperation at compile time.

The variable can also be constexpr. In this case, these variables must be evaluable at compile time; otherwise, a compilation error will occur.

Interestingly, constexpr-if and constexpr-lambda.

5. Tuples tuple

Much like pair, tuple is a collection of fixed-size values of various data types.

Sometimes it is more convenient to use std::array than tuple. Array is similar to the plain C array with the functionality of the C++ standard library. This data structure is a new addition to Category 11.

6. Derivation of class template parameters

The name of this feature is quite verbose. Starting from Category 17, standard class templates can also be used to derive template parameters. Previously, template parameter derivation only supported function templates. The result is:

Std::pair user = {"M", 25}; / / previous std::pair user = {"M", 25}; / / Clear17

This derivation is "recessive". This is even more convenient for tuple.

/ previous std::tuple user ("M", "Chy", 25); / / deduction in action! Std::tuple user2 ("M", "Chy", 25)

The above feature is of little use to people who are not familiar with C++ templates.

7. Intelligent pointer

Pointers can be scary. Because the C++ language provides a great degree of freedom for programmers, it is sometimes easy to shoot yourself in the foot. And in many cases, the trouble is caused by pointers.

Fortunately, smart pointers are introduced in Craft 11, which is much more convenient than ordinary pointers. They help programmers prevent memory leaks by releasing memory at the right time. They also help your code reach an abnormal security level.

This is the end of the content of "what are the Seven characteristics of C++". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report