stl 学习笔记 12

// cont/array1.cpp
/*
======================
array 可以做为stl容器。但不提供 begin()和end(),也不提供任何成员函数
======================
*/

#include 
< iostream >
#include 
< algorithm >
#include 
< functional >
using   namespace  std;

int  main()
{
    
int coll[] = {5,6,2,4,1,3};
    transform(coll,coll
+6,coll,coll,multiplies<int>());
    sort(coll
+1,coll+6);
    copy(coll,coll
+6,ostream_iterator<int>(cout," "));
    cout 
<<endl;
}


/*
result
====================
25 1 4 9 16 36
*/

 

数组外包装,在数组外包装一层常用的容器界面,性能不输一般的数组,而且更安全

// stl/carray.h
#ifndef CARRAY_H
#define  CARRAY_H

#include 
< cstddef >

template 
< class  T,std::size_t thesize >
class  carray
{
private:
    T v[thesize];
public:
    typedef T                value_type;
    typedef T
*                iterator;
    typedef 
const T*        const_iterator;
    typedef T
&                reference;
    typedef 
const T&        const_reference;
    typedef std::size_t        size_type;
    typedef std::ptrdiff_t    difference_type;

    iterator begin() 
{return v;}
    const_iterator begin() 
const return v;}
    iterator end() 
{return v+thesize;}
    const_iterator end() 
const {return v+thesize;}

    reference 
operator[](std::size_t i)
    
{return v[i];}
    const_iterator 
operator[](std::size_t i) const
    
{return v[i];}

    size_type size() 
const
    
{return thesize;}
    size_type max_size() 
const
    
{return thesize;}

    T
* as_array()
    
{return v;}
}
;

#endif



#include 
< iostream >
#include 
< functional >
#include 
< algorithm >
#include 
" print.h "
#include 
" carray.h "
using   namespace  std;

int  main()
{
    carray
<int,10> a;

    
for(unsigned i = 0; i <a.size();++i)
        a[i] 
=i+1;

    PRINT_ELEMENTS(a);

    reverse(a.begin(),a.end());
    PRINT_ELEMENTS(a);
    transform(a.begin(),a.end(),a.begin(),negate
<int>());
    PRINT_ELEMENTS(a);
}


/*
result
================================
 1 2 3 4 5 6 7 8 9 10
 10 9 8 7 6 5 4 3 2 1
 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1
*/

 

 

#ifndef COUNTER_PRT_H
#define  COUNTER_PTR_H
/*
stl 容器提供的是value 语义 而不是 reference 语义,他在内部共建了元素的副本,有时候元素复制的代价过大,就要采用
智能型指针。这是个解决办法,采用的是 reference counting(参考计数)智能型指针。
*/


template 
< typename T >
class  CountedPtr
{
private:
    T
* ptr;         //ptr
    long* count; //counting
public:
    
explicit CountedPtr(T* p = 0// 避免 = 的类型转换
        :ptr(p),count(new long(1))
    
{}

    CountedPtr(
const CountedPtr<T>& p) throw() // 拷贝构造函数,++counter
        :ptr(p.ptr),count(p.count)
    
{
        
++*count;
    }

    
~CountedPtr() throw()
    
{
        dispose();
    }

    CountedPtr
<T>& operator=(const CountedPtr<T>& p) throw() // =
    {
        
if(this != &p)
        
{
            dispose();
            ptr 
= p.ptr;
            count 
= p.count;
            
++*count;
        }

        
return *this;
    }


    T
& operator*()const throw() // *
    {
        
return *ptr;
    }


    T
* operator ->()const throw() // ->
    {
        
return ptr;
    }


private:
    
void dispose()    //dispose
    {
        
if(--*count == 0)
        
{
            delete count;
            delete ptr;
        }

    }

}
;
#endif

 

 

// stl/refsem1.cpp
/*
===================================================================
如何使用countedPtr<T>
===================================================================
*/

#include 
< iostream >
#include 
< list >
#include 
< deque >
#include 
< algorithm >
#include 
" countptr.h "
using   namespace  std;

void  PrintCountedPtr(CountedPtr < int >  elem)
{
    cout 
<<*elem <<' ';
}


int  main()
{
    
static int values[] = {3,5,9,1,6,4};
    typedef CountedPtr
<int> IntPtr;
    deque
<IntPtr> coll1;
    list
<IntPtr> coll2;

    
for(int i = 0; i <sizeof(values)/sizeof(values[0]);++i)
    
{
        IntPtr ptr(
new int(values[i]));
        coll1.push_back(ptr);
        coll2.push_front(ptr);
    }


    for_each(coll1.begin(),coll1.end(),PrintCountedPtr);
    cout 
<<endl;
    for_each(coll2.begin(),coll2.end(),PrintCountedPtr);
    cout 
<<endl <<endl;

    
*coll1[2*= *coll1[2];
    (
**coll1.begin()) *= -1;
    (
**coll2.begin()) = 0;

    for_each(coll1.begin(),coll1.end(),PrintCountedPtr);
    cout 
<<endl;
    for_each(coll2.begin(),coll2.end(),PrintCountedPtr);
    cout 
<<endl;
    
return 0;
}


/*
result
===========================
3 5 9 1 6 4
4 6 1 9 5 3

-3 5 81 1 6 0
0 6 1 81 5 -3
*/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值