STL Tutorial and Reference Guide -- summary

STL GP--generic program

It's same to OOP, it's an other program method.
Class Template

How C++ Templates Enable Generic Programming

1.Class Templates
There is usually more to making an ordinary class into a generic one than just adding the template keyword and type parameter list. Some modifications may be necessary to ensure that the resulting instances are as efficient as ones defined by ordinary means, no matter what actual types are substituted.
    template <typename T1, typename T2>
    class pair {
        public:
            T1 first;
            T2 second;
            pair() : first(T1()), second(T2()){ }
            pair(const T1& x, const T2& y) : first(x), second(y){ }
    }

2.Function Templates
    bool operator <(const pair<double, long>& x,const pair<double, long>& y)
    {
        return x.first<y.first;
    }
    template <typename T>
    const T& max(const T& x, const T& y){
    if (x<y)
        return y;
    else
        return x;
    }
3.Member Function Templates
    template <typename T>
    class vector{
        public:
        template <typename InputIterator>
        void insert(iterator position, Input Iterator first, Input Iterator last);
    // ...
    };
4.Explicit Specification of Template Arguments
template <typename Container>
Container make(const char s[]){
return Container(&s[0], &s[strlen(s)]);
}

list <char> L = make("Hello, world"); // Incorrect
list <char> L = make< list<char> >("Hello, world!");
This is called explicit specification of the template argument.

5.Default Template Parameters
    template <typename T, typename Allocator = allocator<T> >
    class vector{
        // ...
    }
6.Partial Specialization

Understanding STL's Performance Guarantees
1.Big-Oh Notation and Related Definitions
  T(N)=O(N) The algorithm is said to have a linear time bound
  T(N)=O(N^2) quadratic time bound
  T(N)=O(log N) logarithmic time bound
  T(N)=O(NlogN) NlogN time bound
  T(N)=O(1) a constant time bound

2.Amortized Time complexity
For example, the worst-case time for inserting at the end of an STL vector is O(N), where N is the size of the container, because if there is no extra room the insert operation must allocate new storage and move all the existing elements into the new storage. but if the inserting is small than N, the time is O(1). Thus, we say that the amortized time for insertion is constant or that the time is amortized constant.

3.Limitation of Big-Oh Notation
Two O(N) algorithms can differ dramatically in therir computing times. One could be uniformly 2 or 10 or 100 times faster than the other, but big Oh notation suppresses all such differences.

<Chapter 2>Overview of STL Components
1.Container
<1>Sequence Containers
vector<T>
deque<T>
list<T>
<2>Sorted Associative Containers
multiset<Key>
map<Key, T>
multimap<Key. T>
2.Generic Algorithms
<1> Generic Find Algorithm
char s[] = "C++ is a better C";
const char* where = find(&s[0], &s[len], 'e');
assert (*where == 'e' && *(where+1) == 't');
<2> Generic Merge Algorithm
merge(&s[0],&s[len], list1.begin(), list1.end(), deque1.begin());
assert (deque1 == make< deque<char> >("abcdefghijklmnopq"));
3.Iterators


<Chapter 3>How STL Differs from Other Libraries
Compnent Interchangeability
Algorithm/Container Compatibility
all STL algorithm and container components could be made to interface together, but it was deliberately decided not to do so simply because some algorithms would not be efficient when used with some containers.

<Chapter 4> Iterators
1.Input Iterators
2.Output Iterators
3.Forward Iterators
4.Bidirectional Iterators
5.Random Access Iterators

The STL Iterator Hierarchy: Combining Algorithms and Containers Efficiently.
Insert Iterators
Revisiting Input and Output: Stream Iterators
Designing Generic Algorithms
Choosing the Right Algorithm

<Chapter 5> Generic Algorithms
STL generic algorithms fall into four broad categories, based loosely on their semantics.
Nonmutating sequence algorithms: operate on containers without, in general, modifying the contents of the container
mutating sequence algorithms: typically modify the containers on which they operate
sorting-related algorithms: include sorting and merging algorithms, binary searching algorithms, and set operations on sorted sequences.
generalized numeric algorithms

<Chapter 6>Sequence COntainers
STL provides two families of abstractions, sequence containers (vector, deque, and list) and sorted associative containers (set, multiset, map, and multimap).

Vectors
Each STL container class defines and makes public serveral types:
value_type (T)
pointer (usually T*, but more generally it is determined by the allocator type)
reference (T&)
iterator Iterator type referring to values of type reference; for vertors this type is of the random access category
difference_type Signed integral type hat can represent the difference between two iterators
size_type Unsigned integral type that can represent any nonnegative value of difference type.
reverse_iterator
With vectors the iterator type are all random access types, the most powerful category.

Deques
The main difference is in performance: insertion and erasure at the beginning of a deque are much faster than with a vector, taking only constant time rather than linear time, while other operations are the same as or slower by a constant factor than the corresponding vector opertaions.

Lists
some real differences from vectors or deques, the reason is that lists give up random access iterators in order to allow constant time insertions and deletions.

<Chapter 7>Sorted Associative Constainers
sorted One general approach to associative retrieval is to keep keys sorted according to some total order.
Another approach is hashing: dividing the key space into some number of subsets, inserting each key into its designated subset, and correspondingly confining each search to just on subset, with the association of a key to a subset being done by a "hash" function.
The main advantage of the hased approach is constant average time for storage and retrieval, while that of the sorted approach is reliable performance. (Worst-case performance of hash table opertaions can be very bad-linear in the size, N, of the table -- under some circumstances, but that of balanced binary trees is always O(logN).)

Ideally, both sorted and hashed associative constainers should be in the C++ Standard Library, but only sorted associative container are included.

Set and Multisets
Both classes provide definitions of the same types as the sequence containers. plus the following:
key_type
key_compare
value_compare.

template <type Key, typename Compare = less<Key>, class Allocator =  allocator<Key> >

set(const Compare& comp = compare());

pair<iterator, bool> insert(const value_type& x);
while in class multiset it is
iterator insert(const value_type& x);
In the set version, the iterator returned refers to the position of x in the set, whether or not it was already there, and the bool value is ture if the element was inserted, false if it was already there.

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值