身处项目实践中,就会明白程序猿们的基础是何其之重要,算法、数据结构,编程风格和编程规范这些基础,在你开始项目的头一刻就体现出来了。所以可以有个比较疯狂的想法,想利用闲暇时间多写写数据结构的实现,按照这个列表来写。没想写的多漂亮,STL那种严谨、高效而又优雅的代码俺这种小喽喽下辈子也写不出来,权当作是自己复习数据结构吧,最近也有打算完成大二下开始的、但一直未尽的USACO事业,囧,上USACO网站,登录名和密码全忘记了,难道要我重新开始吗?也许为了心目的谷大哥哥,真的会重新开始,g9大大早就告诫过了,程序猿是不能懒滴啊,行吧,俺就勤快点,就重新开始吧!
#ifndef MYUTILITY_H_INCLUDED
#define MYUTILITY_H_INCLUDED
namespace myUtility
{
template <class T>inline void swap(T& x, T& y)
{
T temp = x;
x = y;
y = temp;
};
template<class T, class U>struct pair
{
typedef T first_type;
typedef U second_type;
T first;
U second;
pair(const T& x = T(), const U& y = U()):first(x),second(y) {}
template<class V, class W>pair(const pair<V, W>& pr):first(pr.first),second(pr.second) {}
};
template <class T>struct less{
bool operator()(const T& x, const T& y) const { return x < y; } };
template <class T>struct greater{
bool operator()(const T& x, const T& y) const { return x > y; } };
}
#endif //MYUTILITY_H_INCLUDED
/******************************************************************
* An array data structure or simply array is a data structure con-
* sisting of a collection of elements (values or variables), each
* identified by one or more integer indices, stored so that the
* address of each element can be computed from its index tuple by
* a simple mathematical formula.
*
*
* Author: 窦小蹦(fairyroad)
* Last update: August 21 09:00 EST 2010
* E-mail: [email protected]
*
*****************************************************************/
#ifndef SIMPLEARRAY_HPP_INCLUDED
#define SIMPLEARRAY_HPP_INCLUDED
#include<cstddef>
template<typename T ,size_t _size>
class SimpleArray
{
private:
T v[_size];
public:
SimpleArray(){Init();}
SimpleArray(const SimpleArray& other) {*this = other;}
T* first(){return v;}
const T* first()const {return v;}
T& operator[](size_t i) { return v[i];}
const T& operator[](size_t i) const { return v[i]; }
T& operator=(const SimpleArray& other)
{
if(this==&other) return *this;
for(int i=0;i<_size;i++) v[i]=other[i];
return *this;
}
//size is constant
size_t size() const { return _size; }
size_t max_size() const { return _size; }
//conversion to ordinary array
T* as_array() { return v; }
first
// some auxiliary function
bool find(const T& target)
{
for(int i=0;i<_size;i++)
{
if(target==v[i]) return true;
}
return false;
}
protected:
void Init()
{
for(int i=0;i<_size;i++)
v[i] = T();
}
}
#endif //SIMPLEARRAY_HPP_INCLUDED