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

C++ Programmer's Cookbook

{C++ 基礎} {C++ 高級} {C#界面,C++核心算法} {設計模式} {C#基礎}

理解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 夢在天涯 閱讀(2365) 評論(2)  編輯 收藏 引用 所屬分類: STL/Boost

評論

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

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

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

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

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

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

總的來說,原文作者的文章還是不錯,不過有的地方說的有點含糊

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

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

value不應該算:template parameter list中的1個,要算也要算是類的成員函數的參數列表中的一個  回復  更多評論   

公告

EMail:itech001#126.com

導航

統計

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

常用鏈接

隨筆分類

隨筆檔案

收藏夾

Blogs

c#(csharp)

C++(cpp)

Enlish

Forums(bbs)

My self

Often go

Useful Webs

Xml/Uml/html

搜索

  •  

積分與排名

  • 積分 - 1811979
  • 排名 - 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>
              日韩香蕉视频| 亚洲三级性片| 亚洲在线视频免费观看| 亚洲最新视频在线播放| 国产欧美精品日韩| 欧美www视频| 亚洲视频你懂的| 女人香蕉久久**毛片精品| 在线综合视频| 亚洲日韩欧美一区二区在线| 国产欧美三级| 一区二区在线看| 国产精品毛片a∨一区二区三区|国 | 久久精品综合| 宅男噜噜噜66一区二区| 亚洲免费在线视频一区 二区| 亚洲高清电影| 国产亚洲欧美一区二区| 欧美日韩www| 欧美成人精品一区二区| 国产精品免费网站| 亚洲黄色一区| 亚洲黑丝一区二区| 亚洲欧美视频一区| 性色一区二区三区| 午夜激情一区| 免费不卡在线视频| 欧美激情欧美狂野欧美精品| 毛片精品免费在线观看| 猫咪成人在线观看| 亚洲午夜精品久久久久久浪潮 | 欧美一区二区久久久| 亚洲欧美在线看| 性伦欧美刺激片在线观看| 亚洲一区日韩| 欧美韩国日本综合| 欧美日韩国产成人在线观看| 国产亚洲一区二区三区| 亚洲一区二区精品视频| 欧美一区二区三区精品电影| 午夜精品久久久99热福利| 欧美福利影院| 99国产一区| 亚洲欧美大片| 久久国产婷婷国产香蕉| 午夜国产精品影院在线观看 | 免费欧美日韩| 欧美精品在线观看播放| 欧美日韩另类视频| 国产精品蜜臀在线观看| 99re热精品| 最新国产精品拍自在线播放| 91久久在线观看| 美女爽到呻吟久久久久| 欧美日韩国内自拍| 亚洲精品国产精品国自产观看浪潮| 久久五月激情| 亚洲精品一区二区三区四区高清 | 久久国产66| 国产又爽又黄的激情精品视频| 亚洲娇小video精品| 久久在线视频在线| 久久亚洲国产成人| 欧美色大人视频| 国产区在线观看成人精品| 亚洲欧美日韩在线高清直播| 亚洲一区在线免费观看| 国产午夜精品理论片a级探花| 日韩视频二区| 久久久久久久综合日本| 欧美激情黄色片| 麻豆成人在线观看| 99热免费精品| 亚洲午夜电影| 黄色资源网久久资源365| 免费视频一区| 欧美日韩亚洲精品内裤| 欧美一区二区三区视频在线| 久久精品国产亚洲精品| 欧美日韩人人澡狠狠躁视频| 亚洲午夜一区二区三区| 午夜精品www| 亚洲国产欧美日韩精品| 一区二区日韩免费看| 欧美二区视频| 亚洲无线视频| 欧美中文字幕在线播放| 国产精品日韩欧美综合| 99在线热播精品免费99热| 艳妇臀荡乳欲伦亚洲一区| 国产亚洲精品aa| 亚洲美女精品成人在线视频| 国产一本一道久久香蕉| 欧美激情一区二区三区高清视频 | 国产在线拍揄自揄视频不卡99| 亚洲第一偷拍| 久久综合九色综合欧美就去吻| 国产精品试看| 欧美jjzz| 国产性天天综合网| 国产日韩欧美成人| 亚洲经典在线看| 亚洲视频在线播放| 亚洲二区在线视频| 亚洲女与黑人做爰| 99热在线精品观看| 久久深夜福利| 久久精品国产亚洲一区二区| 欧美一区二区三区婷婷月色| 91久久线看在观草草青青| 亚洲大胆av| 欧美成人精品1314www| 欧美在线一二三| 性欧美暴力猛交69hd| 一本久道综合久久精品| 久久女同互慰一区二区三区| 欧美一区二区视频97| 国产精品久久久久久久久久免费 | 中日韩男男gay无套| 中文国产成人精品久久一| 亚洲大片在线| 亚洲免费观看| 国产亚洲欧美日韩美女| 亚洲色诱最新| 国产精品99久久久久久人| 欧美激情第五页| 亚洲国产欧美日韩另类综合| 国产精品久久综合| 亚洲最新视频在线播放| 夜夜精品视频一区二区| 欧美激情91| 亚洲人成啪啪网站| 一区二区毛片| 欧美日韩在线三区| 亚洲美女精品一区| 亚洲手机在线| 国产精品视频99| 欧美中文字幕在线观看| 久久久久国产一区二区| 黑人操亚洲美女惩罚| 日韩亚洲成人av在线| 99精品视频免费在线观看| 欧美日韩国产欧| 亚洲调教视频在线观看| 午夜精品剧场| 国产日韩欧美综合精品| 久久精品成人| 亚洲缚视频在线观看| 一区二区三区免费观看| 国产精品三区www17con| 久久精品国产精品亚洲精品| 欧美大片在线观看一区二区| 日韩亚洲一区二区| 国产精品久久九九| 久久精品亚洲精品国产欧美kt∨| 欧美肥婆在线| 亚洲欧美成人| 在线播放日韩| 久久精品人人| 亚洲欧洲一区二区三区久久| 亚洲欧美激情精品一区二区| 国产婷婷色综合av蜜臀av| 久久一区二区三区av| 日韩视频一区二区| 久久精品国产精品亚洲| 亚洲精品视频免费| 国产精品嫩草影院av蜜臀| 久久精品国产精品亚洲| 久久综合网hezyo| 亚洲精品专区| 浪潮色综合久久天堂| 亚洲天堂网在线观看| 在线观看视频日韩| 国产精品视频免费观看| 免费成人高清视频| 午夜精品免费| 亚洲精品久久久蜜桃| 另类天堂视频在线观看| 亚洲性夜色噜噜噜7777| 亚洲第一伊人| 国产综合久久久久久| 国产精品久久久久影院亚瑟 | 亚洲欧美国产毛片在线| 亚洲国产日韩欧美在线99| 久久九九国产精品| 亚洲一级特黄| 夜夜嗨av一区二区三区中文字幕 | 欧美激情中文字幕乱码免费| 欧美激情精品久久久久久久变态| 亚洲欧美日韩国产一区二区三区| 亚洲国产天堂久久综合| 国产主播精品| 国产网站欧美日韩免费精品在线观看| 欧美日韩亚洲一区二| 噜噜噜躁狠狠躁狠狠精品视频| 午夜视频在线观看一区二区| 一区二区三区毛片| 日韩视频免费在线观看| 亚洲经典在线看| 欧美激情国产日韩|