stl 学习笔记 12
2007-12-16 03:16
369 查看
//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
*/
相关文章推荐
- C++ Primer 学习笔记_38_STL实践与分析(12)--容器的综合应用:文本查询程序
- STL学习笔记12— —特殊容器queue
- STL学习笔记----12.STL算法之 (移除性算法)
- STL学习笔记----12.STL算法之 (移除性算法)
- C++ Primer 学习笔记_38_STL实践与分析(12)--集成的应用程序容器:文本查询程序
- STL学习笔记— —特殊容器queue
- STL基础学习笔记
- (12)JavaScript学习笔记 - 函数(续)
- 46、我的C#学习笔记12
- cocos2d-x学习笔记12:如何将win32移植到iOS
- STL学习笔记之容器--优先队列
- Oracle 学习笔记 12 -- 序列、索引、同义词
- Linux 学习笔记_12_文件共享服务_2_FTP应用--vsftpd
- C++ STL学习笔记八 multiset多重集合容器
- STL 学习笔记 ( 一. 概述 )
- 12/21 学习笔记 hashcode String
- 【算法学习笔记】12.数据结构基础 图的初步1
- 09 12 07 Java学习笔记
- iOS学习笔记--12 NSLog相关
- STL(学习笔记1-STL巡礼)