C++11 new Feature application--introduction of several new convenience algorithms (algorithms for changing the order of elements in a container)

Source: Internet
Author: User
Tags ranges shuffle

Yesterday a list of new algorithms in c++11, including Find_if_not, all_of, Any_of, none_of Four algorithms, the common denominator of these four algorithms is non-modifying sequence operations.

So, today comes the new algorithm in 818 c++11, and these algorithms are characterized by: modifying sequence operations.

Copy algorithm We are very familiar with, here introduce c++11 new copy_n.

Copy_n
Prototype:

template <classclassclass OutputIterator>  firstresult);

Role:
Copies the first n elements from the range beginning at first to the range beginning at result.

The return value is critical:
The function returns a iterator to the end of the destination range (which points to one past the last element copied).

If n is a negative number:
If n is negative, the function does nothing.

If the two intervals are duplicated:
If the ranges overlap, some of the elements in the range pointed by result could have undefined but valid values.

Application:

#include <iostream> //std::cout #include <algorithm> //std::copy #include <vector> //std::vector intMain () {intMyints[] = {Ten, -, -, +, -, -, -};STD:: vector<int>Myvector; Myvector.resize (8);//Allocate space for 7 elements    STD:: Copy_n (Myints,7, Myvector.begin () +1);STD::cout<<"Myvector contains:"; for(STD:: vector<int>:: Iterator it = Myvector.begin (); It! = Myvector.end (); ++it)STD::cout<<"'<< *it;STD::cout<<' \ n ';STD:: vector<int>Myvector2; Myvector2.resize (8);STD:: Copy_n (Myints,-1, Myvector2.begin () +1);STD::cout<<"Myvector2 contains:"; for(STD:: vector<int>:: Iterator it = Myvector2.begin (); It! = Myvector2.end (); ++it)STD::cout<<"'<< *it;STD::cout<<' \ n ';return 0;}//output:Myvector contains:0 Ten  -  -  +  -  -  -Myvector2 contains:0 0 0 0 0 0 0 0

copy_if
Prototype:

template <classclassclass UnaryPredicate>  firstlast,                          result, UnaryPredicate pred);

Role:
Copies the elements in the range [First,last] for which pred returns true to the range beginning at result.

Application:

#include <iostream> //std::cout #include <algorithm> //std::copy_if, std::d istance #include <vector> //std::vector intMain () {STD:: vector<int>Foo = { -, the,5,-5,- the};STD:: vector<int>Bar (Foo.size ());//Copy only positive numbers:  Autoit =STD:: Copy_if (Foo.begin (), Foo.end (), Bar.begin (), [] (inti) {return! (i<0);}  ); Bar.resize (STD::d istance (Bar.begin (), it));//Shrink container to new size  STD::cout<<"bar contains:"; for(int& X:bar)STD::cout<<"'<< x;STD::cout<<' \ n ';return 0;}

Move and Move_backward
Move is a cliché, but it's important to note that:
The value of the elements in the [First,last] is transferred to the elements pointed by result. After the call, the elements in the range [First,last] is the left in an unspecified but valid state.

Give the example code directly:

#include <iostream> //std::cout #include <algorithm> //Std::move (ranges) #include <utility> //Std::move (objects) #include <vector> //std::vector #include <string> //std::string intMain () {STD:: Vector<std::string>Foo = {"Air","Water","Fire","Earth"};STD:: Vector<std::string>Bar (4);//Moving ranges:  STD::cout<<"Moving ranges...\n";STD:: Move (Foo.begin (), Foo.begin () +4, Bar.begin ());STD::cout<<"foo contains"<< foo.size () <<"Elements:";STD::cout<<"(each of the unspecified but valid state)";STD::cout<<' \ n ';STD::cout<<"bar contains"<< bar.size () <<"Elements:"; for(STD::string& X:bar)STD::cout<<" ["<< x <<"]";STD::cout<<' \ n ';//Moving container:  STD::cout<<"Moving container...\n"; Foo =STD:: Move (bar);STD::cout<<"foo contains"<< foo.size () <<"Elements:"; for(STD::string& X:foo)STD::cout<<" ["<< x <<"]";STD::cout<<' \ n ';STD::cout<<"Bar is in a unspecified but valid state";STD::cout<<' \ n ';return 0;}//output:Moving Ranges...foo contains4Elements: (Each of the unspecified but the valid state) bar contains4elements: [air] [water] [fire] [earth]moving Container...foo contains4elements: [air] [water] [fire] [Earth]bar are in a unspecified but valid state

Let's talk about Move_backward:
Prototype:

template <classclass BidirectionalIterator2>  first,                                        last,                                        result);

Role:
Moves the elements in the range [First,last] starting from the end to the range terminating at result.

The function returns a iterator to the first element in the destination range.

The function begins by moving (last-1) to (result-1), and then follows backward by the elements preceding these , until first is reached (and including IT).

Do not understand the application, C + + reference on the example of VS2015 can not be run, and so on, and then to engage.

Shuffle
Shuffle has the meaning of reorganization.
Prototype:

template <classclass URNG>  void shuffle (RandomAccessIterator first, RandomAccessIterator last, URNG&& g)

Role:
Rearranges the elements in the range [first,last) randomly, using G as uniform random number generator.

Application:

#include <iostream> //std::cout #include <algorithm> //Std::shuffle #include <array> //Std::array #include <random> /std::d efault_random_engine #include <chrono> //Std::chrono::system_clock intMain () {STD:: Array<int, 5>foo{1,2,3,4,5};//Obtain a time-based seed:    unsignedSeed =STD:: Chrono::system_clock::now (). Time_since_epoch (). Count (); Shuffle (Foo.begin (), Foo.end (),STD::d efault_random_engine (Seed));STD::cout<<"shuffled elements:"; for(int& X:foo)STD::cout<<"'<< x;STD::cout<<' \ n ';return 0;}//Output//shuffled elements:4 2 1 3 5

The above code is used in the header file Chrono, in the previous blog has been told, but also is a c++11 provides a number of convenient tools.

C++11 new Feature application--introduction of several new convenience algorithms (algorithms for changing the order of elements in a container)

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.