Stl_ algorithm _ Fill new values (fill, fill_n, generate, Generate_n)

Source: Internet
Author: User

C + + Primer learning ...

Simple recording of My Learning process (code-based)


All containers apply

Fill (b,e,v)//[b,e) is filled into V
Fill_n (b,n,v)//[b,b+n) filled into V
Generate (B,e,p)//[b,e) is populated according to the P method
Generate_n (b,n,p)//[b,b+n) is populated according to the P method


/**------http://blog.csdn.net/u010579068------**/#include <iostream> #include <cstdio> #include < string> #include <vector> #include <list> #include <deque> #include <ctime> #include < Algorithm>using namespace std;/*****************************************//All containers apply Fill (b,e,v)//[b,e) filled into vfil L_n (b,n,v)//[b,b+n) filled into vgenerate (b,e,p)//[b,e) fill Generate_n (b,n,p)//[b,b+n according to P method and fill ******* according to P method **********************************//**------------------------------------------------------------------------- -------------------------------------------------------------------------------------------**//****************                           Std::fill All sort containers apply Algorithm--------------------------------------------------------------------------------------template < C Lass ForwardIterator, class T > void Fill (forwarditerator first, ForwarDiterator last, const t& value),//eg:template < class ForwardIterator, class T > void Fill (forwarditerator f Irst, ForwardIterator last, const t& value) {while (first! = last) *first++ = value;} *************************************************************************************//************************                           Std::fill_n All sort containers apply Algorithm--------------------------------------------------------------------------------------template < Class Outputiterator, Class Size, class T > void Fill_n (outputiterator first, Size N, const t& value);//eg:temp Late < class Outputiterator, class Size, class T > void Fill_n (outputiterator first, Size N, const t& value) {for (; n>0;--n) *first++ = value;} *************************************************************************************//************************ *****************************************Std::generate all sort containers for algorithm------------------------- -------------------------------------------------------------Template <class ForwardIterator, class Generator > void Generate (ForwardIterator first, ForwardIterator last, Generator gen);//eg:template <class ForwardIterator , class generator> void Generate (ForwardIterator first, ForwardIterator last, Generator Gen.) {while (first! = Last ) *first++ = Gen ();} *************************************************************************************//************************                          Std::generate_n All sort containers apply Algorithm--------------------------------------------------------------------------------------template < Class Outputiterator, Class Size, class generator> void Generate_n (outputiterator first, size n, Generator gen);//e G:template <class OutputiteRator, Class Size, class generator> void Generate_n (outputiterator first, Size N, Generator Gen) {for (; n>0;- -N) *first++ = Gen ();} function Generator:int Randomnumber () {return (rand ()%100);}    Class generator:struct c_unique{int current;    C_unique () {current=0;    } int operator () () {return ++current; }} Uniquenumber;int current (0), int unique_number () {return ++current;}                       int main () {vector<int> myvector (8);   myvector:0 0 0 0 0 0 0 0 Fill (myvector.begin (), Myvector.begin () +4,5);   Myvector:5 5 5 5 0 0 0 0 Fill (myvector.begin () +3,myvector.end () -2,8);    Myvector:5 5 5 8 8 8 0 0 cout << "myvector contains:";    For (Vector<int>::iterator It=myvector.begin (); It!=myvector.end (); ++it) cout << "" << *it;    cout << Endl; /**---------------------------------------------------------------------------------**/deque<int> Mydeque (8,10);     Mydeque:10 Fill_n (Mydeque.begin (), 4,20);   MYDEQUE:20 Ten Fill_n (Mydeque.begin () +3,3,33);    mydeque:20 cout << "mydeque contains:";    For (Deque<int>::iterator It=mydeque.begin (); It!=mydeque.end (); ++it) cout << "" << *it;    cout << Endl; /**---------------------------------------------------------------------------------**/srand (unsigned (    NULL));//Vector<int> Myvector (8);    Vector<int>::iterator it;    Generate (Myvector.begin (), Myvector.end (), randomnumber);    cout << "myvector contains:";    For (It=myvector.begin (); It!=myvector.end (); ++it) cout << "" << *it;    Generate (Myvector.begin (), Myvector.end (), uniquenumber);    cout << "\nmyvector contains:"; For (It=myvector.begin (); IT!=MYVECTOR.End ();    ++it) cout << "<< *it;    cout << Endl;    /**---------------------------------------------------------------------------------**/int myarray[9];    Generate_n (myarray, 9, unique_number);    cout << "myarray contains:";    for (int i=0; i<9; ++i) cout << "" << myarray[i];    cout << Endl; return 0;}  /******output:myvector Contains:5 5 5 8 8 8 0 0 Mydeque contains:20-ten Myvector contains:64 6 Myvector contains:1 2 3 4 5 6 7 8 myarray contains:1 2 3 4 5 6 7 8 9******/

Stl_ algorithm _ Fill new values (fill, fill_n, generate, Generate_n)

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.