C++11 14 features commonly used in __c++

Source: Internet
Author: User
Tags assert
ThreadStudy.cpp: Defines the entry point for a console application.
//
Summary of characteristics of C11

#include "stdafx.h"
#include <iostream>
#include <vector>
#include <list>
#include <map>
#include <string>
#include <memory>
#include <functional>
#include <algorithm>
#include <assert.h>
#include <thread>
#include <regex>
#include <random>

void Property 1 ()
{
Type Auto Derivation
Auto i = 42; I am an int
Auto L = 42LL; L is a long long

The following is a very good way to go.
Std::vector<int> VI;
for (Std::vector<int>::iterator ite = Vi.begin (); Ite!= vi.end (); ++ite)
{
}
for (Auto ite = Vi.begin (); Ite!= vi.end (); ++ite)
{
}
}

void Property 2 ()
{
The null pointer has a macro elevation to the keyword, resulting in a more secure compilation check
int res = 0;
int *p1 = nullptr;
int *p2 = NULL;
Cannot compile through
int i1 = nullptr;
can be compiled by
int i2 = NULL;
}

void Property 3 ()
{
The original tedious cycle is now much simpler.
Std::map<std::string, std::vector<int>> map;
Std::vector<int> v;
V.push_back (1);
V.push_back (2);
V.push_back (3);

map["one"] = V;

for (const Auto & Kvp:map)
{
Std::cout << Kvp.first << Std::endl;
for (auto V:kvp.second)
{
Std::cout << v << std::endl;
}
}
int arr[] = {1, 2, 3, 4, 5};
for (auto& E:arr)
{
E = e * e;
}
}


void Property 4 ()
{
Let the ambiguous overload become explicit
This often becomes a headache topic for test programmers
Now do a more rigorous check, declare want to overload, and don't want to overload
{
Class A
{
Public
virtual void f (short) {std::cout << "a::f" << Std::endl;}
};

Class B:public A
{
Public
virtual void f (int) {std::cout << "b::f" << Std::endl;}
};

The output is actually a
A *p = new B ();
P->f (1);
}
{
Class A
{
Public
virtual void f (int) const {std::cout << "a::f" << Std::endl;}
};
Class B:public A
{
Public
virtual void f (int) {std::cout << "b::f" << Std::endl;}
};

A *p = new B ();
P->f (1);
}
The top two actually output a.
{
Class A
{
Public
virtual void f (short) {std::cout << "a::f" << Std::endl;}
This virtual function should not be overloaded
virtual void g (int) Final {std::cout << "a::g" << Std::endl;}
};
Class B:public A
{
Public
Declares that you want to overload if no overload successfully reports a compilation error
virtual void F (short) override {std::cout << "b::f" << Std::endl;}
If the overload will cause an error
virtual void g (int) {std::cout << "a::g" << Std::endl}//Error C3248: "main::a::g": a function declared "final" cannot Be "Ma" In::b::g "rewrite
No overloads indeed
virtual void g (float) {std::cout << "a::g" << Std::endl;}//Overload
};
}
}

void Property 5 ()
{
Strongly typed enumerations can be compiled by
Enum class N1 {None, one, all};
Enum class N2 {None, one, all};
Traditional methods appear to redefine
/*
Enum A1
{
None, one, all
};
Enum A2
{
None, one, all
};*/
}

void Property 6 ()
{
Unique_ptr cannot assign to another object (no copy constructor), if you really want to do this, move it.
If you have a pointer to an object, use Unique_ptr, and if multiple pointers point to an object, use the shared_ptr (this class uses the reference count)
Weak_ptr:shared_ptr's assistant. Look at this name is destined to be an assistant, want to observe the object, and as much as possible to reduce the Heisenberg effect

Use of a smart pointer array
Std::shared_ptr<std::vector<int>> PVI = std::make_shared<std::vector<int>> ();
Std::vector<std::shared_ptr<int>> VPI;
Vpi.push_back (std::make_shared<int> (1));

std::unique_ptr<int> px1 = std::make_unique<int> (10);
The following remark will cause a compilation error
std::unique_ptr<int> px2 = px1;

Auto SP = std::make_shared<int> (10);

ASSERT (sp.use_count () = = 1);
std::weak_ptr<int> wp (SP);
ASSERT (wp.use_count () = = 1);
if (!wp.expired ())
{
Std::shared_ptr<int> SP2 = Wp.lock ();
*SP2 = 100;
ASSERT (wp.use_count () = = 2);
}
ASSERT (wp.use_count () = = 1);
}

void Property 7 ()
{
If an anonymous function is used in the function pointer of a condition variable, the value of the parameter variable when awakened is still the value of the sleep (that is, the value has changed).
This allows you to use pointers instead
Std::vector<int> v;
V.push_back (1);
V.push_back (2);
V.push_back (3);
Std::for_each (Std::begin (v), Std::end (v), [] (int n) {std::cout << n << std::endl;});
Auto is_odd = [] (int n) {return n% 2 = 1;};
Auto pos = std::find_if (Std::begin (v), Std::end (v), is_odd);
if (Pos!= std::end (v))
{
Std::cout << *pos << Std::endl;
}
}

void Property 8 ()
{
Non-member Begin () and end ()
For example, there's nothing to talk about.
int arr[] = {1, 2, 3};
Std::for_each (Std::begin (arr), Std::end (arr), [] (int n) {std::cout << n << std::endl;});
Auto is_odd = [] (int n) {return n% 2 = 1;};
Auto pos = std::find_if (Std::begin (arr), Std::end (arr), is_odd);
if (Pos!= std::end (arr))
Std::cout << *pos << Std::endl;
}

Template <typename T, size_t size>
Class Vector
{
Static_assert (Size > 3, "Size is too small");
T _points[size];
};

Template <typename T1, TypeName t2>
T1 Add (T1 t1, T2 T2)
{
Static_assert (Std::is_integral<t1>::value, "Type T1 must be Integral");
Static_assert (Std::is_integral<t2>::value, "Type T2 must be Integral");
return t1 + T2;
}

void Property 9 ()
{
Static_assert and type traits
These two are mainly used for compile-time (also mainly for the master of the People), put up with two examples
1
Vector<int, 11> A1;
Vector<double, 2> A2;

Std::cout << Add (1, 3) << Std::endl;
Std::cout << Add (1.1, 2) << Std::endl;
}

void Property 10 ()
{
Move semantics take a closer look at this blog post, which is very clear.
Class A
{
Public
A ()
{
printf ("A () \ n");
}
~a ()
{
printf ("~a () \ n");
}
This function costs a high price and needs to replicate the object
A (const a& Other)
{
printf ("A (const a& Other)");
}

This function is low cost, just swapping objects
A (a&& Other)
{
printf ("A (a&& other) \ n");
}
};

Std::list<a> va;
A A;
The left value automatically calls A (const a& Other)
Va.push_back (a);
Call a (a&& other) by means of semantic transfer, the object of a will be discarded after the normal use, the risk behind the programmer is responsible for
Va.push_back (Std::move (a));
The compiler knows it is the right value, so automatically calls a (a&& other), and the temporary object disappears automatically, without the risk of the above line of code
A temporary object that is swapped back also needs to be destructor
Va.push_back (A ());
}

void F ()
{
}

void Property 11 ()
{
Line threading, line threading also contains a series of functions such as lock, condition variable, atomic operation, etc.
Std::thread T (f);
T.join ();
}

void Property 12 ()
{
Regular expressions
using namespace Std;
Cmatch match;
if (Regex_search ("AAAAAAAAABBBBBBBBC", Match, Regex ("^a*b+c?$"))
{
for (auto Obj:match)
{
cout << obj << Endl;
}
}
Else
{
cout << "No Match case!" << Endl;
}
}

void Property 13 ()
{
Time Library
using namespace Std;
using namespace Chrono;
Auto begin = Std::chrono::system_clock::now ();
This_thread::sleep_for (Seconds (3));
Auto end = System_clock::now ();

Auto Duration = duration_cast<microseconds> (end-begin);
cout << "spent" << (Duration.count () * microseconds::p eriod::num)/microseconds::p eriod::d en << "SEC" < < Endl;
}

void Property 14 ()
{
Random number Generation library, also includes normal distribution, uniform distributed generation
Std::random_device Rd;
for (int n = 0; n < ++n)
Std::cout << Rd () << Std::endl;
}
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.