• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            C++ Programmer's Cookbook

            {C++ 基礎(chǔ)} {C++ 高級} {C#界面,C++核心算法} {設(shè)計(jì)模式} {C#基礎(chǔ)}

            理解STL---understanding stl

            Introduction

            STL has a lot of powerful features which are undiscovered for many programmers. Complexity of templates stops people from discovering the scope of STL. Here, I am removing that complexity by explaining with template’s generated code with commonly used data types. This article intends to trigger you for exploring the internals of STL and use its powerful features. Once you understand the features of STL, you will get addicted of using it in your development. Explaining all the container and algorithm internals are out of the scope of this article. I choose vector and some sample algorithms to explain the usage of STL.

            What is STL?

            The Standard Template Library (STL) is a general-purpose C++ library of algorithms and data structures, originated by Alexander Stepanov and Meng Lee. The STL, based on a concept known as generic programming, is part of the standard ANSI C++ library. The STL is implemented by means of the C++ template mechanism, hence its name. While some aspects of the library are very complex, it can often be applied in a very straightforward way, facilitating reuse of the sophisticated data structures and algorithms it contains.

            Why should I learn STL?

            STL gives generic collections and algorithms which can be applied on those collections. STL code can be used across the platforms. Since STL containers and algorithms are C++ templates, you are able to use any data type, if it satisfies the requirement of the templates. You can make generic code which will accept all types of container classes. Examples of STL containers are deque, list, map, multimap, multiset, set, and vector; and examples of algorithms are find, copy, remove, max, sort, and accumulate.

            Vector and Algorithm Relation

            Let us try to have an idea of how algorithm ‘remove’ works on a vector. Vector is an STL container which grows dynamically and have similar features of an array.

            #include <iostream>
            #include <vector>
            #include <deque>
            #include <list>
            #include <algorithm>
            using namespace std;
            void main()
            {  
                typedef /*list/deque*/vector<int > intVect ;
                typedef intVect::iterator intVectIt ;
                intVectIt end ,it,last;
                        
                intVect Numbers ;  
                Numbers.push_back ( 10 );Numbers.push_back ( 33 ); Numbers.push_back(50);
                Numbers.push_back(33);    
                cout << "Before calling remove" << endl ;
                end = Numbers.end();
                cout << "Numbers { " ;
                for( it = Numbers.begin(); it != end; it++)
                    cout << *it << " " ;
                cout << " }\n" << endl ;
                last = remove(Numbers.begin(), end, 33) ;
                cout << "After calling remove" << endl ;
                cout << "Numbers { " ;
                for(it = Numbers.begin(); it != last; it++)
                    cout << *it << " " ;
                cout << " }\n" << endl ;
            }

            Output of the program

            Before calling remove
            Numbers { 10 33 50 33  }
             
            After calling remove
            Numbers { 10 50  }

            In the above example, we insert 10, 33, 50, and 33 into vector Numbers. Algorithm ‘remove’ is used to remove all the elements which has a value 33. ‘remove’ uses iterator of vector for this operation. iterators have similarities to pointers and are used to traverse an array of objects. In implementation of vector<int>, it's iterator is int*.

            Now, change vector to deque or list in the first line. You can see our program is working exactly same as in the vector case! For changing the container implementation, we had to change only one line of code. This is one of the big benefits which is offered by STL. Now, take the above piece of code to some other operating system (I tested with Windows and Linux ), build and run it. Without any change, it works! This is another big advantage STL offers. You can try different data types instead of int for vector container.

            Constructors of vector

            Now, let us look at how default constructor and constructor with an integer argument work. I will be using object of the following class for inserting into the vector.

            class A
            {
                int iValue;
                A()
                {
                    cout << "A()" << endl;
                }
            };

            Class vector<A> has four member variables: allocator, _First, _Last, and _End. allocator is of type std::allocator<A>, and _First, _Last, and _End are iterators which come out to be A*. Default constructor of vector<A> initializes allocator with std::allocator<A> object. _First, _Last, _End are initialized to null pointers.

            Now, let us see how constructor vector(size_type _N, const _Ty& _V = _Ty(), const _A& _Al = _A()) works. By removing template variables for class A, prototype of the constructor becomes:

            vector ( unsigned int _N , const A& _V = A(), 
              const std::allocator<A> & _A1 = std::allocator<A> () )

            This constructor does the following things to initialize its member variables:

            1. _First = allocator.allocate(_N, (void *)0);
            2. _Ufill(_First, _N, _V);
            3. _Last = _First + _N;
            4. _End = _Last;

            allocator.allocate calls template function _Allocate passing number of objects to be created (_N). For class A, this template function is generated to A* _Allocate ( int , A * ).

            This function allocates _N * sizeof ( A ) memory and returns the starting address. This is assigned to _First. _Ufill does something like this:

            for (; 0 < _N; --_N, ++_F)
                allocator.construct(_F, _X);

            Here, _F ( of type A* ) points to the address where object to be constructed from _X through A ( const A & ).

            _Ufill copies the one object constructed (const reference to object is _V. Refer line 2.) to all _N locations. allocator.construct calls template function _Consturct passing the same arguments. A new object is created at location _F from reference to object _X (new placement operator is used here). In line 3, _Last is assigned to address after last object. _End and _Last point to the same location.

            Now, let us have a look at how the destructor works. Destructor of the vector does the following things:

            _Destroy(_First, _Last);
            allocator.deallocate(_First, _End - _First);
            _First = 0, _Last = 0, _End = 0;

            _Destroy calls allocator.destroy ( _F ) for each A* _F from _First through _Last. This function will call destructor ~A() explicitly to destroy the object (remember, we allocated using placement new operator). allocator.deallocate deletes the memory pointed to by (_First) which will free memory allocated for all the objects in the vector.

            How push_back works?

            Most complicated and most frequently used function in a vector is push_back. Let us analyze how push_back works? push_back will insert new data at the end of the vector. It calls insert ( end(), _X). (Member function end() returns _Last and _X is reference to the object (class A) to be added.) insert calls another overloaded function insert (_Last , 1 , _X).

            For class A, this function definition becomes:

            void insert(A* _P, unsigned int _M, const A& _X)
            {
                if (_End - _Last < _M)
                {
                    unsigned int _N = size() + (_M < size() ? size() : _M);
                    A* _S = allocator.allocate(_N, (void *)0);
                    A* _Q = _Ucopy(_First, _P, _S);
                    _Ufill(_Q, _M, _X);
                    _Ucopy(_P, _Last, _Q + _M);
                    _Destroy(_First, _Last);
                    allocator.deallocate(_First, _End - _First);
                    _End = _S + _N;
                    _Last = _S + size() + _M;
                    _First = _S;
                }
                else if (_Last - _P < _M)
                {
                    _Ucopy(_P, _Last, _P + _M);
                    _Ufill(_Last, _M - (_Last - _P), _X);
                    fill(_P, _Last, _X);
                    _Last += _M; 
                }
                else if (0 < _M)
                {
                    _Ucopy(_Last - _M, _Last, _Last);
                    copy_backward(_P, _Last - _M, _Last);
                    fill(_P, _P + _M, _X);
                    _Last += _M; 
                }
            }

            Before explaining this code, we should look at the difference between _End and _Last. _End is the end of the buffer allocated to the vector, and _Last is the end of the last value inserted. To make it clear, take the case of pushing back object of A to a vector which currently contains three objects, and assume _End and _Last point to the same location. In this case, allocator will allocate memory for 3+3 objects even though we have to insert only one. This is to reduce reallocations. After push_back, _End will point to the end of 6th object, and _Last will point to the end of the 4th object. During push_back, insert checks whether reallocation is needed ((_End - _Last < _M)). If reallocation is needed, it allocates double size or size() + _M, whichever is higher. It copies all the data to the new buffer and removes the earlier buffer. It then inserts new data at the _Last position and reassigns _Last and _End. If we have enough space to insert a new element (i.e., _End - _Last > _M), new element is inserted at _Last, and _Last is reassigned.

            STL Algorithms (how it works with a vector)

            Before understanding algorithms, we will have to learn how function objects work. Class of a function object defines operator()(). The result is, a template function can not detect whether you passed a pointer to a function or object of a class having operator()(). Following example will give you a clear idea about what are function objects:

            #include <iostream.h>
            void  f ()
            {
                cout << "f()" << endl;
            }
             
            class X
            {
            public:
                void operator()()
                {
                    cout << "X::operator()" << endl;
                }
            };
             
            template < class T >
            void test_func ( T f1 )
            {
                f1();
            }
            void main()
            {
                X a;
                test_func(f );
                test_func(a);
            }

            Output of the program is:

            f()
            X::operator()

            Function objects can be classified as Generator (no argument), UnaryFunction (single argument), and BinaryFunction (takes two arguments). A special case of unary and binary functions are predicates (UnaryPredicate, BinaryPredicate) which simply means function returns a bool. STL has, in the header file <functional>, a set of templates that automatically create function objects for you. It is powerful not only because it’s a reasonably complete library of tools, but also because it provides a vocabulary for thinking about problem solutions, and because it is a framework for creating additional tools.

            How copy works?

            Problem: copy all the data of one vector<A> to another vector<A>.

            Analysis: For class A, function definition becomes (you have to pass arguments which support operator ++ () and operator *(). You can apply * and ++ to iterators.):

            copy ( A* first , A* last , A* x )

            Function copy evaluates *(x+N) = * ( first + N ) for all N in the range [0, last-first]. It returns x+N. Consider vector<A> objects sourceA, DestA. For copying all data from sourceA to DestA, you have to call copy (sourceA.begin() , sourceA.end() , DestA.begin()).

            Before calling copy, ensure that destination vector has enough space to accommodate all the data in source vector.

            How transform works?

            Problem: you have a vector<int> of three members and you want to store square of each element in another vector<int>.

            Solution: following piece of code does the job for you:

            int square_it ( const int x)
            {
                return x*x;
            }
            void main ()
            {
                vector < int > v1, v2(3);
                v1.push_back(2);
                v1.push_back(5);
                v1.push_back(83);
                transform ( v1.begin() , v1.end() , v2.begin() , square_it );
                for ( vector<int>::iterator it = v2.begin(); it != v2.end() ; it++ )
                    cout << *it;
            }

            For the above code, function prototype generated for transform is:

            int * trnsform ( int * First , int * Last , int* x, int (*f) (const int) )

            transform evaluates *(x + N ) = square_it (*(First + N ) ) for all N in the range [0, Last - First]. Note: v2 has enough memory allocated (memory for three integers) before calling transform.

            How generate works?

            Problem: insert three ascending numbers in a vector without using push_back.

            Solution: following piece of code does the job for you:

            int f ()
            {
                static int x = 1;
                return ++x;
            }
            void main ()
            {
                vector < int > v2(3);
                generate ( v2.begin() , v2.end() , f );
                for ( vector<int>::iterator it = v2.begin(); it != v2.end() ; it++ )
                    cout << *it;
            }

            For the above code, generated function prototype for ‘generate’ is:

            void generate ( int* first , int* last , int (*f)() )

            generate evaluates *(first + N ) = f ( ) for all N in the range [0 , last - first].

            How replace_if works?

            Problem: change all 0 to -1 in the vector<int>.

            Solution: assume vector<int> v contains integers 1, 2, 0, 6, 0. Following code will change all the 0s to -1:

            int f (const int x)
            {
               if ( x ==  0 )
                   return true;
               else
                   return false;
            }
            replace_if( v.begin() , v.end() , f , -1);

            Generated prototype for replace_if is:

            void replace_if ( int * first , int * last , int (*f) ( const int) , -1 )

            For all N in the range [0, last-first], replace_if evaluates to:

            if ( f ( *(first+N) ) )
                *(first + N ) = -1;

            How for_each works?

            Problem: print all the members of vector.

            Solution: following piece of code does the work for you:

            void f (const int x)
            {
                cout << x << endl;
            }
            for_each ( v.begin() , v.end() , f);

            Prototype generated for the above code is:

            void for_each ( int * first , int* last , void (*f) (const int ) );

            for_each will evaluate to:

            f ( *( first + N ) ) for all N in the range [0 , last - first ]

            How fill works?

            Problem: fill vector<int> v with value 10.

            Solution: following code does the work for you:

            fill ( v.begin() , v.end() , 10 );

            Generated function prototype for the above invocation of fill is:

            void fill(int * first, int * last, const int &) ;

            fill evaluates *(first + N) = x once for each N in the range [0, last - first].

            How count_if works?

            Problem: count number of '2's in an integer array.

            Solution: following code does the work for you:

            bool f2 ( const int x )
            {
                if ( x == 2 )
                    return true;
                else
                    return false;
            }

            Assume vector<int> v contains integers. Following code will return number of '2's in the vector:

            int iNoof2s = count_if( v.begin() , v.end() , f2 );

            Generated function prototype for the above code is:

            unsigned int count_if (  int * first , int * last , bool (*fn) ( const int ) );

            count_if sets a count n to zero. It then executes ++n for each N in the range [0, last - first] for which the predicate fn(*(first + N)) is true. It evaluates the predicate exactly last - first times.

            Conclusion

            You have got an idea about how vector and some of the algorithms work. Try using other containers and explore more algorithms. You will discover more interesting things. Happy coding with STL.

            About Jais Joy


            5+ year in software industry.Currently working for IBM Bangalore
            Looking forward to contribute to software industry.

            Click here to view Jais Joy's online profile.

            posted on 2005-12-28 08:46 夢在天涯 閱讀(2343) 評論(2)  編輯 收藏 引用 所屬分類: STL/Boost

            評論

            # re: 理解STL---understanding stl 2006-01-10 11:35 shanzy

            CP網(wǎng)站上評分才1.98,可見文章的可看程度是多少??

            原文作者使用的是VC自帶的STL,都知道這個(gè)可讀性幾乎為0

            另外,文章的出處還沒有注明:

            http://www.codeproject.com/vcpp/stl/stl_by_code_walk.asp  回復(fù)  更多評論   

            # re: 理解STL---understanding stl 2006-01-10 12:11 shanzy

            總的來說,原文作者的文章還是不錯(cuò),不過有的地方說的有點(diǎn)含糊

            http://www.codeproject.com/vcpp/stl/stlintroduction.asp

            This example declares a class Value, which stores a parameterized value, _value, of type T.

            value不應(yīng)該算:template parameter list中的1個(gè),要算也要算是類的成員函數(shù)的參數(shù)列表中的一個(gè)  回復(fù)  更多評論   

            公告

            EMail:itech001#126.com

            導(dǎo)航

            統(tǒng)計(jì)

            • 隨筆 - 461
            • 文章 - 4
            • 評論 - 746
            • 引用 - 0

            常用鏈接

            隨筆分類

            隨筆檔案

            收藏夾

            Blogs

            c#(csharp)

            C++(cpp)

            Enlish

            Forums(bbs)

            My self

            Often go

            Useful Webs

            Xml/Uml/html

            搜索

            •  

            積分與排名

            • 積分 - 1807503
            • 排名 - 5

            最新評論

            閱讀排行榜

            无码专区久久综合久中文字幕 | 精品无码久久久久久久久久| 国产99久久久国产精免费| 久久久精品国产亚洲成人满18免费网站 | 青青草原综合久久大伊人导航| 久久影视国产亚洲| 精品久久8x国产免费观看| 精品水蜜桃久久久久久久| 国内精品人妻无码久久久影院导航 | 亚洲精品WWW久久久久久| 久久A级毛片免费观看| 久久男人中文字幕资源站| 国产精品一区二区久久国产| 香蕉99久久国产综合精品宅男自| 久久国产精品一国产精品金尊| 久久精品免费大片国产大片| 77777亚洲午夜久久多喷| 久久人人爽人人爽人人爽| 久久国产精品99精品国产987| 亚洲精品无码专区久久久 | 久久久久久久波多野结衣高潮| 国产国产成人精品久久| 中文字幕久久久久人妻| 色天使久久综合网天天| 久久国产高清一区二区三区| 久久精品嫩草影院| 99久久99久久久精品齐齐| 久久丫精品国产亚洲av不卡| 久久人人爽人人爽人人av东京热| 久久99精品九九九久久婷婷| 国产成人精品久久免费动漫| 久久久久亚洲精品天堂| 久久中文骚妇内射| 久久久久久国产精品无码超碰| 国产亚洲美女精品久久久2020| 综合久久精品色| 亚洲国产成人久久一区WWW| 精品欧美一区二区三区久久久| 久久精品国产WWW456C0M| 欧美午夜A∨大片久久| 青青久久精品国产免费看|