青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

C++ Programmer's Cookbook

{C++ 基礎(chǔ)} {C++ 高級} {C#界面,C++核心算法} {設(shè)計模式} {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 夢在天涯 閱讀(2373) 評論(2)  編輯 收藏 引用 所屬分類: STL/Boost

評論

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

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

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

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

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

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

總的來說,原文作者的文章還是不錯,不過有的地方說的有點(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個,要算也要算是類的成員函數(shù)的參數(shù)列表中的一個  回復(fù)  更多評論   

公告

EMail:itech001#126.com

導(dǎo)航

統(tǒng)計

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

常用鏈接

隨筆分類

隨筆檔案

收藏夾

Blogs

c#(csharp)

C++(cpp)

Enlish

Forums(bbs)

My self

Often go

Useful Webs

Xml/Uml/html

搜索

  •  

積分與排名

  • 積分 - 1815011
  • 排名 - 5

最新評論

閱讀排行榜

青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <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>
              99re视频这里只有精品| 国产视频一区在线观看| 亚洲乱码精品一二三四区日韩在线| 久久精品人人做人人综合| 午夜精品视频在线| 欧美一区二区三区免费视| 午夜精品久久久久久久久久久| 午夜精品久久久久久久99黑人| 午夜一区二区三区不卡视频| 欧美在线国产| 美女亚洲精品| 91久久综合| 亚洲国产精品一区二区www| 久久久精品动漫| 免费亚洲电影| 亚洲国产女人aaa毛片在线| 亚洲丰满少妇videoshd| 亚洲国产99| 亚洲精品国偷自产在线99热| 亚洲免费av片| 亚洲一区二区三区三| 蜜臀久久99精品久久久画质超高清 | 亚洲欧洲午夜| 在线观看中文字幕亚洲| 亚洲第一区在线| 91久久综合亚洲鲁鲁五月天| 日韩视频在线一区| 亚洲一区二区久久| 久久精品国产第一区二区三区| 久久一区二区三区四区| 欧美电影电视剧在线观看| 亚洲国产一二三| 亚洲一级在线观看| 久久久久成人精品| 欧美国产日韩精品| 国产精品久久久久久亚洲调教| 国产精品久久久91| 狠狠色丁香久久综合频道| 亚洲精品九九| 亚洲激情另类| 亚洲欧美色一区| 久久久亚洲国产美女国产盗摄| 亚洲国产二区| 午夜免费在线观看精品视频| 老司机午夜精品视频在线观看| 欧美刺激午夜性久久久久久久| 国产精品久久久久91| 国产综合欧美| 夜夜嗨av一区二区三区四区 | 精品不卡在线| 一片黄亚洲嫩模| 久久一区二区视频| 亚洲精品日韩在线| 久久国产精品99精品国产| 欧美另类高清视频在线| 国产亚洲精品久久久久久| 国产九九精品| 一区二区三区在线视频播放| 亚洲激情第一区| 日韩午夜在线视频| 欧美在线视频一区二区| 欧美成人国产va精品日本一级| 亚洲第一网站| 亚洲欧美国产另类| 国产精品va在线| 亚洲精品久久久久中文字幕欢迎你| 老司机午夜精品| 欧美一级片一区| 欧美精品在线免费观看| 亚洲福利国产精品| 久久久久久成人| 亚洲一区二区三区四区五区午夜| 欧美国产91| 伊人伊人伊人久久| 久久se精品一区精品二区| 99国产精品国产精品久久| 美女网站久久| 黄网动漫久久久| 久久久av网站| 欧美一级久久| 国产一区二区久久| 久久久久久久久久看片| 欧美一区二区三区久久精品茉莉花 | 国产精品av免费在线观看| 最新热久久免费视频| 久久综合网络一区二区| 欧美一区二区大片| 韩日欧美一区二区| 久久九九精品| 久久国产综合精品| 激情欧美日韩| 麻豆91精品| 久久综合狠狠综合久久激情| 尤妮丝一区二区裸体视频| 卡通动漫国产精品| 免费av成人在线| 一区二区三区高清| 亚洲视频狠狠| 国产视频亚洲精品| 能在线观看的日韩av| 欧美激情亚洲视频| 亚洲一区二区精品视频| 欧美一级片在线播放| 在线观看成人网| 久久久久久国产精品一区| 午夜免费在线观看精品视频| 欧美精品日韩三级| 亚洲精品男同| 欧美激情女人20p| 欧美 日韩 国产一区二区在线视频| 亚洲黄色成人久久久| 亚洲巨乳在线| 国产日韩欧美日韩大片| 欧美成年人视频| 欧美网站大全在线观看| 久久精品理论片| 欧美激情麻豆| 篠田优中文在线播放第一区| 久久精品99无色码中文字幕| 国产精品你懂的在线| 亚洲一区精品电影| 国产视频在线一区二区| 日韩一本二本av| 亚洲自拍三区| 亚洲国产你懂的| 亚洲午夜免费视频| 亚洲人午夜精品免费| 亚洲免费一区二区| 亚洲精品一区二区三区婷婷月| 亚洲激情成人| 欧美国产一区二区| 一区二区三区四区国产精品| 一本色道久久加勒比88综合| 国产精自产拍久久久久久| 亚洲电影下载| 国产一区二区三区久久久久久久久| 亚洲国产婷婷香蕉久久久久久| 国产欧美日本一区视频| 亚洲黄色在线| 亚洲第一视频| 亚洲欧美日韩在线播放| 99国产精品私拍| 久久久久久夜精品精品免费| 亚洲自拍偷拍网址| 欧美日韩不卡| 亚洲激情在线观看| 亚洲福利久久| 久久国内精品自在自线400部| 性伦欧美刺激片在线观看| 欧美日韩高清在线播放| 欧美成人精品在线观看| 国产在线拍偷自揄拍精品| 亚洲国产国产亚洲一二三| 国产精品自在在线| 亚洲欧美另类久久久精品2019| 中文有码久久| 欧美日韩成人在线播放| 欧美精品一区二区三区很污很色的 | 国产精品一区一区三区| 91久久精品国产| 亚洲福利电影| 欧美成人国产| 亚洲国产一区二区三区高清| 性色av一区二区三区红粉影视| 亚洲女优在线| 国产精品捆绑调教| 欧美精品一区在线播放| 亚洲激情影视| 在线视频精品| 久久香蕉国产线看观看av| 久久综合网hezyo| 雨宫琴音一区二区在线| 久久亚洲精品中文字幕冲田杏梨| 久久久久久电影| 狠狠爱综合网| 免费观看日韩| 亚洲第一久久影院| 亚洲最新视频在线| 国产精品久久久一区二区| 亚洲免费视频成人| 久久一区二区三区av| 亚洲人成网站色ww在线| 欧美日韩1区2区3区| 久久精品国产欧美亚洲人人爽| 亚洲女人小视频在线观看| 欧美在线播放高清精品| 韩国一区二区在线观看| 免费中文日韩| 亚洲国产小视频在线观看| 亚洲高清一区二| 欧美色道久久88综合亚洲精品| 亚洲一区二区视频在线| 久久久久成人精品| 亚洲激情专区| 国产精品久久久久久久9999| 午夜精品久久久久久久99樱桃| 免费人成精品欧美精品| 宅男66日本亚洲欧美视频| 国产精自产拍久久久久久| 久久婷婷综合激情|