C++
  Home arrow C++ arrow Page 3 - The Standard Template Library
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
C++

The Standard Template Library
By: Gabor Bernat
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 4
    2008-06-24

    Table of Contents:
  • The Standard Template Library
  • History Class
  • Looking Inside the Library
  • Iterators

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    The Standard Template Library - Looking Inside the Library


    (Page 3 of 4 )

    For the sole purpose of better understanding the presentation of the containers, I feel like we should first look at some source code. Therefore, I have chosen the vector because it is one of the most used containers and most efficient/best fitting data structures for daily programming. Check out the code snippet attached below (I have removed some lines and added “…” in their place because they were just too confusing for a beginner):

    template<class _Ty,

    class _Ax>

    class vector

    : public _Vector_val<_Ty, _Ax>

    { // varying size array of values

    vector()

    : _Mybase()

    { // construct empty vector

    _Buy(0);

    }

    void pop_back()

    { // erase element at end

    if (!empty())

    { // erase last element

    _Destroy(_Mylast - 1, _Mylast);

    --_Mylast;

    }

    }

    protected:

    void _Assign_n(size_type _Count, const _Ty& _Val)

    { // assign _Count * _Val

    _Ty _Tmp = _Val; // in case _Val is in sequence

    erase(begin(), end());

    insert(begin(), _Count, _Tmp);

    }

    pointer _Myfirst; // pointer to beginning of array

    pointer _Mylast; // pointer to current end of sequence

    pointer _Myend; // pointer to end of array

    };

    That should be enough to review the main ideas. No wonder it is all based on templates -- a template that gets to parameters. The first template is for the type of data that will be held in the container, while the second is the allocation type to use for allocating members. This has the following options:

    1.       alloc:

    This is the default allocator. It is thread-safe and usually has the best performance characteristics.

    2.       pthread_alloc:

    This has different memory pools for each existing thread. Although this is typically faster than the default, you shouldn’t forget the memory fragmentation.

    3.       single_client_alloc:

    Even though this is fast, it is unsafe for single threaded programs.

    4.       malloc_alloc:

    This should sound familiar because it uses the existing library function for C, which is malloc.

    Vector in itself is a class that contains an array of _Ty elements. It is derived from _Vector_val, which is itself from _Container_base -- the base of all containers.  _Vector_val just holds the allocator value. The main implementation of the vector is written in the vector class.

    The default constructor is quite easy to comprehend because it just constructs an empty vector, or in other words, “buys” 0 members for it. There are three pointer type values: one pointing to the start of the data structure in the memory, one at the end of the currently allocated member sequence, and one pointing to the end of the current sequence.

    This is because, for example, you may have memory allocated, let’s say, for 10 members, but you aren’t required to fill all of them with data. You may only use four items in the memory. You can easily conclude from here that whenever _Myend reaches _Mylast and you want to add another member, the class has to reallocate extra space. Space allocation in STL is done via the principle of double or nothing. It starts from a basic value (around 15, but depends on the implementation) and each time it needs more, it just doubles the currently allocated size.

    However, when you are adding many members, it can kill your process because the allocation will be called many times. For this reason, it is a good idea to pre-allocate the final memory size right when you know the number of members to be added.

    More C++ Articles
    More By Gabor Bernat


       · STL is probably one of those things that can be attributed to the evolution of...
     

    C++ ARTICLES

    - More Tricks to Gain Speed in Programming Con...
    - Easy and Efficient Programming for Contests
    - Preparing For Programming Contests
    - Programming Contests: Why Bother?
    - Polymorphism in C++
    - Overview of Virtual Functions
    - Inheritance in C++
    - Extending the Basic Streams in C++
    - Using Stringstreams in C++
    - Custom Stream Manipulation in C++
    - General Stream Manipulation in C++
    - Serialize Your Class into Streams in C++
    - Advanced File Handling with Streams in C++
    - File Handling and Streams in C++
    - The STL String Class







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
    Stay green...Green IT