Added content in STL library in C ++ 11

Source: Internet
Author: User

A significant change in C ++ 11 is that some functions in the boost Library have been formally standardized and integrated into STL. This article will briefly introduce them.

Reference Wrapper)

When the template function parameter is of the generic type, you cannot export whether to pass the value or to reference it. By default, the value transmission method is used. You can use std: ref to explicitly specify the template function as a reference.

# Include <functional>
# Include
<Iostream>

Template <class
T>
Void foo (T
Arg)
{
Arg ++;
}

Int main ()
{
Int count = 3;

Foo (count); // The value is passed at this time. The template is instantiated as foo (int), and the count value remains unchanged.
Std: cout <count <std: endl;

Foo (std: ref (count); // reference is passed. The template is instantiated as foo (int &), and the count value is added to 1.
Std: cout <count <std: endl;
}

Smart Pointers)

Smart pointers mainly introduce shared_ptr, weak_ptr, and unique_ptr. Among them, shared_ptr and weak_ptr are the application objects in the boost library. I have also written related articles to introduce them. I will not introduce them here.

The newly introduced unique_ptr is similar to scoped_ptr in the previously introduced boost library, while STL itself has a similar auto_ptr. the main differences between them are:

  • Auto_ptr supports the '=' operation or the return value of the function.
  • Unique_ptr does not support the '=' operation and can be used as the return value of the function.
  • Scoped_ptr does not support the '=' operation and cannot be returned as a function.

Compared with auto_ptr, unique_ptr has lower permissions than auto_ptr, and does not have scope_ptr restrictions that cannot be used as return values. It is the most suitable method and can replace auto_ptr.

Imitation Functions

The four boost libraries are also integrated into the stl Library:

  • Function
  • Bind
  • Result_of
  • Mem_fn

Among them, function and bind have been introduced in the boost library before. After the auto keyword is supported, it is easier to create a function through bind, we can create a member function with just one sentence.

# Include
<Iostream>
# Include
<Functional>
Using
Namespace std;
Using
Namespace std: placeholders;

Struct
X
{
Bool foo (int
A) {cout <a <endl; return
False ;}
};

Int main ()
{
X x;

Auto func = bind (& X: foo, & x, _ 1 );
Func (5 );
}

PS: when using bind, you need to add the std: placeholders using. Otherwise, a syntax error is reported during compilation.

However, it seems that the bind is basically given by the lambda expression for seconds. In the above example, the lambda expression is written as follows:

Auto func = [& x] (int
A) {x. foo ();};
Function <void (int)> func = [& x] (int
A) {x. foo ();};

Lambda expressions are syntactic sugar, so the readability is better (I feel that the conciseness is almost close to the anonymous function of C #). There are no placeholders SUCH AS _ 1 and _ 2, the function call method is also explicit and direct, which is more intuitive.

Result_of is basically useless after auto is introduced. It is much easier to directly use auto.

Container

The container mainly includes the following:

  1. Tuple
  2. Array
  3. Unordered_set and unordered_map

Here, tuple and array are basically boost-related libraries standardized, while unordered_set and unordered_map are set and map in the hash table mode to provide higher query performance. The usage is similar to that of the original binary tree version.

Regular Expression

The Boost regex library has finally been standardized. To use string processing, you do not need to look for third-party Regular Expression Libraries. At present, VC does not support escape characters such as C # (gcc is acceptable), and regular expressions in the Code are still very difficult to read. We hope MS can support raw string literal as soon as possible.

Thread

The Boost thread library is also standardized, and that is similar. the packaged_task of the Net TPL library is also standardized. Since it has a lot to introduce, I will write an article to introduce it. I will not talk about it here.

Time Functions

In fact, the C language standard library provides time functions, but it is extremely difficult to use. Now the Boost time function chrono has been standardized, although it is still not comparable. net TimeSpan is easy to use, but at least it is much better than standard C.

# Include
<Iostream>
# Include
<Chrono>
# Include
<Ctime>
Using
Namespace std;

Int fibonacci (int
N)
{
If (n <3) return 1;
Return maid (n-1) + maid (n-2 );
}

Int main ()
{
Auto start = chrono: system_clock: now ();
Int result = maid (40 );
Auto end = chrono: system_clock: now ();

Int elapsed_seconds = chrono: duration_cast <chrono: milliseconds>
(End-start). count ();

Auto end_time = chrono: system_clock: to_time_t (end );

Std: cout <"result:" <result <endl
<"Finished computation at" <std: ctime (& end_time)
<"Elapsed time:" <elapsed_seconds <"ms \ n ";
}

Another Date function, Boost. Date, does not seem to have been standardized. To use Date-related functions, you can only use the boost library.

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.